Browse Source

the basics

isundil 7 years ago
parent
commit
48da667fff
5 changed files with 26 additions and 13 deletions
  1. 1 1
      .gitignore
  2. 5 2
      README.md
  3. 4 2
      appContext.hpp
  4. 4 3
      exception.cpp
  5. 12 5
      history.hpp

+ 1 - 1
.gitignore

@@ -8,7 +8,7 @@ SRC=	main.cpp		\
 
 OBJ=	$(SRC:.cpp=.o)
 
-CXXFLAGS=	-std=c++17 -g3
+CXXFLAGS=	-std=c++17 -g3 -W -Wall -Wextra
 
 $(OUTPUT):	all
 

+ 5 - 2
README.md

@@ -31,7 +31,7 @@ std::string AppContext::GetAbsolutePath(const std::string &dir, const std::strin
     return ss.str();
 }
 
-AppContext::AppContext(char **av, char **en)
+AppContext::AppContext(char **av)
 {
     const std::string appName = *av;
     vector<string> args = ::GetArgs(av +1);
@@ -45,6 +45,7 @@ AppContext::AppContext(char **av, char **en)
     bool historyOnly = false;
     bool flush = false;
     bool getHistoryFile = false;
+    bool verbose = false;
     string histDir;
     std::stringstream ss ("cd_");
     ss << "cd_" << getppid() << ".hist";
@@ -69,6 +70,8 @@ AppContext::AppContext(char **av, char **en)
             historyOnly = true;
         else if (*i == "--flush" || *i == "-F")
             flush = true;
+        else if (*i == "--verbose" || *i == "-v")
+            verbose = true;
         else if (*i == "--help")
             usageOnly = true;
         else if (path.empty())
@@ -83,7 +86,7 @@ AppContext::AppContext(char **av, char **en)
             path = getenv("HOME");
         // FIXME else wtf
     }
-    fArgs = AppContext::Args(usageOnly, historyOnly, getHistoryFile, flush, histDir.empty() ? "/tmp/" : histDir, histFile.empty() ? defaultHistFile : histFile, path);
+    fArgs = AppContext::Args(usageOnly, historyOnly, getHistoryFile, flush, verbose, histDir.empty() ? "/tmp/" : histDir, histFile.empty() ? defaultHistFile : histFile, path);
 }
 
 bool AppContext::IsReadOnly() const

+ 4 - 2
appContext.hpp

@@ -10,7 +10,7 @@ class ArgumentException;
 class AppContext
 {
     public:
-        AppContext(char **, char **);
+        AppContext(char **);
         static std::string GetAbsolutePath(const std::string &, const std::string &);
 
         struct Args {
@@ -19,17 +19,19 @@ class AppContext
                 bool historyOnly;
                 bool getHistoryFile;
                 bool flush;
+                bool verbose;
                 std::string histDir;
                 std::string histFile;
                 std::string histAbsolutePath;
                 std::string pushArg;
 
                 Args(){};
-                Args(bool _usageOnly, bool _historyOnly, bool _getHistoryFile, bool _flush, const std::string &_histDir, const std::string &_histFile, const std::string &_pushArg):
+                Args(bool _usageOnly, bool _historyOnly, bool _getHistoryFile, bool _flush, bool _verbose, const std::string &_histDir, const std::string &_histFile, const std::string &_pushArg):
                     usageOnly(_usageOnly),
                     historyOnly(_historyOnly),
                     getHistoryFile(_getHistoryFile),
                     flush(_flush),
+                    verbose(_verbose),
                     histDir(_histDir),
                     histFile(_histFile),
                     histAbsolutePath(GetAbsolutePath(histDir, histFile)),

+ 4 - 3
exception.cpp

@@ -15,6 +15,7 @@ static std::string RelativePathToAbsolute(const string &path)
     if (path[0] == '/')
         return path;
     char *p = realpath(path.c_str(), nullptr);
+    // FIXME error check
     string pathResult(p);
     free(p);
     return pathResult;
@@ -64,9 +65,9 @@ void History::Read(std::istream &file)
     PTR_TYPE current = -1;
     PTR_TYPE prev = -1;
     unsigned int histIndex =0;
-    size_t size;
+    std::streamsize size;
     char *buf = nullptr;
-    size_t bufSize = 0;
+    std::streamsize bufSize = 0;
 
     hist.clear();
     if (!file.good() && errno != ENOENT)
@@ -196,7 +197,7 @@ const HistoryEntry *History::GetNextWorkingDirectory(const AppContext &app) cons
             if (e)
                 return new HistoryEntry(*e);
         }
-        if (buf = getenv("OLDPWD"))
+        if ((buf = getenv("OLDPWD")))
             return new HistoryEntry(hist.size() +1, buf);
         else
             return nullptr;

+ 12 - 5
history.hpp

@@ -14,16 +14,22 @@ void Usage(std::ostream &out, const std::string &progName)
     // --history
     // --histDir
     // --histFile
+    /*  "--gethistfile"
+        "--history"
+        "--flush" || "-F"
+        "--verbose" || "-v"
+        "--help"
+    */
     out << progName << std::endl;
 }
 
-AppContext *GetAppContext(char **av, char **en)
+AppContext *GetAppContext(char **av)
 {
     AppContext *app;
 
     try
     {
-        app = new AppContext(av, en);
+        app = new AppContext(av);
     }
     catch (ArgumentException &e)
     {
@@ -57,9 +63,9 @@ void PrintHistory(std::ostream &out, const std::string &progName, const History
     }
 }
 
-int main(int ac, char **av, char **ev)
+int main(int, char **av)
 {
-    AppContext *app = GetAppContext(av, ev);
+    AppContext *app = GetAppContext(av);
     bool written = false;
 
     if (!app)
@@ -99,7 +105,8 @@ int main(int ac, char **av, char **ev)
     if (nextEnt)
     {
         written |= ((HistoryRW*)hist)->Append(*nextEnt);
-        std::cerr << "exec {" << nextEnt->GetPath() << "}" << std::endl;
+        if (app->GetArgs().verbose)
+            std::cerr << nextEnt->GetPath() << std::endl;
         std::cout << nextEnt->GetPath() << std::endl;
         delete nextEnt;
     }