From 815a1816c8a1ee14b28c776bf92c172b1fcdf13a Mon Sep 17 00:00:00 2001 From: clinton Date: Mon, 23 Feb 2009 23:43:38 +0000 Subject: [PATCH] Remove String->const char* conversion operator in favor of c_str method This was dumb to begin with (I reserve the right to be mean to my younger self) and really only caused a number of evil cases of ambiguious overloading whenever a String and std::string are used together. --- source/Bot.C | 16 ++++++++-------- source/Interp.C | 12 ++++++------ source/Main.C | 4 ++-- source/Mask.C | 4 ++-- source/ShitList.C | 8 ++++---- source/Socket.C | 8 ++++---- source/String.C | 9 +++++---- source/String.H | 2 +- source/UserCommands.C | 22 +++++++++++----------- source/UserList.C | 10 +++++----- 10 files changed, 48 insertions(+), 47 deletions(-) diff --git a/source/Bot.C b/source/Bot.C index e46a87e..cc860b9 100644 --- a/source/Bot.C +++ b/source/Bot.C @@ -130,7 +130,7 @@ Bot::Bot(String filename, bool debug_on) dccConnections = new DCCManager (); // Let's read the alias file - std::ifstream initFile(initFileName); + std::ifstream initFile(initFileName.c_str ()); if (initFile) { @@ -235,7 +235,7 @@ Bot::logLine(String line) void Bot::readConfig() { - std::ifstream file(configFileName); + std::ifstream file(configFileName.c_str ()); String temp; int line = 1; @@ -324,7 +324,7 @@ Bot::readConfig() else if (command == "LOCALIP") localIP = parameters; else if (command == "MAXNICKLENGTH") - MAX_NICKLENGTH = std::atoi (parameters); + MAX_NICKLENGTH = std::atoi (parameters.c_str ()); else if (command == "SERVER") { if (parameters.indexOf(' ') == -1) serverList->addServer(new Server(parameters)); @@ -753,14 +753,14 @@ Bot::destroy_user_functions () void Bot::set_log_file (String name) { - logFileName = name; + logFileName = logs_dir + name; logFile.close (); logFile.clear (); #if HAVE_IOSBASE - logFile.open(logs_dir + logFileName, std::ios_base::out | + logFile.open(logFileName.c_str (), std::ios_base::out | std::ios_base::ate | std::ios_base::app); #else - logFile.open(logs_dir + logFileName, ios::out | ios::ate + logFile.open(logFileName.c_str (), ios::out | ios::ate | ios::app); #endif @@ -772,11 +772,11 @@ Bot::set_log_dir (String dir) { logs_dir = dir; - DIR *temp = opendir (logs_dir); + DIR *temp = opendir (logs_dir.c_str ()); if (!temp) { - mkdir (logs_dir, S_IRWXU); + mkdir (logs_dir.c_str (), S_IRWXU); } else { diff --git a/source/Interp.C b/source/Interp.C index 911bad2..e634d35 100644 --- a/source/Interp.C +++ b/source/Interp.C @@ -150,8 +150,8 @@ interp_init_helper (void* unused) // sys-dir scm_c_define ("bot:sys-scripts-dir", - scm_from_locale_string (String(PREFIX) + - "/share/bobotpp/scripts/")); + scm_from_locale_string ((String(PREFIX) + + "/share/bobotpp/scripts/").c_str ())); // Hooks scm_c_define ("hooks/action", scm_from_int(Hook::ACTION)); scm_c_define ("hooks/nickname", scm_from_int(Hook::NICKNAME)); @@ -286,8 +286,8 @@ interp_post_startup_helper (void *bot_module) // load bobot-utils scm_primitive_load - (scm_from_locale_string (String(PREFIX) + - "/share/bobotpp/scripts/bobot-utils.scm")); + (scm_from_locale_string ((String(PREFIX) + + "/share/bobotpp/scripts/bobot-utils.scm").c_str ())); return SCM_UNSPECIFIED; } @@ -326,7 +326,7 @@ Interp::Execute(Bot *b, String command) bot = b; scm_internal_catch(SCM_BOOL_T, - (scm_t_catch_body) lazy_eval_string, (void *) static_cast (command), + (scm_t_catch_body) lazy_eval_string, (void *) static_cast (command.c_str ()), (scm_t_catch_handler) Interp::EmptyHandler, 0); #ifdef MULTITHREAD @@ -346,7 +346,7 @@ Interp::LoadScript(Bot *b, String filename) bot = b; scm_internal_catch(SCM_BOOL_T, (scm_t_catch_body) lazy_eval_file, - (void *)static_cast(filename), + (void *)static_cast(filename.c_str ()), (scm_t_catch_handler) Interp::EmptyHandler, 0); #ifdef MULTITHREAD // We release the lock diff --git a/source/Main.C b/source/Main.C index 93b61a9..8e187fe 100644 --- a/source/Main.C +++ b/source/Main.C @@ -200,9 +200,9 @@ namespace DIR * temp; - if (! (temp = opendir (String(getenv ("HOME")) + "/.bobotpp/logs"))) + if (! (temp = opendir ((String(getenv ("HOME")) + "/.bobotpp/logs").c_str ()))) { - mkdir (String(getenv ("HOME")) + "/.bobotpp/logs", + mkdir ((String(getenv ("HOME")) + "/.bobotpp/logs").c_str (), S_IRWXU); } else diff --git a/source/Mask.C b/source/Mask.C index 7d0a948..cb8bfbe 100644 --- a/source/Mask.C +++ b/source/Mask.C @@ -32,13 +32,13 @@ Mask::getMask() const bool Mask::matches(String s) const { - return match((const char *)mask, (const char *)s); + return match(mask.c_str (), s.c_str ()); } bool Mask::matches(const Mask & m) const { - return match((const char *)mask, (const char *)m.mask); + return match(mask.c_str (), m.mask.c_str ()); } bool diff --git a/source/ShitList.C b/source/ShitList.C index 320c6f0..98f0071 100644 --- a/source/ShitList.C +++ b/source/ShitList.C @@ -48,7 +48,7 @@ ShitList::~ShitList() void ShitList::read() { - std::ifstream file(listFileName); + std::ifstream file(listFileName.c_str ()); String temp; int line = 1; @@ -66,8 +66,8 @@ ShitList::read() String level = st.next_token(':'); String expiration = st.next_token(':'); String reason = Utils::trim_str (st.rest()); - l.push_back (new ShitEntry(mask, channelMask, std::atoi(level), - std::atol(expiration), reason)); + l.push_back (new ShitEntry(mask, channelMask, std::atoi(level.c_str ()), + std::atol(expiration.c_str ()), reason)); line++; } file.close(); @@ -77,7 +77,7 @@ void ShitList::save() { std::list::iterator it = l.begin(); - std::ofstream file(listFileName); + std::ofstream file(listFileName.c_str ()); if (!file) return; diff --git a/source/Socket.C b/source/Socket.C index b0f7293..6581047 100644 --- a/source/Socket.C +++ b/source/Socket.C @@ -94,7 +94,7 @@ Socket::setRemoteHostname(String hostname) { struct hostent *host; - if ((host = gethostbyname((const char *)hostname)) == 0) + if ((host = gethostbyname(hostname.c_str ())) == 0) return false; memcpy(&remoteAddress, host->h_addr, host->h_length); @@ -123,7 +123,7 @@ Socket::setLocalHostname(String hostname) { struct hostent *host; - if ((host = gethostbyname((const char *)hostname)) == 0) + if ((host = gethostbyname(hostname.c_str ())) == 0) return false; memcpy(&localAddress, host->h_addr, host->h_length); @@ -237,12 +237,12 @@ Socket::write(String s, bool m) return false; if (m) { - if (::write(fd->fd, (const char *)s, s.length()) + + if (::write(fd->fd, s.c_str (), s.length()) + ::write(fd->fd, "\n", 1) != s.length() + 1) return false; } else - if (::write(fd->fd, (const char *)s, s.length()) + + if (::write(fd->fd, s.c_str (), s.length()) + ::write(fd->fd, "\r\n", 2) != s.length() + 2) return false; diff --git a/source/String.C b/source/String.C index f5f6fbb..05acdff 100644 --- a/source/String.C +++ b/source/String.C @@ -292,10 +292,11 @@ String::operator+(const std::string & s) return my_string + s; } -String::operator const char *() const -{ - return my_string.c_str (); -} +const char* +String::c_str () const + { + return my_string.c_str (); + } String::operator std::string () const { diff --git a/source/String.H b/source/String.H index b138a57..7d1bee9 100644 --- a/source/String.H +++ b/source/String.H @@ -82,7 +82,7 @@ public: friend std::string operator+(const std::string &, const String &); - operator const char *() const; + const char* c_str () const; operator std::string () const; friend std::ostream & operator<<(std::ostream &, const String &); diff --git a/source/UserCommands.C b/source/UserCommands.C index c60b91e..40da3a6 100644 --- a/source/UserCommands.C +++ b/source/UserCommands.C @@ -129,17 +129,17 @@ UserCommands::AddUser(ServerConnection *cnx, Person *from, bool a; time_t e; - l = atoi((const char *)level); + l = atoi(level.c_str ()); if (l < 0 || l > User::FRIEND) return; if (l > Utils::get_level(cnx->bot, from->getAddress())) { from->sendNotice("\002You can not give a level greater than yours.\002"); return; } - p = atoi((const char *)prot); + p = atoi(prot.c_str ()); if (p < 0 || p > User::NO_DEOP) return; - a = (bool)atoi((const char *)aop); + a = (bool)atoi(aop.c_str ()); if (a != 0 && a != 1) return; @@ -184,7 +184,7 @@ UserCommands::AddServer(ServerConnection *cnx, Person *from, if (st.more_tokens_p()) { String temp = st.next_token(); - port = atoi((const char *)temp); + port = atoi(temp.c_str ()); } Message m = Commands::AddServer(cnx->bot, serverName, port); @@ -240,7 +240,7 @@ UserCommands::AddShit(ServerConnection *cnx, Person *from, int l; time_t e; - l = atoi((const char *)level); + l = atoi(level.c_str ()); if (l < 0 || l > ShitEntry::SHIT_NODEBAN) return; @@ -417,7 +417,7 @@ UserCommands::Channels(ServerConnection *cnx, Person *from, if (result == "") from->sendNotice("\002I am not on any channel.\002"); else - from->sendNotice(String("\002I am currently on channel(s):\002 ") + + from->sendNotice(String("\002I am currently on channel(s):\002 ") + result); } @@ -475,7 +475,7 @@ UserCommands::DelServer(ServerConnection *cnx, Person *from, return; } - int serverNumber = atoi(rest); + int serverNumber = atoi(rest.c_str ()); Message m = Commands::DelServer(cnx->bot, serverNumber); if (m.getCode() != 0) @@ -640,7 +640,7 @@ UserCommands::Help(ServerConnection *cnx, Person *from, StringTokenizer st(rest); String command = Utils::to_upper (st.next_token()); - std::ifstream helpFile(cnx->bot->helpFileName); + std::ifstream helpFile(cnx->bot->helpFileName.c_str ()); if (!helpFile) { from->sendNotice(String("\002Error: I can not find the " @@ -1170,7 +1170,7 @@ UserCommands::Server(ServerConnection *cnx, Person *from, return; } - serverNumber = atoi(rest); + serverNumber = atoi(rest.c_str ()); if (serverNumber < 0 || serverNumber >= cnx->bot->serverList->size()) { from->sendNotice(String((long)serverNumber) + @@ -1214,11 +1214,11 @@ void UserCommands::SetFloodRate(ServerConnection *cnx, Person *from, String channel, String rest) { - Message m = Commands::SetFloodRate (cnx->bot, std::atoi (rest)); + Message m = Commands::SetFloodRate (cnx->bot, std::atoi (rest.c_str ())); if (m.getCode ()) from->sendNotice (m.getMessage ()); else - from->sendNotice ("Flood Rate set to:" + String(std::atol(rest))); + from->sendNotice ("Flood Rate set to:" + String(std::atol(rest.c_str ()))); } void diff --git a/source/UserList.C b/source/UserList.C index ef2d21f..afd49fa 100644 --- a/source/UserList.C +++ b/source/UserList.C @@ -39,7 +39,7 @@ UserList::~UserList() void UserList::read() { - std::ifstream file(listFilename); + std::ifstream file(listFilename.c_str ()); String temp, empty = ""; int line = 1; @@ -75,9 +75,9 @@ UserList::read() password = ""; } - l.push_back(new UserListItem(mask, maskChannel, std::atoi(level), - std::atoi(prot), std::atoi(aop), - std::atol(expiration), password)); + l.push_back(new UserListItem(mask, maskChannel, std::atoi(level.c_str ()), + std::atoi(prot.c_str ()), std::atoi(aop.c_str ()), + std::atol(expiration.c_str ()), password)); line++; } @@ -88,7 +88,7 @@ void UserList::save() { std::list::iterator it = l.begin(); - std::ofstream file(listFilename); + std::ofstream file(listFilename.c_str ()); if (!file) return; -- 2.20.1