about summary refs log tree commit diff stats
path: root/pcoin.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'pcoin.cpp')
-rw-r--r--pcoin.cpp60
1 files changed, 40 insertions, 20 deletions
diff --git a/pcoin.cpp b/pcoin.cpp
index b6f2f8f..2d5a490 100644
--- a/pcoin.cpp
+++ b/pcoin.cpp
@@ -28,7 +28,8 @@
 #define TCOIN_PASS_PATH "/home/login/tcoin/passwords/"
 #define TCOIN_PROG_ACT_PATH "/home/login/tcoin/program_accounting/"
 #define PROG_ACT_W_SLASH "program_accounting/"
-#define PCOIN_KEY_PATH "/home/login/bin/pcoin_keys"
+#define PCOIN_KEY_PATH_W_SLASH "/home/login/bin/pcoin_keys/"
+#define LS_PCOIN_KEY_CMD "/bin/ls /home/login/bin/pcoin_keys"
 #define TCOIN_CODEZ_PATH "/home/login/bin/tcoin_codez"
 #define TCOIN_BIN_PATH_W_SPACE "/home/login/bin/tcoin "
 
@@ -556,21 +557,30 @@ void show_tsv_messages_tail(const char* username, int lineCount)
 
 bool program_exists(const char* username)
 {
-  std::ifstream fin(PCOIN_KEY_PATH);
-  //first word is program username, second word is key
-  std::string word1;
-  std::string word2;
-  while(fin >> word1)
+  char *program_key_path = new char[strlen(username) + sizeof(PCOIN_KEY_PATH_W_SLASH) + 4]; //sizeof counts NULL char at the end too
+  std::strcpy(program_key_path, PCOIN_KEY_PATH_W_SLASH);
+  std::strcat(program_key_path, username);
+  std::strcat(program_key_path, ".txt");
+
+  std::ifstream fin(program_key_path);
+  bool return_value = false;
+
+  if(!fin) //file doesn't exist
   {
-    if(!word1.compare(username))
-    {
-      fin.close();
-      return true;
-    }
-    fin >> word2; //to get rid of the second word (which is the key)
+    fin.close();
+    delete[] program_key_path;
+
+    return_value = false; //program account not found
   }
-  fin.close();
-  return false; //if program not found
+  else
+  {
+    fin.close();
+    delete[] program_key_path;
+
+    return_value = true; //program account found
+  }
+
+  return return_value;
 }
 
 bool username_exists(const char* username)
@@ -1463,22 +1473,32 @@ bool is_number(const char* test_string)
 
 std::string get_username_from_key(std::string &key)
 {
-  std::ifstream fin(PCOIN_KEY_PATH);
+  const static std::string all_usernames_dot_txt = exec(LS_PCOIN_KEY_CMD);
+  std::istringstream iss(all_usernames_dot_txt);
+
   std::string word1, word2;
-  bool should_return = false;
-  //first word is program username, second word is key
-  while(fin >> word1)
+  //first word is program username with .txt on the end, second word is key
+  while(iss >> word1)
   {
+    char *program_key_path = new char[strlen(word1.c_str()) + sizeof(PCOIN_KEY_PATH_W_SLASH)]; //sizeof counts NULL char at the end too
+    std::strcpy(program_key_path, PCOIN_KEY_PATH_W_SLASH);
+    std::strcat(program_key_path, word1.c_str());
+
+    std::ifstream fin(program_key_path);
+
     fin >> word2;
+
     if(!strctcmp(word2.c_str(), key.c_str()))
     {
       fin.close();
-      return word1;
+      delete[] program_key_path;
+      return word1.substr(0, word1.size()-4); //removing .txt from the username returned
     }
+    fin.close();
+    delete[] program_key_path;
   }
 
   word1.assign("n/a");
-  fin.close();
   return word1;
 }