Browse Source

No more using namespace std;

bugz 4 years ago
parent
commit
b302c0b7ef
2 changed files with 59 additions and 67 deletions
  1. 32 38
      hharry.cpp
  2. 27 29
      input-int.cpp

+ 32 - 38
hharry.cpp

@@ -1,16 +1,24 @@
+#include "terminal.h"
+#include "utils.h"
+#include "wordplay.h"
+#include <ctype.h>
 #include <fcntl.h>
+#include <fstream>
+#include <iostream>
 #include <pty.h>
+#include <sstream>
 #include <stdio.h>
+#include <stdlib.h> // random()
 #include <string.h>
+#include <string>
+#include <strings.h> // strcasecmp
 #include <sys/select.h>
 #include <sys/wait.h>
 #include <termios.h>
+#include <time.h>
 #include <unistd.h>
 
-#include <fstream>
-#include <iostream>
-#include <sstream>
-#include <string>
+struct console_details console;
 
 /*
 We get the username/fullname from tailing the node logs,
@@ -19,16 +27,8 @@ and reading the user.dat file.
 std::string username;
 std::string fullname;
 
-using namespace std;
-
 // #include <signal.h> // handle Ctrl-C/SIGINT
 
-#include <strings.h> // strcasecmp
-#include <time.h>
-
-#include <ctype.h>
-#include <stdlib.h> // random()
-
 /* Log level guideline:
  * - ZF_LOG_FATAL - happened something impossible and absolutely unexpected.
  *   Process can't continue and must be terminated.
@@ -65,6 +65,7 @@ using namespace std;
 // zf_log.h is included.
 
 // LOGGING with file output
+
 #include "zf_log.h"
 
 FILE *g_log_file;
@@ -93,13 +94,6 @@ void log_flush(void) { fflush(g_log_file); }
 
 // END LOGGING
 
-#include "terminal.h"
-#include "utils.h"
-
-struct console_details console;
-
-#include "wordplay.h"
-
 /*
 What is the name of the actual, real Mystic executable
 that we'll be executing and mangling?
@@ -149,18 +143,18 @@ int locate_user(const char *alias) {
   return 1;
 }
 
-ifstream logfile;
-streampos log_pos;
+std::ifstream logfile;
+std::streampos log_pos;
 
 void open_mystic_log(void) {
-  string mystic_logfile;
+  std::string mystic_logfile;
   {
-    ostringstream buffer;
+    std::ostringstream buffer;
 
     buffer << "logs/node" << node << ".log";
     mystic_logfile = buffer.str();
   };
-  logfile.open(mystic_logfile, ios_base::in | ios_base::ate);
+  logfile.open(mystic_logfile, std::ios_base::in | std::ios_base::ate);
   // Ok, we're at the end of the file.  Or should be.
   if (logfile.is_open()) {
     ZF_LOGD("Log %s open", (const char *)mystic_logfile.c_str());
@@ -175,24 +169,24 @@ void scan_mystic_log(void) {
   if (logfile.is_open()) {
     int again = 0;
     do {
-      string line = find_new_text(logfile, log_pos);
+      std::string line = find_new_text(logfile, log_pos);
       if (line.empty())
         return;
       again = 1;
 
       ZF_LOGD("mystic log: %s", (const char *)line.c_str());
       // Ok, we have a line, look for interesting details
-      if (line.find("New user application") != string::npos) {
+      if (line.find("New user application") != std::string::npos) {
         ZF_LOGE("New User");
       }
 
       size_t pos;
       pos = line.find("Created Account: ");
-      if (pos != string::npos) {
+      if (pos != std::string::npos) {
         pos += 18 - 1;
         // Ok, find the end '#'
         size_t len = line.find('#', pos);
-        if (len != string::npos) {
+        if (len != std::string::npos) {
           username = line.substr(pos, len - pos - 1);
           ZF_LOGE("New User: %s", (const char *)username.c_str());
           // once we know this works -- lookup user's record
@@ -202,10 +196,10 @@ void scan_mystic_log(void) {
         }
       }
       pos = line.find(" logged in");
-      if (pos != string::npos) {
+      if (pos != std::string::npos) {
         --pos;
         size_t len = line.rfind(' ', pos);
-        if (len != string::npos) {
+        if (len != std::string::npos) {
           len++;
           username = line.substr(len, pos + 1 - len);
           ZF_LOGE("User: %s", (const char *)username.c_str());
@@ -366,9 +360,9 @@ int main(int argc, char *argv[]) {
     return 2;
   }
 
-  string logfile;
+  std::string logfile;
   {
-    ostringstream buffer;
+    std::ostringstream buffer;
 
     buffer << "horrible_harry_" << node << ".log";
     logfile = buffer.str();
@@ -447,9 +441,9 @@ int main(int argc, char *argv[]) {
     This doesn't need to be static -- because it is part of
     main.  Once main ends, we're done.
     */
-    string buffer;
+    std::string buffer;
     buffer.reserve(BSIZE * 2);
-    string play;
+    std::string play;
     play.reserve(4096);
 
     int zmodem = 0;
@@ -561,10 +555,10 @@ int main(int argc, char *argv[]) {
           if (zmodem) {
             // Ok, we're zmodem mode -- is it time to exit?
             size_t zend = buffer.find("\x1b[0m");
-            if (zend != string::npos)
+            if (zend != std::string::npos)
               zmodem = 0;
             zend = buffer.find("\x1b[1;1H");
-            if (zend != string::npos)
+            if (zend != std::string::npos)
               zmodem = 0;
             if (!zmodem)
               ZF_LOGD("Zmodem end");
@@ -572,7 +566,7 @@ int main(int argc, char *argv[]) {
             // Should we be in zmodem mode?
             size_t zstart = buffer.find("**\x18"
                                         "B0");
-            if (zstart != string::npos) {
+            if (zstart != std::string::npos) {
               zmodem = 1;
               ZF_LOGD("Zmodem start");
             }
@@ -593,7 +587,7 @@ int main(int argc, char *argv[]) {
             size_t pos = buffer.rfind("\r\n");
             // rstrnstr(buffer, size, "\r\n");
             //  >= 0) {
-            if (pos != string::npos) {
+            if (pos != std::string::npos) {
               // found something!
 
               pos += 2;

+ 27 - 29
input-int.cpp

@@ -3,19 +3,17 @@
 #include <regex>
 #include <string>
 
-using namespace std;
-
 int main() {
-  string input;
+  std::string input;
   // regex integer("(\\+|-)?[[:digit:]]+");
-  regex integer("(\\+|-)?([[:digit:]]+)");
-  smatch match;
+  std::regex integer("(\\+|-)?([[:digit:]]+)");
+  std::smatch match;
   // As long as the input is correct ask for another number
   while (true) {
-    cout << "Give me an integer! (q to quit)" << endl;
-    getline(cin, input);
+    std::cout << "Give me an integer! (q to quit)" << std::endl;
+    std::getline(std::cin, input);
     // cin >> input;
-    if (!cin)
+    if (!std::cin)
       break;
     // Exit when the user inputs q
     if (input == "q")
@@ -23,56 +21,56 @@ int main() {
 
     // Apparently, hitting enter, doesn't give you a blank input!  use getline.
 
-    cout << "Length: " << input.length() << " Empty? " << input.empty() << endl;
+    std::cout << "Length: " << input.length() << " Empty? " << input.empty() << std::endl;
     if (input.empty())
       break;
 
     // I need position and length.
-    vector<pair<int,int>> pos_len;
+    std::vector<std::pair<int,int>> pos_len;
 
-    for (auto it = sregex_iterator(input.begin(), input.end(), integer);
-         it != sregex_iterator(); ++it) {
-	    pos_len.push_back(make_pair(it->position(), it->length() ) );
-      cout << it->position() << " " << it->length() << " : " << it->str()
-           << endl;
+    for (auto it = std::sregex_iterator(input.begin(), input.end(), integer);
+         it != std::sregex_iterator(); ++it) {
+	    pos_len.push_back(std::make_pair(it->position(), it->length() ) );
+      std::cout << it->position() << " " << it->length() << " : " << it->str()
+           << std::endl;
     }
 
-    for (vector<pair<int,int>>::iterator vit = pos_len.begin(); vit != pos_len.end(); ++vit ) {
-	    pair<int,int> p;
+    for (std::vector<std::pair<int,int>>::iterator vit = pos_len.begin(); vit != pos_len.end(); ++vit ) {
+	    std::pair<int,int> p;
 		p = *vit;
     cout << (*vit).first << "," << (*vit).second << endl;
     cout << p.first << "," << p.second << endl;
 	}
 
 #ifdef RX1
-    if (regex_match(input, match, integer)) {  
-      cout << "integer" << endl;
+    if (std::regex_match(input, match, integer)) {  
+      std::cout << "integer" << std::endl;
       for (int i = 0; i < match.size(); ++i) {
         std::cout << "match " << i << " (" << match[i] << ") At "
                   << match.position(i) << " of " << match.length(i) << " chars."
-                  << endl;
+                  << std::endl;
       }
-      for (smatch::iterator im = match.begin(); im != match.end(); ++im) {
-        cout << "matches:" << *im << std::endl;
+      for (std::smatch::iterator im = match.begin(); im != match.end(); ++im) {
+        std::cout << "matches:" << *im << std::endl;
       }
     } else {
-      cout << "Invalid input" << endl;
+      std::cout << "Invalid input" << std::endl;
     }
 
     int offset = 0;
-    while (regex_search(input, match, integer)) {
-      cout << "input: [" << input << "]" << endl;
-      cout << offset << " match len " << match.length(0) << endl;
+    while (std::regex_search(input, match, integer)) {
+      std::cout << "input: [" << input << "]" << std::endl;
+      std::cout << offset << " match len " << match.length(0) << std::endl;
       for (int i = 0; i < match.size(); ++i) {
         std::cout << "match " << i << " (" << match[i] << ") At "
                   << offset + match.position(i) << " of " << match.length(i)
-                  << " chars." << endl;
+                  << " chars." << std::endl;
       }
 
       for (auto x : match) {
-        cout << x << " ";
+        std::cout << x << " ";
       }
-      cout << endl;
+      std::cout << std::endl;
       input = match.suffix().str();
       offset += match.length(0) + match.prefix(0).size();
     }