Moved some more functions from CUtils into CString

git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@246 726aef4b-f618-498e-8847-2d620e286838
This commit is contained in:
prozacx
2005-05-07 11:42:10 +00:00
parent 11ae1b690f
commit 078bbcf019
9 changed files with 200 additions and 232 deletions

View File

@@ -7,7 +7,7 @@ CChan::CChan(const CString& sName, CUser* pUser) {
m_sName = sName.Token(0);
m_sKey = sName.Token(1);
if (CUtils::Left(m_sName, 1) != "#" && CUtils::Left(m_sName, 1) != "&") {
if (m_sName.Left(1) != "#" && m_sName.Left(1) != "&") {
m_sName = "#" + m_sName;
}

View File

@@ -3,8 +3,8 @@
void CDCCBounce::ReadLine(const CString& sData) {
CString sLine = sData;
while ((CUtils::Right(sLine, 1) == "\r") || (CUtils::Right(sLine, 1) == "\n")) {
CUtils::RightChomp(sLine);
while ((sLine.Right(1) == "\r") || (sLine.Right(1) == "\n")) {
sLine.RightChomp();
}
DEBUG_ONLY(cout << GetSockName() << " <- [" << sLine << "]" << endl);

View File

@@ -7,8 +7,8 @@ CFile::CFile(const CString& sLongName) {
m_sShortName = sLongName;
// @todo shouldn't this be Right() and RightChomp() ?!
while (CUtils::Left(m_sShortName, 1) == "/") {
CUtils::LeftChomp(m_sShortName);
while (m_sShortName.Left(1) == "/") {
m_sShortName.LeftChomp();
}
CString::size_type uPos = m_sShortName.rfind('/');

View File

@@ -45,8 +45,8 @@ CIRCSock::~CIRCSock() {
void CIRCSock::ReadLine(const CString& sData) {
CString sLine = sData;
while ((CUtils::Right(sLine, 1) == "\r") || (CUtils::Right(sLine, 1) == "\n")) {
CUtils::RightChomp(sLine);
while ((sLine.Right(1) == "\r") || (sLine.Right(1) == "\n")) {
sLine.RightChomp();
}
DEBUG_ONLY(cout << GetSockName() << " <- [" << sLine << "]" << endl);
@@ -63,7 +63,7 @@ void CIRCSock::ReadLine(const CString& sData) {
CString sCmd = sLine.Token(1);
if ((sCmd.length() == 3) && (isdigit(sCmd[0])) && (isdigit(sCmd[1])) && (isdigit(sCmd[2]))) {
CString sServer = sLine.Token(0); CUtils::LeftChomp(sServer);
CString sServer = sLine.Token(0); sServer.LeftChomp();
unsigned int uRaw = strtoul(sCmd.c_str(), NULL, 10);
CString sNick = sLine.Token(2);
CString sRest = sLine.Token(3, true);
@@ -147,29 +147,29 @@ void CIRCSock::ReadLine(const CString& sData) {
if ((!sAltNick.empty()) && (strcasecmp(sConfNick.c_str(), sAltNick.c_str()) != 0)) {
PutServ("NICK " + sAltNick);
} else {
PutServ("NICK " + CUtils::Left(sConfNick, 8) + "-");
PutServ("NICK " + sConfNick.Left(8) + "-");
}
} else if (strcasecmp(sBadNick.c_str(), sAltNick.c_str()) == 0) {
PutServ("NICK " + CUtils::Left(sConfNick, 8) + "-");
} else if (strcasecmp(sBadNick.c_str(), CString(CUtils::Left(sConfNick, 8) + "-").c_str()) == 0) {
PutServ("NICK " + CUtils::Left(sConfNick, 8) + "|");
} else if (strcasecmp(sBadNick.c_str(), CString(CUtils::Left(sConfNick, 8) + "|").c_str()) == 0) {
PutServ("NICK " + CUtils::Left(sConfNick, 8) + "^");
PutServ("NICK " + sConfNick.Left(8) + "-");
} else if (strcasecmp(sBadNick.c_str(), CString(sConfNick.Left(8) + "-").c_str()) == 0) {
PutServ("NICK " + sConfNick.Left(8) + "|");
} else if (strcasecmp(sBadNick.c_str(), CString(sConfNick.Left(8) + "|").c_str()) == 0) {
PutServ("NICK " + sConfNick.Left(8) + "^");
} else {
char cLetter = 0;
if (sBadNick.empty()) {
Close();
return;
Close();
return;
}
cLetter = CUtils::Right(sBadNick, 1)[0];
cLetter = sBadNick.Right(1)[0];
if (cLetter == 'z') {
Close();
return;
}
CString sSend = "NICK " + CUtils::Left(sConfNick, 8) + cLetter++;
CString sSend = "NICK " + sConfNick.Left(8) + cLetter++;
PutServ(sSend);
}
@@ -212,7 +212,7 @@ void CIRCSock::ReadLine(const CString& sData) {
if (pChan) {
CString sTopic = sLine.Token(4, true);
CUtils::LeftChomp(sTopic);
sTopic.LeftChomp();
pChan->SetTopic(sTopic);
}
@@ -239,7 +239,7 @@ void CIRCSock::ReadLine(const CString& sData) {
CString sIdent = sLine.Token(4);
CString sHost = sLine.Token(5);
CUtils::LeftChomp(sServer);
sServer.LeftChomp();
if (strcasecmp(sNick.c_str(), GetNick().c_str()) == 0) {
m_Nick.SetIdent(sIdent);
@@ -272,8 +272,8 @@ void CIRCSock::ReadLine(const CString& sData) {
CChan* pChan = m_pUser->FindChan(sRest.Token(1));
if (pChan) {
CString sNicks = sRest.Token(2, true);
if (CUtils::Left(sNicks, 1) == ":") {
CUtils::LeftChomp(sNicks);
if (sNicks.Left(1) == ":") {
sNicks.LeftChomp();
}
pChan->AddNicks(sNicks);
@@ -329,7 +329,7 @@ void CIRCSock::ReadLine(const CString& sData) {
}
} else { //if (CUtils::wildcmp(":*!*@* * *", sLine.c_str())) {
CString sNickMask = sLine.Token(0);
CUtils::LeftChomp(sNickMask);
sNickMask.LeftChomp();
CString sNick = sNickMask.Token(0, false, '!');
CString sCmd = sLine.Token(1);
@@ -339,8 +339,8 @@ void CIRCSock::ReadLine(const CString& sData) {
CString sNewNick = sRest;
bool bIsVisible = false;
if (CUtils::Left(sNewNick, 1) == ":") {
CUtils::LeftChomp(sNewNick);
if (sNewNick.Left(1) == ":") {
sNewNick.LeftChomp();
}
vector<CChan*> vFoundChans;
@@ -378,8 +378,8 @@ void CIRCSock::ReadLine(const CString& sData) {
CString sMessage = sRest;
bool bIsVisible = false;
if (CUtils::Left(sMessage, 1) == ":") {
CUtils::LeftChomp(sMessage);
if (sMessage.Left(1) == ":") {
sMessage.LeftChomp();
}
// :nick!ident@host.com QUIT :message
@@ -413,8 +413,8 @@ void CIRCSock::ReadLine(const CString& sData) {
}
} else if (strcasecmp(sCmd.c_str(), "JOIN") == 0) {
CString sChan = sRest.Token(0);
if (CUtils::Left(sChan, 1) == ":") {
CUtils::LeftChomp(sChan);
if (sChan.Left(1) == ":") {
sChan.LeftChomp();
}
if (strcasecmp(sNick.c_str(), GetNick().c_str()) == 0) {
@@ -433,8 +433,8 @@ void CIRCSock::ReadLine(const CString& sData) {
}
} else if (strcasecmp(sCmd.c_str(), "PART") == 0) {
CString sChan = sRest.Token(0);
if (CUtils::Left(sChan, 1) == ":") {
CUtils::LeftChomp(sChan);
if (sChan.Left(1) == ":") {
sChan.LeftChomp();
}
CChan* pChan = m_pUser->FindChan(sChan);
@@ -469,7 +469,7 @@ void CIRCSock::ReadLine(const CString& sData) {
CString sChan = sRest.Token(0);
CString sKickedNick = sRest.Token(1);
CString sMsg = sRest.Token(2, true);
CUtils::LeftChomp(sMsg);
sMsg.LeftChomp();
CChan* pChan = m_pUser->FindChan(sChan);
@@ -501,11 +501,11 @@ void CIRCSock::ReadLine(const CString& sData) {
CString sTarget = sRest.Token(0);
CString sMsg = sRest.Token(1, true);
CUtils::LeftChomp(sMsg);
sMsg.LeftChomp();
if (CUtils::wildcmp("\001*\001", sMsg.c_str())) {
CUtils::LeftChomp(sMsg);
CUtils::RightChomp(sMsg);
sMsg.LeftChomp();
sMsg.RightChomp();
if (strcasecmp(sTarget.c_str(), GetNick().c_str()) == 0) {
if (OnCTCPReply(sNickMask, sMsg)) {
@@ -536,7 +536,7 @@ void CIRCSock::ReadLine(const CString& sData) {
if (pChan) {
CNick Nick(sNickMask);
CString sTopic = sLine.Token(3, true);
CUtils::LeftChomp(sTopic);
sTopic.LeftChomp();
pChan->SetTopicOwner(Nick.GetNick());
pChan->SetTopicDate((unsigned long) time(NULL)); // @todo use local time
pChan->SetTopic(sTopic);
@@ -548,13 +548,13 @@ void CIRCSock::ReadLine(const CString& sData) {
CString sTarget = sRest.Token(0);
CString sMsg = sRest.Token(1, true);
if (CUtils::Left(sMsg, 1) == ":") {
CUtils::LeftChomp(sMsg);
if (sMsg.Left(1) == ":") {
sMsg.LeftChomp();
}
if (CUtils::wildcmp("\001*\001", sMsg.c_str())) {
CUtils::LeftChomp(sMsg);
CUtils::RightChomp(sMsg);
sMsg.LeftChomp();
sMsg.RightChomp();
if (strcasecmp(sTarget.c_str(), GetNick().c_str()) == 0) {
if (OnPrivCTCP(sNickMask, sMsg)) {

View File

@@ -199,9 +199,9 @@ bool CUser::AddServer(const CString& sName) {
CString sHost = sLine.Token(0);
CString sPort = sLine.Token(1);
if (CUtils::Left(sPort, 1) == "+") {
if (sPort.Left(1) == "+") {
bSSL = true;
CUtils::LeftChomp(sPort);
sPort.LeftChomp();
}
unsigned short uPort = strtoul(sPort.c_str(), NULL, 10);

View File

@@ -10,8 +10,8 @@
void CUserSock::ReadLine(const CString& sData) {
CString sLine = sData;
while ((CUtils::Right(sLine, 1) == "\r") || (CUtils::Right(sLine, 1) == "\n")) {
CUtils::RightChomp(sLine);
while ((sLine.Right(1) == "\r") || (sLine.Right(1) == "\n")) {
sLine.RightChomp();
}
DEBUG_ONLY(cout << GetSockName() << " <- [" << sLine << "]" << endl);
@@ -68,8 +68,8 @@ void CUserSock::ReadLine(const CString& sData) {
return; // Don't forward this msg. ZNC has already registered us.
} else if (strcasecmp(sCommand.c_str(), "NICK") == 0) {
CString sNick = sLine.Token(1);
if (CUtils::Left(sNick, 1) == ":") {
CUtils::LeftChomp(sNick);
if (sNick.Left(1) == ":") {
sNick.LeftChomp();
}
if (!m_bAuthed) {
@@ -99,8 +99,8 @@ void CUserSock::ReadLine(const CString& sData) {
return; // Don't forward this msg. ZNC has already registered us.
} else if (strcasecmp(sCommand.c_str(), "JOIN") == 0) {
CString sChan = sLine.Token(1);
if (CUtils::Left(sChan, 1) == ":") {
CUtils::LeftChomp(sChan);
if (sChan.Left(1) == ":") {
sChan.LeftChomp();
}
if (m_pUser) {
@@ -122,8 +122,8 @@ void CUserSock::ReadLine(const CString& sData) {
CString sTarget = sLine.Token(1);
CString sMsg = sLine.Token(2, true);
if (CUtils::Left(sMsg, 1) == ":") {
CUtils::LeftChomp(sMsg);
if (sMsg.Left(1) == ":") {
sMsg.LeftChomp();
}
if ((!m_pUser) || (strcasecmp(sTarget.c_str(), CString(m_pUser->GetStatusPrefix() + "status").c_str())) == 0) {
@@ -134,7 +134,7 @@ void CUserSock::ReadLine(const CString& sData) {
#ifdef _MODULES
if (m_pUser) {
CString sModule = sTarget;
CUtils::LeftChomp(sModule, m_pUser->GetStatusPrefix().length());
sModule.LeftChomp(m_pUser->GetStatusPrefix().length());
CModule* pModule = m_pUser->GetModules().FindModule(sModule);
if (pModule) {
@@ -160,8 +160,8 @@ void CUserSock::ReadLine(const CString& sData) {
#ifdef _MODULES
if (CUtils::wildcmp("\001*\001", sMsg.c_str())) {
CString sCTCP = sMsg;
CUtils::LeftChomp(sCTCP);
CUtils::RightChomp(sCTCP);
sCTCP.LeftChomp();
sCTCP.RightChomp();
if ((m_pUser) && (m_pUser->GetModules().OnUserCTCPReply(sTarget, sCTCP))) {
return;
@@ -181,14 +181,14 @@ void CUserSock::ReadLine(const CString& sData) {
CString sTarget = sLine.Token(1);
CString sMsg = sLine.Token(2, true);
if (CUtils::Left(sMsg, 1) == ":") {
CUtils::LeftChomp(sMsg);
if (sMsg.Left(1) == ":") {
sMsg.LeftChomp();
}
if (CUtils::wildcmp("\001*\001", sMsg.c_str())) {
CString sCTCP = sMsg;
CUtils::LeftChomp(sCTCP);
CUtils::RightChomp(sCTCP);
sCTCP.LeftChomp();
sCTCP.RightChomp();
if (strncasecmp(sCTCP.c_str(), "DCC ", 4) == 0) {
CString sType = sCTCP.Token(1);
@@ -288,7 +288,7 @@ void CUserSock::ReadLine(const CString& sData) {
if (strncasecmp(sTarget.c_str(), m_pUser->GetStatusPrefix().c_str(), m_pUser->GetStatusPrefix().length()) == 0) {
#ifdef _MODULES
CString sModule = sTarget;
CUtils::LeftChomp(sModule, m_pUser->GetStatusPrefix().length());
sModule.LeftChomp(m_pUser->GetStatusPrefix().length());
CModule* pModule = m_pUser->GetModules().FindModule(sModule);
if (pModule) {
@@ -324,7 +324,7 @@ void CUserSock::ReadLine(const CString& sData) {
#ifdef _MODULES
if (m_pUser) {
CString sModule = sTarget;
CUtils::LeftChomp(sModule, m_pUser->GetStatusPrefix().length());
sModule.LeftChomp(m_pUser->GetStatusPrefix().length());
CModule* pModule = m_pUser->GetModules().FindModule(sModule);
if (pModule) {

170
Utils.cpp
View File

@@ -63,7 +63,7 @@ void CUtils::GenerateCert(FILE *pOut, bool bEncPrivKey) {
pHostName = "unknown.com";
}
string sEmailAddr = pLogName;
CString sEmailAddr = pLogName;
sEmailAddr += "@";
sEmailAddr += pHostName;
@@ -88,7 +88,7 @@ void CUtils::GenerateCert(FILE *pOut, bool bEncPrivKey) {
};
#endif /* HAVE_LIBSSL */
string CUtils::GetIP(unsigned long addr) {
CString CUtils::GetIP(unsigned long addr) {
char szBuf[16];
memset((char*) szBuf, 0, 16);
@@ -104,7 +104,7 @@ string CUtils::GetIP(unsigned long addr) {
return szBuf;
}
unsigned long CUtils::GetLongIP(const string& sIP) {
unsigned long CUtils::GetLongIP(const CString& sIP) {
register int i;
char *addr = (char *) malloc(sIP.length() +1);
char ip[4][4], n;
@@ -129,8 +129,8 @@ CString CUtils::ChangeDir(const CString& sPath, const CString& sAdd, const CStri
CString sAddDir = sAdd;
if (CUtils::Left(sAddDir, 2) == "~/") {
CUtils::LeftChomp(sAddDir);
if (sAddDir.Left(2) == "~/") {
sAddDir.LeftChomp();
sAddDir = sHomeDir + sAddDir;
}
@@ -138,8 +138,8 @@ CString CUtils::ChangeDir(const CString& sPath, const CString& sAdd, const CStri
sAddDir += "/";
CString sCurDir;
if (CUtils::Right(sRet, 1) == "/") {
CUtils::RightChomp(sRet);
if (sRet.Right(1) == "/") {
sRet.RightChomp();
}
for (unsigned int a = 0; a < sAddDir.size(); a++) {
@@ -162,11 +162,11 @@ CString CUtils::ChangeDir(const CString& sPath, const CString& sAdd, const CStri
return (sRet.empty()) ? "/" : sRet;
}
int CUtils::MakeDir(const string& sPath, mode_t iMode) {
string sDir = sPath;
string::size_type iFind = sDir.find("/");
int CUtils::MakeDir(const CString& sPath, mode_t iMode) {
CString sDir = sPath;
CString::size_type iFind = sDir.find("/");
if (iFind == string::npos) {
if (iFind == CString::npos) {
return mkdir(sDir.c_str(), iMode);
}
iFind++;
@@ -179,8 +179,8 @@ int CUtils::MakeDir(const string& sPath, mode_t iMode) {
return mkdir(sDir.c_str(), iMode);
}
string sWorkDir = sDir.substr(0, iFind); // include the trailing slash
string sNewDir = sDir.erase(0, iFind);
CString sWorkDir = sDir.substr(0, iFind); // include the trailing slash
CString sNewDir = sDir.erase(0, iFind);
struct stat st;
@@ -220,7 +220,7 @@ int CUtils::MakeDir(const string& sPath, mode_t iMode) {
return -1;
}
string CUtils::GetHashPass() {
CString CUtils::GetHashPass() {
while (true) {
char* pass = CUtils::GetPass("Enter Password");
char* pass1 = (char*) malloc(strlen(pass) +1);
@@ -234,7 +234,7 @@ string CUtils::GetHashPass() {
} else if (!iLen) {
CUtils::PrintError("You can not use an empty password");
} else {
string sRet((const char*) CMD5(pass1, iLen));
CString sRet((const char*) CMD5(pass1, iLen));
memset((char*) pass1, 0, iLen); // null out our pass so it doesn't sit in memory
memset((char*) pass2, 0, strlen(pass2)); // null out our pass so it doesn't sit in memory
free(pass1);
@@ -250,17 +250,17 @@ string CUtils::GetHashPass() {
return "";
}
char* CUtils::GetPass(const string& sPrompt) {
char* CUtils::GetPass(const CString& sPrompt) {
PrintPrompt(sPrompt);
return getpass("");
}
bool CUtils::GetBoolInput(const string& sPrompt, bool bDefault) {
bool CUtils::GetBoolInput(const CString& sPrompt, bool bDefault) {
return CUtils::GetBoolInput(sPrompt, &bDefault);
}
bool CUtils::GetBoolInput(const string& sPrompt, bool *pbDefault) {
string sRet, sDefault;
bool CUtils::GetBoolInput(const CString& sPrompt, bool *pbDefault) {
CString sRet, sDefault;
if (pbDefault) {
sDefault = (*pbDefault) ? "yes" : "no";
@@ -277,13 +277,13 @@ bool CUtils::GetBoolInput(const string& sPrompt, bool *pbDefault) {
return GetBoolInput(sPrompt, pbDefault);
}
bool CUtils::GetNumInput(const string& sPrompt, unsigned int& uRet, unsigned int uMin, unsigned int uMax, unsigned int uDefault) {
bool CUtils::GetNumInput(const CString& sPrompt, unsigned int& uRet, unsigned int uMin, unsigned int uMax, unsigned int uDefault) {
if (uMin > uMax) {
return false;
}
string sDefault = (uDefault != (unsigned int) ~0) ? CUtils::ToString(uDefault) : "";
string sNum, sHint;
CString sDefault = (uDefault != (unsigned int) ~0) ? CUtils::ToString(uDefault) : "";
CString sNum, sHint;
if (uMax != (unsigned int) ~0) {
sHint = CUtils::ToString(uMin) + " to " + CUtils::ToString(uMax);
@@ -309,8 +309,8 @@ bool CUtils::GetNumInput(const string& sPrompt, unsigned int& uRet, unsigned int
return true;
}
bool CUtils::GetInput(const string& sPrompt, string& sRet, const string& sDefault, const string& sHint) {
string sExtra;
bool CUtils::GetInput(const CString& sPrompt, CString& sRet, const CString& sDefault, const CString& sHint) {
CString sExtra;
sExtra += (!sHint.empty()) ? (" (" + sHint + ")") : "";
sExtra += (!sDefault.empty()) ? (" [" + sDefault + "]") : "";
@@ -320,8 +320,8 @@ bool CUtils::GetInput(const string& sPrompt, string& sRet, const string& sDefaul
fgets(szBuf, 1024, stdin);
sRet = szBuf;
if (CUtils::Right(sRet, 1) == "\n") {
CUtils::RightChomp(sRet);
if (sRet.Right(1) == "\n") {
sRet.RightChomp();
}
if (sRet.empty()) {
@@ -331,15 +331,15 @@ bool CUtils::GetInput(const string& sPrompt, string& sRet, const string& sDefaul
return !sRet.empty();
}
void CUtils::PrintError(const string& sMessage) {
void CUtils::PrintError(const CString& sMessage) {
fprintf(stdout, "\033[1m\033[34m[\033[31m ** \033[34m]\033[39m\033[22m %s\n", sMessage.c_str());
}
void CUtils::PrintPrompt(const string& sMessage) {
void CUtils::PrintPrompt(const CString& sMessage) {
fprintf(stdout, "\033[1m\033[34m[\033[33m ?? \033[34m]\033[39m\033[22m %s: ", sMessage.c_str());
}
void CUtils::PrintMessage(const string& sMessage, bool bStrong) {
void CUtils::PrintMessage(const CString& sMessage, bool bStrong) {
fprintf(stdout, "\033[1m\033[34m[\033[33m ** \033[34m]\033[39m\033[22m %s%s%s\n",
((bStrong) ? "\033[1m" : ""),
sMessage.c_str(),
@@ -347,12 +347,12 @@ void CUtils::PrintMessage(const string& sMessage, bool bStrong) {
);
}
void CUtils::PrintAction(const string& sMessage) {
void CUtils::PrintAction(const CString& sMessage) {
fprintf(stdout, "\033[1m\033[34m[\033[32m \033[34m]\033[39m\033[22m %s... ", sMessage.c_str());
fflush(stdout);
}
void CUtils::PrintStatus(bool bSuccess, const string& sMessage) {
void CUtils::PrintStatus(bool bSuccess, const CString& sMessage) {
if (!sMessage.empty()) {
if (bSuccess) {
fprintf(stdout, "%s", sMessage.c_str());
@@ -370,94 +370,68 @@ void CUtils::PrintStatus(bool bSuccess, const string& sMessage) {
}
}
string CUtils::ToString(short i) { stringstream s; s << i; return s.str(); }
string CUtils::ToString(unsigned short i) { stringstream s; s << i; return s.str(); }
string CUtils::ToString(int i) { stringstream s; s << i; return s.str(); }
string CUtils::ToString(unsigned int i) { stringstream s; s << i; return s.str(); }
string CUtils::ToString(long i) { stringstream s; s << i; return s.str(); }
string CUtils::ToString(unsigned long i) { stringstream s; s << i; return s.str(); }
string CUtils::ToString(unsigned long long i) { stringstream s; s << i; return s.str(); }
string CUtils::ToString(double i) { stringstream s; s << i; return s.str(); }
string CUtils::ToString(float i) { stringstream s; s << i; return s.str(); }
CString CUtils::ToString(short i) { stringstream s; s << i; return s.str(); }
CString CUtils::ToString(unsigned short i) { stringstream s; s << i; return s.str(); }
CString CUtils::ToString(int i) { stringstream s; s << i; return s.str(); }
CString CUtils::ToString(unsigned int i) { stringstream s; s << i; return s.str(); }
CString CUtils::ToString(long i) { stringstream s; s << i; return s.str(); }
CString CUtils::ToString(unsigned long i) { stringstream s; s << i; return s.str(); }
CString CUtils::ToString(unsigned long long i) { stringstream s; s << i; return s.str(); }
CString CUtils::ToString(double i) { stringstream s; s << i; return s.str(); }
CString CUtils::ToString(float i) { stringstream s; s << i; return s.str(); }
string CUtils::ToPercent(double d) {
CString CUtils::ToPercent(double d) {
char szRet[32];
snprintf(szRet, 32, "%.02f%%", d);
return szRet;
}
string CUtils::ToKBytes(double d) {
CString CUtils::ToKBytes(double d) {
char szRet[32];
snprintf(szRet, 32, "%.0f K/s", d);
return szRet;
}
string CUtils::Left(const string& s, unsigned int u) {
u = (u > s.length()) ? s.length() : u;
return s.substr(0, u);
}
string CUtils::Right(const string& s, unsigned int u) {
u = (u > s.length()) ? s.length() : u;
return s.substr(s.length() - u, u);
}
string& CUtils::Trim(string& s) {
while ((Right(s, 1) == " ") || (Right(s, 1) == "\t") || (Right(s, 1) == "\r") || (Right(s, 1) == "\n")) {
RightChomp(s);
CString& CUtils::Trim(CString& s) {
while ((s.Right(1) == " ") || (s.Right(1) == "\t") || (s.Right(1) == "\r") || (s.Right(1) == "\n")) {
s.RightChomp();
}
while ((Left(s, 1) == " ") || (Left(s, 1) == "\t") || (Left(s, 1) == "\r") || (Left(s, 1) == "\n")) {
LeftChomp(s);
while ((s.Left(1) == " ") || (s.Left(1) == "\t") || (s.Left(1) == "\r") || (s.Left(1) == "\n")) {
s.LeftChomp();
}
return s;
}
string& CUtils::LeftChomp(string& s, unsigned int uLen) {
while ((uLen--) && (s.length())) {
s.erase(0, 1);
}
return s;
}
string& CUtils::RightChomp(string& s, unsigned int uLen) {
while ((uLen--) && (s.length())) {
s.erase(s.length() -1);
}
return s;
}
bool CUtils::wildcmp(const string& sWild, const string& sString) {
bool CUtils::wildcmp(const CString& sWild, const CString& sString) {
// Written by Jack Handy - jakkhandy@hotmail.com
const char *wild = sWild.c_str(), *string = sString.c_str();
const char *wild = sWild.c_str(), *CString = sString.c_str();
const char *cp = NULL, *mp = NULL;
while ((*string) && (*wild != '*')) {
if ((*wild != *string) && (*wild != '?')) {
while ((*CString) && (*wild != '*')) {
if ((*wild != *CString) && (*wild != '?')) {
return false;
}
wild++;
string++;
CString++;
}
while (*string) {
while (*CString) {
if (*wild == '*') {
if (!*++wild) {
return true;
}
mp = wild;
cp = string+1;
} else if ((*wild == *string) || (*wild == '?')) {
cp = CString+1;
} else if ((*wild == *CString) || (*wild == '?')) {
wild++;
string++;
CString++;
} else {
wild = mp;
string = cp++;
CString = cp++;
}
}
@@ -477,7 +451,7 @@ CTable::~CTable() {
clear();
}
bool CTable::AddColumn(const string& sName) {
bool CTable::AddColumn(const CString& sName) {
for (unsigned int a = 0; a < m_vsHeaders.size(); a++) {
if (strcasecmp(m_vsHeaders[a].c_str(), sName.c_str()) == 0) {
return false;
@@ -489,11 +463,11 @@ bool CTable::AddColumn(const string& sName) {
}
unsigned int CTable::AddRow() {
push_back(new map<string, string>);
push_back(new map<CString, CString>);
return size() -1;
}
bool CTable::SetCell(const string& sColumn, const string& sValue, unsigned int uRowIdx) {
bool CTable::SetCell(const CString& sColumn, const CString& sValue, unsigned int uRowIdx) {
if (uRowIdx == (unsigned int) ~0) {
if (!size()) {
return false;
@@ -506,7 +480,7 @@ bool CTable::SetCell(const string& sColumn, const string& sValue, unsigned int u
return true;
}
bool CTable::GetLine(unsigned int uIdx, string& sLine) {
bool CTable::GetLine(unsigned int uIdx, CString& sLine) {
stringstream ssRet;
if (!size()) {
@@ -542,7 +516,7 @@ bool CTable::GetLine(unsigned int uIdx, string& sLine) {
uIdx -= 3;
if (uIdx < size()) {
map<string, string>* pRow = (*this)[uIdx];
map<CString, CString>* pRow = (*this)[uIdx];
ssRet.fill(' ');
ssRet << "| ";
@@ -569,7 +543,7 @@ bool CTable::Output(std::ostream oOut) {
oOut << endl << ssSep.str() << endl;
for (unsigned int b = 0; b < size(); b++) {
map<string, string>* pRow = (*this)[b];
map<CString, CString>* pRow = (*this)[b];
oOut << " | ";
@@ -592,16 +566,16 @@ unsigned int CTable::GetColumnWidth(unsigned int uIdx) {
return 0;
}
const string& sColName = m_vsHeaders[uIdx];
const CString& sColName = m_vsHeaders[uIdx];
unsigned int uRet = sColName.size();
map<string, unsigned int>::iterator it = m_msuWidths.find(sColName);
map<CString, unsigned int>::iterator it = m_msuWidths.find(sColName);
if (it != m_msuWidths.end()) {
return it->second;
}
for (unsigned int a = 0; a < size(); a++) {
map<string, string>* pRow = (*this)[a];
map<CString, CString>* pRow = (*this)[a];
uRet = uRet >? (*pRow)[m_vsHeaders[uIdx]].size();
}
@@ -610,7 +584,7 @@ unsigned int CTable::GetColumnWidth(unsigned int uIdx) {
#ifdef HAVE_LIBSSL
CBlowfish::CBlowfish(const string & sPassword, int iEncrypt, const string & sIvec) {
CBlowfish::CBlowfish(const CString & sPassword, int iEncrypt, const CString & sIvec) {
m_iEncrypt = iEncrypt;
m_ivec = (unsigned char *)calloc(sizeof(unsigned char), 8);
m_num = 0;
@@ -633,9 +607,9 @@ unsigned char *CBlowfish::MD5(const unsigned char *input, u_int ilen) {
return output;
}
//! returns an md5 of the string (not hex encoded)
string CBlowfish::MD5(const string & sInput, bool bHexEncode) {
string sRet;
//! returns an md5 of the CString (not hex encoded)
CString CBlowfish::MD5(const CString & sInput, bool bHexEncode) {
CString sRet;
unsigned char *data = MD5((const unsigned char *)sInput.data(), sInput.length());
if (!bHexEncode) {
@@ -663,9 +637,9 @@ unsigned char * CBlowfish::Crypt(unsigned char *input, u_int ibytes) {
return buff;
}
string CBlowfish::Crypt(const string & sData) {
CString CBlowfish::Crypt(const CString & sData) {
unsigned char *buff = Crypt((unsigned char *)sData.data(), sData.length());
string sOutput;
CString sOutput;
sOutput.append((const char *)buff, sData.length());
free(buff);
return sOutput;

106
Utils.h
View File

@@ -8,10 +8,8 @@
#include <stdio.h>
#include "String.h"
#include <string>
#include <vector>
#include <map>
using std::string;
using std::vector;
using std::map;
@@ -28,40 +26,36 @@ public:
CUtils();
virtual ~CUtils();
static string GetIP(unsigned long addr);
static unsigned long GetLongIP(const string& sIP);
static CString GetIP(unsigned long addr);
static unsigned long GetLongIP(const CString& sIP);
static CString ChangeDir(const CString& sPath, const CString& sAdd, const CString& sHomeDir);
static int MakeDir(const string& sPath, mode_t iMode = 0700);
static void PrintError(const string& sMessage);
static void PrintMessage(const string& sMessage, bool bStrong = false);
static void PrintPrompt(const string& sMessage);
static void PrintAction(const string& sMessage);
static void PrintStatus(bool bSuccess, const string& sMessage = "");
static string GetHashPass();
static char* GetPass(const string& sPrompt);
static bool GetInput(const string& sPrompt, string& sRet, const string& sDefault = "", const string& sHint = "");
static bool GetBoolInput(const string& sPrompt, bool bDefault);
static bool GetBoolInput(const string& sPrompt, bool *pbDefault = NULL);
static bool GetNumInput(const string& sPrompt, unsigned int& uRet, unsigned int uMin = 0, unsigned int uMax = ~0, unsigned int uDefault = ~0);
static int MakeDir(const CString& sPath, mode_t iMode = 0700);
static void PrintError(const CString& sMessage);
static void PrintMessage(const CString& sMessage, bool bStrong = false);
static void PrintPrompt(const CString& sMessage);
static void PrintAction(const CString& sMessage);
static void PrintStatus(bool bSuccess, const CString& sMessage = "");
static CString GetHashPass();
static char* GetPass(const CString& sPrompt);
static bool GetInput(const CString& sPrompt, CString& sRet, const CString& sDefault = "", const CString& sHint = "");
static bool GetBoolInput(const CString& sPrompt, bool bDefault);
static bool GetBoolInput(const CString& sPrompt, bool *pbDefault = NULL);
static bool GetNumInput(const CString& sPrompt, unsigned int& uRet, unsigned int uMin = 0, unsigned int uMax = ~0, unsigned int uDefault = ~0);
static string ToString(short i);
static string ToString(unsigned short i);
static string ToString(int i);
static string ToString(unsigned int i);
static string ToString(long i);
static string ToString(unsigned long i);
static string ToString(unsigned long long i);
static string ToString(double i);
static string ToString(float i);
static string ToPercent(double d);
static string ToKBytes(double d);
static CString ToString(short i);
static CString ToString(unsigned short i);
static CString ToString(int i);
static CString ToString(unsigned int i);
static CString ToString(long i);
static CString ToString(unsigned long i);
static CString ToString(unsigned long long i);
static CString ToString(double i);
static CString ToString(float i);
static CString ToPercent(double d);
static CString ToKBytes(double d);
static string Left(const string& s, unsigned int u);
static string Right(const string& s, unsigned int u);
static string& Trim(string& s);
static string& LeftChomp(string& s, unsigned int uLen = 1);
static string& RightChomp(string& s, unsigned int uLen = 1);
static bool wildcmp(const string& sWild, const string& sString);
static CString& Trim(CString& s);
static bool wildcmp(const CString& sWild, const CString& sString);
static unsigned long long GetMillTime() {
struct timeval tv;
@@ -87,7 +81,7 @@ public:
m_pid = 0;
}
CLockFile(const string& sFile) {
CLockFile(const CString& sFile) {
Open(sFile);
}
@@ -103,7 +97,7 @@ public:
}
}
void Open(const string& sFile) {
void Open(const CString& sFile) {
m_fd = open(sFile.c_str(), O_RDONLY);
m_bCreated = false;
@@ -118,7 +112,7 @@ public:
}
//! timeout in milliseconds
bool TryExLock(const string& sLockFile, unsigned long long iTimeout = 0) {
bool TryExLock(const CString& sLockFile, unsigned long long iTimeout = 0) {
Open(sLockFile);
return TryExLock(iTimeout);
}
@@ -187,7 +181,7 @@ private:
int m_fd;
int m_pid;
bool m_bCreated;
string m_sFileName;
CString m_sFileName;
};
class CException {
@@ -209,21 +203,21 @@ protected:
};
class CTable : public vector<map<string, string>* > {
class CTable : public vector<map<CString, CString>* > {
public:
CTable();
virtual ~CTable();
bool AddColumn(const string& sName);
bool AddColumn(const CString& sName);
unsigned int AddRow();
bool SetCell(const string& sColumn, const string& sValue, unsigned int uRowIdx = ~0);
bool GetLine(unsigned int uIdx, string& sLine);
bool SetCell(const CString& sColumn, const CString& sValue, unsigned int uRowIdx = ~0);
bool GetLine(unsigned int uIdx, CString& sLine);
unsigned int GetColumnWidth(unsigned int uIdx);
private:
protected:
vector<string> m_vsHeaders;
map<string, unsigned int> m_msuWidths; // Used to cache the width of a column
vector<CString> m_vsHeaders;
map<CString, unsigned int> m_msuWidths; // Used to cache the width of a column
};
@@ -238,21 +232,21 @@ public:
* @iEncrypt encrypt method (BF_DECRYPT or BF_ENCRYPT)
* @sIvec what to set the ivector to start with, default sets it all 0's
*/
CBlowfish(const string & sPassword, int iEncrypt, const string & sIvec = "");
CBlowfish(const CString & sPassword, int iEncrypt, const CString & sIvec = "");
~CBlowfish();
//! output must be freed
static unsigned char *MD5(const unsigned char *input, u_int ilen);
//! returns an md5 of the string (not hex encoded)
static string MD5(const string & sInput, bool bHexEncode = false);
//! returns an md5 of the CString (not hex encoded)
static CString MD5(const CString & sInput, bool bHexEncode = false);
//! output must be the same size as input
void Crypt(unsigned char *input, unsigned char *output, u_int ibytes);
//! must free result
unsigned char * Crypt(unsigned char *input, u_int ibytes);
string Crypt(const string & sData);
CString Crypt(const CString & sData);
private:
unsigned char *m_ivec;
@@ -263,7 +257,7 @@ private:
#endif /* HAVE_LIBSSL */
#define RF_BUFF 4096
inline bool ReadFile(const string & sFilename, string & sLine) {
inline bool ReadFile(const CString & sFilename, CString & sLine) {
char inbuff[RF_BUFF];
int bytes;
// clear ourselves out
@@ -287,7 +281,7 @@ inline bool ReadFile(const string & sFilename, string & sLine) {
return true;
}
inline bool WriteFile(const string & sFilename, const string & sData) {
inline bool WriteFile(const CString & sFilename, const CString & sData) {
FILE *f = fopen(sFilename.c_str(), "w");
if (!f) {
return false;
@@ -304,7 +298,7 @@ inline bool WriteFile(const string & sFilename, const string & sData) {
return true;
}
inline bool ReadLine(const string & sData, string & sLine, u_int & iPos) {
inline bool ReadLine(const CString & sData, CString & sLine, u_int & iPos) {
sLine.clear();
if (iPos >= sData.length()) {
@@ -313,9 +307,9 @@ inline bool ReadLine(const string & sData, string & sLine, u_int & iPos) {
u_int iFind = sData.find("\n", iPos);
if (iFind == string::npos) {
if (iFind == CString::npos) {
sLine = sData.substr(iPos, (sData.length() - iPos));
iPos = string::npos;
iPos = CString::npos;
return true;
}
@@ -325,8 +319,8 @@ inline bool ReadLine(const string & sData, string & sLine, u_int & iPos) {
return true;
}
inline string Lower(const string & sLine) {
string sRet;
inline CString Lower(const CString & sLine) {
CString sRet;
for(u_int a = 0; a < sLine.length(); a++) {
sRet += tolower(sLine[a]);
}
@@ -334,8 +328,8 @@ inline string Lower(const string & sLine) {
return sRet;
}
inline string Upper(const string & sLine) {
string sRet;
inline CString Upper(const CString & sLine) {
CString sRet;
for(u_int a = 0; a < sLine.length(); a++) {
sRet += toupper(sLine[a]);
}

38
znc.cpp
View File

@@ -237,9 +237,9 @@ CString CZNC::GetConfigPath(const CString& sConfigFile) {
if (sConfigFile.empty()) {
sRetPath = GetZNCPath() + "/znc.conf";
} else {
if (CUtils::Left(sConfigFile, 2) == "./" || CUtils::Left(sConfigFile, 3) == "../") {
if (sConfigFile.Left(2) == "./" || sConfigFile.Left(3) == "../") {
sRetPath = GetCurPath() + "/" + sConfigFile;
} else if (CUtils::Left(sConfigFile, 1) != "/") {
} else if (sConfigFile.Left(1) != "/") {
sRetPath = GetZNCPath() + "/" + sConfigFile;
} else {
sRetPath = sConfigFile;
@@ -322,8 +322,8 @@ bool CZNC::WriteNewConfig(const CString& sConfig) {
const CModInfo& Info = *it;
CString sName = Info.GetName();
if (strcasecmp(CUtils::Right(sName, 3).c_str(), ".so") == 0) {
CUtils::RightChomp(sName, 3);
if (strcasecmp(sName.Right(3).c_str(), ".so") == 0) {
sName.RightChomp(3);
}
if (CUtils::GetBoolInput("Load " + CString((Info.IsSystem()) ? "system" : "local") + " module <\033[1m" + sName + "\033[22m>?", false)) {
@@ -448,16 +448,16 @@ bool CZNC::ParseConfig(const CString& sConfig) {
CChan* pChan = NULL; // Used to keep track of which chan block we are in
while (File.ReadLine(sLine)) {
while ((CUtils::Right(sLine, 1) == "\r") || (CUtils::Right(sLine, 1) == "\n")) {
while ((sLine.Right(1) == "\r") || (sLine.Right(1) == "\n")) {
CUtils::Trim(sLine);
}
if ((sLine.empty()) || (sLine[0] == '#') || (CUtils::Left(sLine, 2) == "//")) {
if ((sLine.empty()) || (sLine[0] == '#') || (sLine.Left(2) == "//")) {
continue;
}
if (CUtils::Left(sLine, 2) == "/*") {
if (CUtils::Right(sLine, 2) != "*/") {
if (sLine.Left(2) == "/*") {
if (sLine.Right(2) != "*/") {
bCommented = true;
}
@@ -465,17 +465,17 @@ bool CZNC::ParseConfig(const CString& sConfig) {
}
if (bCommented) {
if (CUtils::Right(sLine, 2) == "*/") {
if (sLine.Right(2) == "*/") {
bCommented = false;
}
continue;
}
if ((CUtils::Left(sLine, 1) == "<") && (CUtils::Right(sLine, 1) == ">")) {
CUtils::LeftChomp(sLine);
CUtils::RightChomp(sLine);
CUtils::Trim(sLine);
if ((sLine.Left(1) == "<") && (sLine.Right(1) == ">")) {
sLine.LeftChomp();
sLine.RightChomp();
sLine.Trim();
CString sTag = sLine.substr(0, sLine.find_first_of(" \t\r\n"));
CString sValue = (sTag.size() < sLine.size()) ? sLine.substr(sTag.size() +1) : "";
@@ -483,7 +483,7 @@ bool CZNC::ParseConfig(const CString& sConfig) {
CUtils::Trim(sTag);
CUtils::Trim(sValue);
if (CUtils::Left(sLine, 1) == "/") {
if (sLine.Left(1) == "/") {
CString sTag = sLine.substr(1);
if (pUser) {
@@ -599,9 +599,9 @@ bool CZNC::ParseConfig(const CString& sConfig) {
pUser->SetAltNick(sValue);
continue;
} else if (strcasecmp(sName.c_str(), "Pass") == 0) {
if (CUtils::Right(sValue, 1) == "-") {
CUtils::RightChomp(sValue);
CUtils::Trim(sValue);
if (sValue.Right(1) == "-") {
sValue.RightChomp();
sValue.Trim();
pUser->SetPass(sValue, true);
} else {
pUser->SetPass(sValue, false);
@@ -672,8 +672,8 @@ bool CZNC::ParseConfig(const CString& sConfig) {
if (strcasecmp(sName.c_str(), "ListenPort") == 0) {
m_bSSL = false;
CString sPort = sValue;
if (CUtils::Left(sPort, 1) == "+") {
CUtils::LeftChomp(sPort);
if (sPort.Left(1) == "+") {
sPort.LeftChomp();
m_bSSL = true;
}