Fix more code with respect to bypassing "using namespace". Currently,

Utils.h still has a using namespace, to prevent non-CommonLibs? code from
breaking. All CommonLibs? code has been compiled with tue Utils.h "using
namespace" commented out, though.
This commit is contained in:
dgotwisner 2014-03-31 10:50:02 +02:00 committed by Michael Iedema
parent 2b6d0c6fa7
commit 4575b4d264
6 changed files with 169 additions and 169 deletions

View File

@ -188,16 +188,16 @@ ConfigurationTable::ConfigurationTable(const char* filename, const char *wCmdNam
try {
if (wCmdName == NULL) { wCmdName = ""; }
LOG(INFO) << wCmdName << ":" << " List of non-default config parameters:";
string snippet("");
std::string snippet("");
ConfigurationKeyMap view = getSimilarKeys(snippet);
for (ConfigurationKeyMap::iterator it = view.begin(); it != view.end(); it++) {
string name = it->first;
std::string name = it->first;
ConfigurationKey key = it->second;
if (name != key.getName()) {
LOG(ALERT) << "SQL database is corrupt at name:"<<name <<" != key:"<<key.getName();
}
string defaultValue= key.getDefaultValue();
string value = this->getStr(name);
std::string defaultValue= key.getDefaultValue();
std::string value = this->getStr(name);
if (value != defaultValue) {
LOG(INFO) << "Config Variable"<<LOGVAR(name) <<LOGVAR(value) <<LOGVAR(defaultValue);
}
@ -209,9 +209,9 @@ ConfigurationTable::ConfigurationTable(const char* filename, const char *wCmdNam
}
string ConfigurationTable::getDefaultSQL(const std::string& program, const std::string& version)
std::string ConfigurationTable::getDefaultSQL(const std::string& program, const std::string& version)
{
stringstream ss;
std::stringstream ss;
ConfigurationKeyMap::iterator mp;
ss << "--" << std::endl;
@ -243,10 +243,10 @@ string ConfigurationTable::getDefaultSQL(const std::string& program, const std::
// optional
ss << "0,";
// description
const string description = mp->second.getDescription();
const std::string description = mp->second.getDescription();
// Try to use a quote that will work: if the description contains ' quote with " else '.
// This is not perfect because these quotes could themselves be quoted.
const char * quote = string::npos != description.find('\'') ? "\"" : "'";
const char * quote = std::string::npos != description.find('\'') ? "\"" : "'";
ss << quote;
if (mp->second.getType() == ConfigurationKey::BOOLEAN) {
ss << "1=enabled, 0=disabled - ";
@ -266,9 +266,9 @@ string ConfigurationTable::getDefaultSQL(const std::string& program, const std::
return ss.str();
}
string ConfigurationTable::getTeX(const std::string& program, const std::string& version)
std::string ConfigurationTable::getTeX(const std::string& program, const std::string& version)
{
stringstream ss;
std::stringstream ss;
ConfigurationKeyMap::iterator mp;
ss << "% START AUTO-GENERATED CONTENT" << std::endl;
@ -336,11 +336,11 @@ string ConfigurationTable::getTeX(const std::string& program, const std::string&
ss << "% END AUTO-GENERATED CONTENT" << std::endl;
ss << std::endl;
string tmp = Utils::replaceAll(ss.str(), "^", "\\^");
std::string tmp = Utils::replaceAll(ss.str(), "^", "\\^");
return Utils::replaceAll(tmp, "_", "\\_");
}
bool ConfigurationTable::defines(const string& key)
bool ConfigurationTable::defines(const std::string& key)
{
try {
ScopedLock lock(mLock);
@ -637,7 +637,7 @@ ConfigurationKeyMap ConfigurationTable::getSimilarKeys(const std::string& snippe
return tmp;
}
const ConfigurationRecord& ConfigurationTable::lookup(const string& key)
const ConfigurationRecord& ConfigurationTable::lookup(const std::string& key)
{
assert(mDB);
checkCacheAge();
@ -678,7 +678,7 @@ const ConfigurationRecord& ConfigurationTable::lookup(const string& key)
bool ConfigurationTable::isStatic(const string& key)
bool ConfigurationTable::isStatic(const std::string& key)
{
if (keyDefinedInSchema(key)) {
return mSchema[key].isStatic();
@ -690,7 +690,7 @@ bool ConfigurationTable::isStatic(const string& key)
string ConfigurationTable::getStr(const string& key)
std::string ConfigurationTable::getStr(const std::string& key)
{
// We need the lock because rec is a reference into the cache.
try {
@ -704,7 +704,7 @@ string ConfigurationTable::getStr(const string& key)
}
bool ConfigurationTable::getBool(const string& key)
bool ConfigurationTable::getBool(const std::string& key)
{
try {
return getNum(key) != 0;
@ -716,7 +716,7 @@ bool ConfigurationTable::getBool(const string& key)
}
long ConfigurationTable::getNum(const string& key)
long ConfigurationTable::getNum(const std::string& key)
{
// We need the lock because rec is a reference into the cache.
try {
@ -730,7 +730,7 @@ long ConfigurationTable::getNum(const string& key)
}
float ConfigurationTable::getFloat(const string& key)
float ConfigurationTable::getFloat(const std::string& key)
{
try {
ScopedLock lock(mLock);
@ -742,7 +742,7 @@ float ConfigurationTable::getFloat(const string& key)
}
}
std::vector<string> ConfigurationTable::getVectorOfStrings(const string& key)
std::vector<std::string> ConfigurationTable::getVectorOfStrings(const std::string& key)
{
// Look up the string.
char *line=NULL;
@ -760,7 +760,7 @@ std::vector<string> ConfigurationTable::getVectorOfStrings(const string& key)
char *lp = line;
// Parse the string.
std::vector<string> retVal;
std::vector<std::string> retVal;
while (lp) {
while (*lp==' ') lp++;
if (*lp == '\0') break;
@ -773,7 +773,7 @@ std::vector<string> ConfigurationTable::getVectorOfStrings(const string& key)
}
std::vector<unsigned> ConfigurationTable::getVector(const string& key)
std::vector<unsigned> ConfigurationTable::getVector(const std::string& key)
{
// Look up the string.
char *line=NULL;
@ -804,7 +804,7 @@ std::vector<unsigned> ConfigurationTable::getVector(const string& key)
}
bool ConfigurationTable::remove(const string& key)
bool ConfigurationTable::remove(const std::string& key)
{
assert(mDB);
@ -813,16 +813,16 @@ bool ConfigurationTable::remove(const string& key)
ConfigurationMap::iterator where = mCache.find(key);
if (where!=mCache.end()) mCache.erase(where);
// Really remove it.
string cmd = "DELETE FROM CONFIG WHERE KEYSTRING=='"+key+"'";
std::string cmd = "DELETE FROM CONFIG WHERE KEYSTRING=='"+key+"'";
return sqlite3_command(mDB,cmd.c_str());
}
void ConfigurationTable::find(const string& pat, std::ostream& os) const
void ConfigurationTable::find(const std::string& pat, std::ostream& os) const
{
// Prepare the statement.
string cmd = "SELECT KEYSTRING,VALUESTRING FROM CONFIG WHERE KEYSTRING LIKE \"%" + pat + "%\"";
std::string cmd = "SELECT KEYSTRING,VALUESTRING FROM CONFIG WHERE KEYSTRING LIKE \"%" + pat + "%\"";
sqlite3_stmt *stmt;
if (sqlite3_prepare_statement(mDB,&stmt,cmd.c_str())) return;
// Read the result.
@ -847,7 +847,7 @@ ConfigurationRecordMap ConfigurationTable::getAllPairs() const
ConfigurationRecordMap tmp;
// Prepare the statement.
string cmd = "SELECT KEYSTRING,VALUESTRING FROM CONFIG";
std::string cmd = "SELECT KEYSTRING,VALUESTRING FROM CONFIG";
sqlite3_stmt *stmt;
if (sqlite3_prepare_statement(mDB,&stmt,cmd.c_str())) return tmp;
// Read the result.
@ -856,10 +856,10 @@ ConfigurationRecordMap ConfigurationTable::getAllPairs() const
const char* key = (const char*)sqlite3_column_text(stmt,0);
const char* value = (const char*)sqlite3_column_text(stmt,1);
if (key && value) {
string skey(key);
std::string skey(key);
tmp[skey] = ConfigurationRecord(skey,value);
} else if (key && !value) {
string skey(key);
std::string skey(key);
tmp[skey] = ConfigurationRecord(skey,false);
}
src = sqlite3_run_query(mDB,stmt);
@ -869,11 +869,11 @@ ConfigurationRecordMap ConfigurationTable::getAllPairs() const
return tmp;
}
bool ConfigurationTable::set(const string& key, const string& value)
bool ConfigurationTable::set(const std::string& key, const std::string& value)
{
assert(mDB);
ScopedLock lock(mLock);
string cmd;
std::string cmd;
if (keyDefinedInSchema(key)) {
cmd = "INSERT OR REPLACE INTO CONFIG (KEYSTRING,VALUESTRING,OPTIONAL,COMMENTS) VALUES (\"" + key + "\",\"" + value + "\",1,\'" + mSchema[key].getDescription() + "\')";
} else {
@ -886,7 +886,7 @@ bool ConfigurationTable::set(const string& key, const string& value)
return success;
}
bool ConfigurationTable::set(const string& key, long value)
bool ConfigurationTable::set(const std::string& key, long value)
{
char buffer[30];
sprintf(buffer,"%ld",value);
@ -931,14 +931,14 @@ void ConfigurationTable::setUpdateHook(void(*func)(void *,int ,char const *,char
}
void ConfigurationTable::setCrossCheckHook(vector<string> (*wCrossCheck)(const string&))
void ConfigurationTable::setCrossCheckHook(std::vector<std::string> (*wCrossCheck)(const std::string&))
{
mCrossCheck = wCrossCheck;
}
vector<string> ConfigurationTable::crossCheck(const string& key) {
vector<string> ret;
std::vector<std::string> ConfigurationTable::crossCheck(const std::string& key) {
std::vector<std::string> ret;
if (mCrossCheck != NULL) {
ret = mCrossCheck(key);

View File

@ -43,8 +43,8 @@ extern ConfigurationTable gConfig;
/**@ The global alarms table. */
//@{
Mutex alarmsLock;
list<string> alarmsList;
void addAlarm(const string&);
std::list<std::string> alarmsList;
void addAlarm(const std::string&);
//@}
@ -71,7 +71,7 @@ Mutex gLogToLock;
LogGroup gLogGroup;
int levelStringToInt(const string& name)
int levelStringToInt(const std::string& name)
{
// Reverse search, since the numerically larger levels are more common.
for (int i=numLevels-1; i>=0; i--) {
@ -90,13 +90,13 @@ int levelStringToInt(const string& name)
}
/** Given a string, return the corresponding level name. */
int lookupLevel(const string& key)
int lookupLevel(const std::string& key)
{
string val = gConfig.getStr(key);
std::string val = gConfig.getStr(key);
int level = levelStringToInt(val);
if (level == -1) {
string defaultLevel = gConfig.mSchema["Log.Level"].getDefaultValue();
std::string defaultLevel = gConfig.mSchema["Log.Level"].getDefaultValue();
level = levelStringToInt(defaultLevel);
_LOG(CRIT) << "undefined logging level (" << key << " = \"" << val << "\") defaulting to \"" << defaultLevel << ".\" Valid levels are: EMERG, ALERT, CRIT, ERR, WARNING, NOTICE, INFO or DEBUG";
gConfig.set(key, defaultLevel);
@ -112,7 +112,7 @@ int getLoggingLevel(const char* filename)
if (!filename) return lookupLevel("Log.Level");
// This can afford to be inefficient since it is not called that often.
const string keyName = string("Log.Level.") + string(filename);
const std::string keyName = std::string("Log.Level.") + std::string(filename);
if (gConfig.defines(keyName)) return lookupLevel(keyName);
return lookupLevel("Log.Level");
}
@ -120,7 +120,7 @@ int getLoggingLevel(const char* filename)
//bool gCheckGroupLogLevel(const char *groupname, int loglevel)
//{
// // Gag me
// string keyName = string("Log.Group.") + groupname;
// std::string keyName = std::string("Log.Group.") + groupname;
// return gConfig.defines(keyName) ? (lookupLevel(gConfig.getStr(keyName)) >= loglevel) : false;
//}
@ -131,7 +131,7 @@ int gGetLoggingLevel(const char* filename)
// This is called a lot and needs to be efficient.
static Mutex sLogCacheLock;
static map<uint64_t,int> sLogCache;
static std::map<uint64_t,int> sLogCache;
static unsigned sCacheCount;
static const unsigned sCacheRefreshCount = 1000;
@ -147,7 +147,7 @@ int gGetLoggingLevel(const char* filename)
sCacheCount=0;
}
// Is it cached already?
map<uint64_t,int>::const_iterator where = sLogCache.find(key);
std::map<uint64_t,int>::const_iterator where = sLogCache.find(key);
sCacheCount++;
if (where!=sLogCache.end()) {
int retVal = where->second;
@ -160,7 +160,7 @@ int gGetLoggingLevel(const char* filename)
sLogCacheLock.unlock();
int level = getLoggingLevel(filename);
sLogCacheLock.lock();
sLogCache.insert(pair<uint64_t,int>(key,level));
sLogCache.insert(std::pair<uint64_t,int>(key,level));
sLogCacheLock.unlock();
return level;
}
@ -170,20 +170,20 @@ int gGetLoggingLevel(const char* filename)
// copies the alarm list and returns it. list supposed to be small.
list<string> gGetLoggerAlarms()
std::list<std::string> gGetLoggerAlarms()
{
alarmsLock.lock();
list<string> ret;
std::list<std::string> ret;
// excuse the "complexity", but to use std::copy with a list you need
// an insert_iterator - copy technically overwrites, doesn't insert.
insert_iterator< list<string> > ii(ret, ret.begin());
std::insert_iterator< std::list<std::string> > ii(ret, ret.begin());
copy(alarmsList.begin(), alarmsList.end(), ii);
alarmsLock.unlock();
return ret;
}
/** Add an alarm to the alarm list. */
void addAlarm(const string& s)
void addAlarm(const std::string& s)
{
alarmsLock.lock();
alarmsList.push_back(s);
@ -200,7 +200,7 @@ Log::~Log()
// Save alarms in the local list and echo them to stderr.
if (mPriority <= LOG_CRIT) {
if (sLoggerInited) addAlarm(mStream.str().c_str());
cerr << mStream.str() << std::endl;
std::cerr << mStream.str() << std::endl;
}
// Current logging level was already checked by the macro.
// So just log.
@ -242,7 +242,7 @@ Log::Log(const char* name, const char* level, int facility)
}
ostringstream& Log::get()
std::ostringstream& Log::get()
{
assert(mPriority<numLevels);
mStream << levelNames[mPriority] << ' ';
@ -288,7 +288,7 @@ void gLogInit(const char* name, const char* level, int facility)
// Pat added, tired of the syslog facility.
// Both the transceiver and OpenBTS use this same facility, but only OpenBTS/OpenNodeB may use this log file:
string str = gConfig.getStr("Log.File");
std::string str = gConfig.getStr("Log.File");
if (gLogToFile==0 && str.length() && 0==strncmp(gCmdName,"Open",4)) {
const char *fn = str.c_str();
if (fn && *fn && strlen(fn)>3) { // strlen because a garbage char is getting in sometimes.
@ -368,7 +368,7 @@ static const char *LogGroupPrefix = "Log.Group.";
#if UNUSED
// Return true if this was a LogGroup config parameter.
// These dont have to be fast.
bool LogGroup::setGroup(const string groupName, const string levelName)
bool LogGroup::setGroup(const std::string groupName, const std::string levelName)
{
const int len = strlen(LogGroupPrefix);
if (0 != strncasecmp(groupName.c_str(),LogGroupPrefix,len)) { return false; }
@ -380,13 +380,13 @@ bool LogGroup::setGroup(const string groupName, const string levelName)
}
//GroupMapType::iterator it = mGroupNameToIndex.find(groupName);
//if (it != map::end) {
//if (it != std::map::end) {
// mDebugLevel[it->second] = lookupLevel(levelName);
//}
return true;
}
bool LogGroup::unsetGroup(const string groupName)
bool LogGroup::unsetGroup(const std::string groupName)
{
const int len = strlen(LogGroupPrefix);
if (0 != strncasecmp(groupName.c_str(),LogGroupPrefix,len)) { return false; }
@ -398,7 +398,7 @@ bool LogGroup::unsetGroup(const string groupName)
}
//GroupMapType::iterator it = mGroupNameToIndex.find(groupName);
//if (it != map::end) {
//if (it != std::map::end) {
// mDebugLevel[it->second] = lookupLevel(levelName);
//}
return true;
@ -408,11 +408,11 @@ bool LogGroup::unsetGroup(const string groupName)
void LogGroup::setAll()
{
LOG(DEBUG);
string prefix = string(LogGroupPrefix);
std::string prefix = std::string(LogGroupPrefix);
for (unsigned g = 0; g < _NumberOfLogGroups; g++) {
string param = prefix + mGroupNames[g];
std::string param = prefix + mGroupNames[g];
if (gConfig.defines(param)) {
string levelName = gConfig.getStr(param);
std::string levelName = gConfig.getStr(param);
LOG(DEBUG) << "Setting "<<LOGVAR(param)<<LOGVAR(levelName);
//mDebugLevel[g] = lookupLevel(levelName);
mDebugLevel[g] = levelStringToInt(levelName);

View File

@ -55,7 +55,7 @@ void lockCout()
void unlockCout()
{
std::cout << dec << std::endl << flush;
std::cout << std::dec << std::endl << std::flush;
gStreamLock.unlock();
}
@ -64,12 +64,12 @@ void lockCerr()
{
gStreamLock.lock();
Timeval entryTime;
cerr << entryTime << " " << pthread_self() << ": ";
std::cerr << entryTime << " " << pthread_self() << ": ";
}
void unlockCerr()
{
cerr << dec << std::endl << flush;
std::cerr << std::dec << std::endl << std::flush;
gStreamLock.unlock();
}
@ -119,9 +119,9 @@ bool Mutex::timedlock(int msecs) // Wait this long in milli-seconds.
return ETIMEDOUT != pthread_mutex_timedlock(&mMutex, &timeout);
}
string Mutex::mutext() const
std::string Mutex::mutext() const
{
string result;
std::string result;
result.reserve(100);
//result += format("lockid=%u lockcnt=%d",(unsigned)this,mLockCnt);
result += format("lockcnt=%d",mLockCnt);

132
Utils.cpp
View File

@ -67,7 +67,7 @@ MemStats::MemStats()
memset(mMemName,0,sizeof(mMemName));
}
void MemStats::text(ostream &os)
void MemStats::text(std::ostream &os)
{
os << "Structs current total:\n";
for (int i = 0; i < mMax; i++) {
@ -96,12 +96,12 @@ void MemStats::memChkDel(MemoryNames memIndex, const char *id)
}
}
ostream& operator<<(std::ostream& os, std::ostringstream& ss)
std::ostream& operator<<(std::ostream& os, std::ostringstream& ss)
{
return os << ss.str();
}
ostream &osprintf(std::ostream &os, const char *fmt, ...)
std::ostream &osprintf(std::ostream &os, const char *fmt, ...)
{
va_list ap;
char buf[300];
@ -113,16 +113,16 @@ ostream &osprintf(std::ostream &os, const char *fmt, ...)
return os;
}
string format(const char *fmt, ...)
std::string format(const char *fmt, ...)
{
va_list ap;
char buf[200];
va_start(ap,fmt);
int n = vsnprintf(buf,199,fmt,ap);
va_end(ap);
string result;
std::string result;
if (n <= 199) {
result = string(buf);
result = std::string(buf);
} else {
if (n > 5000) { LOG(ERR) << "oversized string in format"; n = 5000; }
// We could use vasprintf but we already computed the length...
@ -132,12 +132,12 @@ string format(const char *fmt, ...)
vsnprintf(buffer,n+1,fmt,ap);
va_end(ap);
//if (n >= (2000-4)) { strcpy(&buf[(2000-4)],"..."); }
result = string(buffer);
result = std::string(buffer);
free(buffer);
}
return result;
#if 0 // Maybe ok, but not recommended. data() is const char*
string result;
std::string result;
va_list ap;
va_start(ap,fmt);
result.reserve(200);
@ -156,16 +156,16 @@ string format(const char *fmt, ...)
}
// Absolutely identical to format above. This sucks...
string format1(const char *fmt, ...)
std::string format1(const char *fmt, ...)
{
va_list ap;
char buf[200];
va_start(ap,fmt);
int n = vsnprintf(buf,199,fmt,ap);
va_end(ap);
string result;
std::string result;
if (n <= 199) {
result = string(buf);
result = std::string(buf);
} else {
if (n > 5000) { LOG(ERR) << "oversized string in format"; n = 5000; }
// We could use vasprintf but we already computed the length...
@ -175,13 +175,13 @@ string format1(const char *fmt, ...)
vsnprintf(buffer,n+1,fmt,ap);
va_end(ap);
//if (n >= (2000-4)) { strcpy(&buf[(2000-4)],"..."); }
result = string(buffer);
result = std::string(buffer);
free(buffer);
}
return result;
}
int myscanf(const char *str, const char *fmt, string *s1)
int myscanf(const char *str, const char *fmt, std::string *s1)
{
int maxlen = strlen(str)+1;
char *a1 = (char*)alloca(maxlen);
@ -189,7 +189,7 @@ int myscanf(const char *str, const char *fmt, string *s1)
s1->assign(a1);
return n;
}
int myscanf(const char *str, const char *fmt, string *s1, string *s2)
int myscanf(const char *str, const char *fmt, std::string *s1, std::string *s2)
{
int maxlen = strlen(str)+1;
char *a1 = (char*)alloca(maxlen);
@ -201,7 +201,7 @@ int myscanf(const char *str, const char *fmt, string *s1, string *s2)
}
return n;
}
int myscanf(const char *str, const char *fmt, string *s1, string *s2, string *s3)
int myscanf(const char *str, const char *fmt, std::string *s1, std::string *s2, std::string *s3)
{
int maxlen = strlen(str)+1;
char *a1 = (char*)alloca(maxlen);
@ -215,7 +215,7 @@ int myscanf(const char *str, const char *fmt, string *s1, string *s2, string *s3
}
return n;
}
int myscanf(const char *str, const char *fmt, string *s1, string *s2, string *s3, string *s4)
int myscanf(const char *str, const char *fmt, std::string *s1, std::string *s2, std::string *s3, std::string *s4)
{
int maxlen = strlen(str)+1;
char *a1 = (char*)alloca(maxlen);
@ -233,25 +233,25 @@ int myscanf(const char *str, const char *fmt, string *s1, string *s2, string *s3
}
#if 0
string format(const char *fmt, string s1) {
std::string format(const char *fmt, std::string s1) {
return format(fmt,s1.c_str());
}
string format(const char *fmt, string s1, string s2) {
std::string format(const char *fmt, std::string s1, std::string s2) {
return format(fmt,s1.c_str(),s2.c_str());
}
string format(const char *fmt, string s1, string s2, string s3) {
std::string format(const char *fmt, std::string s1, std::string s2, std::string s3) {
return format(fmt,s1.c_str(),s2.c_str(),s3.c_str());
}
string format(const char *fmt, string s1, int i1) {
std::string format(const char *fmt, std::string s1, int i1) {
return format(fmt,s1.c_str(),i1);
}
string format(const char *fmt, int i1, string s1) {
std::string format(const char *fmt, int i1, std::string s1) {
return format(fmt,i1,s1.c_str());
}
string format(const char *fmt, string s1, string s2, int i1) {
std::string format(const char *fmt, std::string s1, std::string s2, int i1) {
return format(fmt,s1.c_str(),s2.c_str(),i1);
}
string format(const char *fmt, string s1, string s2, int i1, int i2) {
std::string format(const char *fmt, std::string s1, std::string s2, int i1, int i2) {
return format(fmt,s1.c_str(),s2.c_str(),i1,i2);
}
#endif
@ -265,14 +265,14 @@ double timef()
return tv.tv_usec / 1000000.0 + tv.tv_sec;
}
const string timestr(unsigned fieldwidth) // Use to pick the number of chars in the output.
const std::string timestr(unsigned fieldwidth) // Use to pick the number of chars in the output.
{
struct timeval tv;
struct tm tm;
gettimeofday(&tv,NULL);
localtime_r(&tv.tv_sec,&tm);
unsigned tenths = tv.tv_usec / 100000; // Rounding down is ok.
string result = format(" %02d:%02d:%02d.%1d",tm.tm_hour,tm.tm_min,tm.tm_sec,tenths);
std::string result = format(" %02d:%02d:%02d.%1d",tm.tm_hour,tm.tm_min,tm.tm_sec,tenths);
return result.substr(fieldwidth >= result.size() ? 0 : result.size() - fieldwidth);
//switch (maxfield) {
//case 'h': case 'H': return format("%02d:%02d:%02d.%1d",tm.tm_hour,tm.tm_min,tm.tm_sec,tenths);
@ -282,7 +282,7 @@ const string timestr(unsigned fieldwidth) // Use to pick the number of chars in
//}
}
const string timestr() { return timestr(12); }
const std::string timestr() { return timestr(12); }
// High resolution sleep for the specified time.
// Return FALSE if time is already past.
@ -301,16 +301,16 @@ void sleepf(double howlong)
//sleepf(sleeptime);
//}
string Text2Str::str() const
std::string Text2Str::str() const
{
ostringstream ss;
std::ostringstream ss;
text(ss);
return ss.str();
}
ostream& operator<<(std::ostream& os, const Text2Str *val)
std::ostream& operator<<(std::ostream& os, const Text2Str *val)
{
ostringstream ss;
std::ostringstream ss;
if (val) {
val->text(ss);
os << ss.str();
@ -379,7 +379,7 @@ char *cstrGetArg(const char *in, int nth, unsigned *length)
}
vector<string>& stringSplit(vector<string> &result,const char *input)
std::vector<std::string>& stringSplit(std::vector<std::string> &result,const char *input)
{
char *argv[40];
//char buf[202];
@ -387,18 +387,18 @@ vector<string>& stringSplit(vector<string> &result,const char *input)
char *buf = strdup(input);
int cnt = cstrSplit(buf,argv,40,NULL);
for (int i = 0; i < cnt; i++) {
result.push_back(string(argv[i]));
result.push_back(std::string(argv[i]));
}
free(buf);
return result;
}
// Print a table formatted as a vector of vector of strings.
// Print a table formatted as a std::vector of vector of strings.
// The columns will be aligned.
// Column size is determined from the columns.
// An entry of "_" is suppressed.
void printPrettyTable(prettyTable_t &tab, ostream&os, bool tabSeparated)
void printPrettyTable(prettyTable_t &tab, std::ostream&os, bool tabSeparated)
{
LOG(DEBUG);
const unsigned maxcols = 30;
@ -416,7 +416,7 @@ void printPrettyTable(prettyTable_t &tab, ostream&os, bool tabSeparated)
}
// Now print it.
for (unsigned nrow = 0; nrow < tab.size(); nrow++) {
vector<string> &row = tab[nrow];
std::vector<std::string> &row = tab[nrow];
// DEBUG: print the column widths.
if (0 && IS_LOG_LEVEL(DEBUG) && nrow == 0) {
@ -449,20 +449,20 @@ void printPrettyTable(prettyTable_t &tab, ostream&os, bool tabSeparated)
ostream& operator<<(std::ostream& os, const Statistic<int> &stat) { stat.text(os); return os; }
ostream& operator<<(std::ostream& os, const Statistic<unsigned> &stat) { stat.text(os); return os; }
ostream& operator<<(std::ostream& os, const Statistic<float> &stat) { stat.text(os); return os; }
ostream& operator<<(std::ostream& os, const Statistic<double> &stat) { stat.text(os); return os; }
std::ostream& operator<<(std::ostream& os, const Statistic<int> &stat) { stat.text(os); return os; }
std::ostream& operator<<(std::ostream& os, const Statistic<unsigned> &stat) { stat.text(os); return os; }
std::ostream& operator<<(std::ostream& os, const Statistic<float> &stat) { stat.text(os); return os; }
std::ostream& operator<<(std::ostream& os, const Statistic<double> &stat) { stat.text(os); return os; }
string replaceAll(const std::string input, const std::string search, const std::string replace)
std::string replaceAll(const std::string input, const std::string search, const std::string replace)
{
string output = input;
std::string output = input;
unsigned index1 = 0;
while (index1 < output.size()) {
try {
index1 = output.find(search, index1);
if (index1 == string::npos) {
if (index1 == std::string::npos) {
break;
}
@ -479,9 +479,9 @@ string replaceAll(const std::string input, const std::string search, const std::
}
// Efficient string concatenation.
string stringcat(string a, string b, string c, string d, string e, string f, string g)
std::string stringcat(std::string a, std::string b, std::string c, std::string d, std::string e, std::string f, std::string g)
{
string result;
std::string result;
result.reserve(a.size() + b.size() + c.size() + d.size() + e.size() + f.size() + g.size());
result.append(a);
result.append(b);
@ -493,58 +493,58 @@ string stringcat(string a, string b, string c, string d, string e, string f, str
return result;
}
static string emptystring("");
static std::string emptystring("");
string stringcat(string a, string b) {
std::string stringcat(std::string a, std::string b) {
return stringcat(a,b,emptystring,emptystring,emptystring,emptystring,emptystring);
}
string stringcat(string a, string b, string c) {
std::string stringcat(std::string a, std::string b, std::string c) {
return stringcat(a,b,c,emptystring,emptystring,emptystring,emptystring);
}
string stringcat(string a, string b, string c, string d) {
std::string stringcat(std::string a, std::string b, std::string c, std::string d) {
return stringcat(a,b,c,d,emptystring,emptystring,emptystring);
}
string stringcat(string a, string b, string c, string d, string e) {
std::string stringcat(std::string a, std::string b, std::string c, std::string d, std::string e) {
return stringcat(a,b,c,d,e,emptystring,emptystring);
}
string stringcat(string a, string b, string c, string d, string e, string f) {
std::string stringcat(std::string a, std::string b, std::string c, std::string d, std::string e, std::string f) {
return stringcat(a,b,c,d,e,f,emptystring);
}
void stringToUint(string strRAND, uint64_t *hRAND, uint64_t *lRAND)
void stringToUint(std::string strRAND, uint64_t *hRAND, uint64_t *lRAND)
{
assert(strRAND.size() == 32);
string strhRAND = strRAND.substr(0, 16);
string strlRAND = strRAND.substr(16, 16);
stringstream ssh;
ssh << hex << strhRAND;
std::string strhRAND = strRAND.substr(0, 16);
std::string strlRAND = strRAND.substr(16, 16);
std::stringstream ssh;
ssh << std::hex << strhRAND;
ssh >> *hRAND;
stringstream ssl;
ssl << hex << strlRAND;
std::stringstream ssl;
ssl << std::hex << strlRAND;
ssl >> *lRAND;
}
string uintToString(uint64_t h, uint64_t l)
std::string uintToString(uint64_t h, uint64_t l)
{
ostringstream os1;
std::ostringstream os1;
os1.width(16);
os1.fill('0');
os1 << hex << h;
ostringstream os2;
os1 << std::hex << h;
std::ostringstream os2;
os2.width(16);
os2.fill('0');
os2 << hex << l;
ostringstream os3;
os2 << std::hex << l;
std::ostringstream os3;
os3 << os1.str() << os2.str();
return os3.str();
}
string uintToString(uint32_t x)
std::string uintToString(uint32_t x)
{
ostringstream os;
std::ostringstream os;
os.width(8);
os.fill('0');
os << hex << x;
os << std::hex << x;
return os.str();
}
};

52
Utils.h
View File

@ -36,40 +36,40 @@ extern const std::string timestr(unsigned fieldwidth); // A timestamp to print
extern void sleepf(double howlong); // high resolution sleep
extern int gcd(int x, int y);
string format(const char *fmt, ...) __attribute__((format (printf,1,2)));
std::string format(const char *fmt, ...) __attribute__((format (printf,1,2)));
// format1 used to prevent C++ confusion over what function to call here.
string format1(const char *fmt, ...) __attribute__((format (printf,1,2)));
std::string format1(const char *fmt, ...) __attribute__((format (printf,1,2)));
// We have to enumerate the cross product of argument types here. This is fixed in C++11.
inline string format(const char *fmt, string s1) {
inline std::string format(const char *fmt, std::string s1) {
return format1(fmt,s1.c_str());
}
inline string format(const char *fmt, string s1, string s2) {
inline std::string format(const char *fmt, std::string s1, std::string s2) {
return format1(fmt,s1.c_str(),s2.c_str());
}
inline string format(const char *fmt, string s1, string s2, string s3) {
inline std::string format(const char *fmt, std::string s1, std::string s2, std::string s3) {
return format1(fmt,s1.c_str(),s2.c_str(),s3.c_str());
}
inline string format(const char *fmt, string s1, string s2, string s3, string s4) {
inline std::string format(const char *fmt, std::string s1, std::string s2, std::string s3, std::string s4) {
return format1(fmt,s1.c_str(),s2.c_str(),s3.c_str(),s4.c_str());
}
inline string format(const char *fmt, string s1, int i1) {
inline std::string format(const char *fmt, std::string s1, int i1) {
return format1(fmt,s1.c_str(),i1);
}
inline string format(const char *fmt, int i1, string s1) {
inline std::string format(const char *fmt, int i1, std::string s1) {
return format1(fmt,i1,s1.c_str());
}
inline string format(const char *fmt, string s1, string s2, int i1) {
inline std::string format(const char *fmt, std::string s1, std::string s2, int i1) {
return format1(fmt,s1.c_str(),s2.c_str(),i1);
}
inline string format(const char *fmt, string s1, string s2, int i1, int i2) {
inline std::string format(const char *fmt, std::string s1, std::string s2, int i1, int i2) {
return format1(fmt,s1.c_str(),s2.c_str(),i1,i2);
}
int myscanf(const char *str, const char *fmt, string *s1);
int myscanf(const char *str, const char *fmt, string *s1, string *s2);
int myscanf(const char *str, const char *fmt, string *s1, string *s2, string *s3);
int myscanf(const char *str, const char *fmt, string *s1, string *s2, string *s3, string *s4);
int myscanf(const char *str, const char *fmt, std::string *s1);
int myscanf(const char *str, const char *fmt, std::string *s1, std::string *s2);
int myscanf(const char *str, const char *fmt, std::string *s1, std::string *s2, std::string *s3);
int myscanf(const char *str, const char *fmt, std::string *s1, std::string *s2, std::string *s3, std::string *s4);
int cstrSplit(char *in, char **pargv,int maxargc, const char *splitchars=NULL);
char *cstrGetArg(const char *in, int nth, unsigned *length);
@ -178,21 +178,21 @@ std::ostream &osprintf(std::ostream &os, const char *fmt, ...) __attribute__((fo
std::string replaceAll(const std::string input, const std::string search, const std::string replace);
vector<string>& stringSplit(vector<string> &result,const char *input);
typedef vector<vector<string> > prettyTable_t;
void printPrettyTable(prettyTable_t &tab, ostream&os, bool tabSeparated = false);
std::vector<std::string>& stringSplit(std::vector<std::string> &result,const char *input);
typedef std::vector<std::vector<std::string> > prettyTable_t;
void printPrettyTable(prettyTable_t &tab, std::ostream&os, bool tabSeparated = false);
// The need for this is eliminated in C++11.
string stringcat(string a, string b);
string stringcat(string a, string b, string c);
string stringcat(string a, string b, string c, string d);
string stringcat(string a, string b, string c, string d, string e);
string stringcat(string a, string b, string c, string d, string e, string f);
string stringcat(string a, string b, string c, string d, string e, string f, string g);
std::string stringcat(std::string a, std::string b);
std::string stringcat(std::string a, std::string b, std::string c);
std::string stringcat(std::string a, std::string b, std::string c, std::string d);
std::string stringcat(std::string a, std::string b, std::string c, std::string d, std::string e);
std::string stringcat(std::string a, std::string b, std::string c, std::string d, std::string e, std::string f);
std::string stringcat(std::string a, std::string b, std::string c, std::string d, std::string e, std::string f, std::string g);
extern void stringToUint(string strRAND, uint64_t *hRAND, uint64_t *lRAND);
extern string uintToString(uint64_t h, uint64_t l);
extern string uintToString(uint32_t x);
extern void stringToUint(std::string strRAND, uint64_t *hRAND, uint64_t *lRAND);
extern std::string uintToString(uint64_t h, uint64_t l);
extern std::string uintToString(uint32_t x);
// The class is created with a RefCnt of 0. The caller must assign the constructed result to a pointer
// of type RefCntPointer. When the last RefCntPointer is freed, this struct is too.

View File

@ -119,13 +119,13 @@ sqlQuery::~sqlQuery()
if (mStmt) sqlite3_finalize(mStmt);
}
string sqlQuery::getResultText(int colNum)
std::string sqlQuery::getResultText(int colNum)
{
if (sqlSuccess()) {
const char* ptr = (const char*)sqlite3_column_text(mStmt,colNum);
return ptr ? string(ptr,sqlite3_column_bytes(mStmt,colNum)) : string("");
return ptr ? std::string(ptr,sqlite3_column_bytes(mStmt,colNum)) : std::string("");
}
return string("");
return std::string("");
}
sqlite3_int64 sqlQuery::getResultInt(int colNum)
@ -206,16 +206,16 @@ sqlite3_stmt *sqlite_lookup_row_u(sqlite3*db, const char *tableName, const char*
return sqlite_lookup_row(db,tableName,condition_u(conditionBuffer,keyName,keyValue),resultColumns);
}
// Pass a comma-separated list of column names to return, or if you want all the columns in the result, pass "*" as the resultColumns.
vector<string> sqlite_multi_lookup_vector(sqlite3* db, const char* tableName, const char* keyName, const char* keyData, const char *resultColumns)
std::vector<std::string> sqlite_multi_lookup_vector(sqlite3* db, const char* tableName, const char* keyName, const char* keyData, const char *resultColumns)
{
vector<string> result;
std::vector<std::string> result;
if (sqlite3_stmt *stmt = sqlite_lookup_row_c(db,tableName,keyName,keyData,resultColumns)) {
int n = sqlite3_column_count(stmt);
if (n < 0 || n > 100) { goto done; } // Would like to LOG an error but afraid to use LOG in here.
result.reserve(n+1);
for (int i = 0; i < n; i++) {
const char* ptr = (const char*)sqlite3_column_text(stmt,i);
result.push_back(ptr ? string(ptr,sqlite3_column_bytes(stmt,i)) : string(""));
result.push_back(ptr ? std::string(ptr,sqlite3_column_bytes(stmt,i)) : std::string(""));
}
done:
sqlite3_finalize(stmt);
@ -227,7 +227,7 @@ vector<string> sqlite_multi_lookup_vector(sqlite3* db, const char* tableName, co
bool sqlite_single_lookup(sqlite3* db, const char* tableName,
const char* keyName, const char* keyData,
const char* resultName, string &resultData)
const char* resultName, std::string &resultData)
{
sqlQuery query(db,tableName,resultName,keyName,keyData);
if (query.sqlSuccess()) {
@ -238,7 +238,7 @@ bool sqlite_single_lookup(sqlite3* db, const char* tableName,
#if 0
if (sqlite3_stmt *stmt = sqlite_lookup_row_c(db,tableName,keyName,keyData,valueName)) {
if (const char* ptr = (const char*)sqlite3_column_text(stmt,0)) {
valueData = string(ptr,sqlite3_column_bytes(stmt,0));
valueData = std::string(ptr,sqlite3_column_bytes(stmt,0));
}
sqlite3_finalize(stmt);
return true;
@ -249,7 +249,7 @@ bool sqlite_single_lookup(sqlite3* db, const char* tableName,
bool sqlite_single_lookup(sqlite3* db, const char* tableName,
const char* keyName, unsigned keyValue,
const char* resultName, string &resultData)
const char* resultName, std::string &resultData)
{
sqlQuery query(db,tableName,resultName,keyName,keyValue);
if (query.sqlSuccess()) {
@ -260,7 +260,7 @@ bool sqlite_single_lookup(sqlite3* db, const char* tableName,
#if 0
if (sqlite3_stmt *stmt = sqlite_lookup_row_u(db,tableName,keyName,keyValue,valueName)) {
if (const char* ptr = (const char*)sqlite3_column_text(stmt,0)) {
valueData = string(ptr,sqlite3_column_bytes(stmt,0));
valueData = std::string(ptr,sqlite3_column_bytes(stmt,0));
}
sqlite3_finalize(stmt);
return true;
@ -271,12 +271,12 @@ bool sqlite_single_lookup(sqlite3* db, const char* tableName,
// Do the lookup and just return the string.
// For this function an empty value is indistinguishable from failure - both return an empty string.
string sqlite_single_lookup_string(sqlite3* db, const char* tableName,
std::string sqlite_single_lookup_string(sqlite3* db, const char* tableName,
const char* keyName, const char* keyData, const char* resultName)
{
return sqlQuery(db,tableName,resultName,keyName,keyData).getResultText();
#if 0
string result;
std::string result;
(void) sqlite_single_lookup(db,tableName,keyName,keyData,valueName,result);
return result;
#endif
@ -349,7 +349,7 @@ bool sqlite_set_attr(sqlite3*db,const char *attr_name,const char*attr_value)
return true;
}
string sqlite_get_attr(sqlite3*db,const char *attr_name)
std::string sqlite_get_attr(sqlite3*db,const char *attr_name)
{
return sqlite_single_lookup_string(db,"ATTR_TABLE","ATTR_NAME",attr_name,"ATTR_VALUE");
}