about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--.pcoin.cpp.swpbin0 -> 1024 bytes
-rw-r--r--.tcoin.cpp.swpbin0 -> 1024 bytes
-rw-r--r--pcoin.cpp59
-rw-r--r--tcoin.cpp46
4 files changed, 96 insertions, 9 deletions
diff --git a/.pcoin.cpp.swp b/.pcoin.cpp.swp
new file mode 100644
index 0000000..7a7ba82
--- /dev/null
+++ b/.pcoin.cpp.swp
Binary files differdiff --git a/.tcoin.cpp.swp b/.tcoin.cpp.swp
new file mode 100644
index 0000000..727320a
--- /dev/null
+++ b/.tcoin.cpp.swp
Binary files differdiff --git a/pcoin.cpp b/pcoin.cpp
index fb85cdd..337fc08 100644
--- a/pcoin.cpp
+++ b/pcoin.cpp
@@ -392,8 +392,48 @@ void show_messages(const char* username)
 {
   std::string messages_path = std::string(TCOIN_MSG_PATH) + std::string(username) + std::string("_messages.txt");
   std::ifstream fin(messages_path.c_str());
-  std::cout << fin.rdbuf();
-  std::cout << "\n";
+  char ch;
+  bool first_char_is_newline = false;
+  bool reached_eof = false;
+  for(int i=0; i < 2; ++i)
+  {
+    if(ch = fin.get())
+    {
+      if(ch == std::istream::traits_type::eof()) //https://stackoverflow.com/questions/4533063/how-does-ifstreams-eof-work
+      {
+        if(first_char_is_newline && i==1)
+        {
+          reached_eof = true;
+          std::cout << "No messages found.\n";
+        }
+        break;
+      }
+      else
+      {
+        if(i==0 && ch=='\n')
+        {
+          first_char_is_newline = true;
+        }
+        std::cout << ch;
+      }
+    }
+  }
+  if(!reached_eof)
+  {
+    std::cout << fin.rdbuf();
+  }
+
+  //removing eofbit
+  fin.clear();
+  //moving back two places from the end to read the last two characters
+  fin.seekg(-2, std::ios::end);
+  char chs[2]; //chs = characters
+  chs[0] = fin.get();
+  chs[1] = fin.get();
+  fin.get(); //to set eofbit again because I like it to be just the way it was before removing the eofbit
+  if(chs[0]!='\n' && chs[1]=='\n') //if only one newline at the end of the file
+    std::cout << "\n"; //print another one
+
   fin.close();
 }
 
@@ -436,11 +476,18 @@ void show_messages_tail(const char* username, int lineCount)
       continue; //we're counting cutting off a '\n ' (and '\n\n') as zero (and one) newline cut off because "<stuff>\n \_message>\n\n" is one message
     --newlineCount;
   }
-  std::cout << "Last " << lineCount << " Messages:\n\n";
+  std::cout << "Last " << lineCount << " Messages:\n";
   std::vector<char>::iterator end = remove(start, buffer.end(), '\r');
-  std::cout << std::string(start, end);
-  if(*(end-2) != '\n') //if it ends with two newlines, don't put another one
-    std::cout << "\n";
+  if((start == (end-1)) && (*(start) == '\n')) //there is only one character, and it is a newline (i.e.. no messages)
+    std::cout << "\nNo messages found.\n\n";
+  else
+  {
+    if(*(start) != '\n') //if it starts with a newline, don't put another one
+      std::cout << "\n";
+    std::cout << std::string(start, end);
+    if(*(end-2) != '\n' && *(end-1) == '\n') //if it ends with two newlines, don't put another one
+      std::cout << "\n";
+  }
 }
 
 void show_tsv_messages_tail(const char* username, int lineCount)
diff --git a/tcoin.cpp b/tcoin.cpp
index 73f0211..35cd6d8 100644
--- a/tcoin.cpp
+++ b/tcoin.cpp
@@ -467,9 +467,49 @@ void show_messages(const char* username)
 {
   std::string messages_path = std::string(TCOIN_MSG_PATH) + std::string(username) + std::string("_messages.txt");
   std::ifstream fin(messages_path.c_str());
-  std::cout << "Messages:\n\n";
-  std::cout << fin.rdbuf();
-  std::cout << "\n";
+  std::cout << "Messages:\n";
+  char ch;
+  bool first_char_is_newline = false;
+  bool reached_eof = false;
+  for(int i=0; i < 2; ++i)
+  {
+    if(ch = fin.get())
+    {
+      if(ch == std::istream::traits_type::eof()) //https://stackoverflow.com/questions/4533063/how-does-ifstreams-eof-work
+      {
+        if(first_char_is_newline && i==1)
+        {
+          reached_eof = true;
+          std::cout << "No messages found.\n";
+        }
+        break;
+      }
+      else
+      {
+        if(i==0 && ch=='\n')
+        {
+          first_char_is_newline = true;
+        }
+        std::cout << ch;
+      }
+    }
+  }
+  if(!reached_eof)
+  {
+    std::cout << fin.rdbuf();
+  }
+
+  //removing eofbit
+  fin.clear();
+  //moving back two places from the end to read the last two characters
+  fin.seekg(-2, std::ios::end);
+  char chs[2]; //chs = characters
+  chs[0] = fin.get();
+  chs[1] = fin.get();
+  fin.get(); //to set eofbit again because I like it to be just the way it was before removing the eofbit
+  if(chs[0]!='\n' && chs[1]=='\n') //if only one newline at the end of the file
+    std::cout << "\n"; //print another one
+
   fin.close();
 }