teditor  1.8.0@@fee5e94
Terminal based editor written in C++
file_utils.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <vector>
5 #include <unordered_map>
6 #include "utils.h"
7 
8 
9 namespace teditor {
10 
11 bool isDir(const std::string& f);
12 bool isFile(const std::string& f);
13 bool isReadOnly(const char* f);
14 void makeDir(const std::string& d);
15 
17 struct Remote {
18  std::string host, file;
19 
20  Remote(const std::string& f);
21 };
22 
23 bool isRemote(const std::string& f);
24 
25 std::string slurp(const std::string& file);
26 Strings slurpToArr(const std::string& file);
27 
28 std::string getpwd();
29 bool isAbs(const std::string& file);
30 std::string rel2abs(const std::string& pwd, const std::string& rel);
31 std::string basename(const std::string& file);
32 std::string dirname(const std::string& file);
33 std::string tempFileName();
35 std::string findFirstUpwards(const std::string& dir, const std::string& file);
37 std::string extension(const std::string& name);
38 
39 
40 struct FilePerm {
41  FilePerm(const std::string& n);
42 
43  std::string name;
44  char perms[10];
45  size_t size;
46 
47  static const int DirModeFileOffset;
48 };
49 
50 typedef std::vector<FilePerm> Files;
51 
52 Files listDir(const std::string& dir);
53 Strings listDirRel(const std::string& dir);
54 std::string listDir2str(const std::string& dir);
55 
56 std::string copyFromRemote(const std::string& file);
57 void copyToRemote(const std::string& rfile, const std::string& local);
58 void copyFile(const std::string& in, const std::string& out);
59 
60 typedef std::pair<std::string, int> FileInfo;
61 
62 FileInfo readFileInfo(const std::string& arg);
63 
65 bool fileStrFind(const std::string& line, const std::string& str);
66 
68 bool isCurrentOrParentDir(const std::string& dir);
69 
70 
72 class DirCache {
73  public:
75  static DirCache& getInstance();
76 
82  static void forceUpdateAt(const std::string& dir, const Files& fs);
83  static void forceUpdateAt(const std::string& dir, const std::string& res);
87  static Strings& getDirContents(const std::string& dir);
88 
89  private:
90  std::unordered_map<std::string, Strings> cache;
91 
92  DirCache(): cache() {}
93 
94  std::string noTrailingSlash(const std::string& dir);
95 }; // class DirCache
96 
97 
99 class FilesHist: public History {
100  public:
101  FilesHist(const std::string& file, int _max): History(file, _max) {}
102  void add(const std::string& file, int line);
103 
104  protected:
105  bool isDuplicate(const std::string& a, const std::string& b) const override;
106 }; // class FilesHist
107 
108 } // end namespace teditor
teditor::fileStrFind
bool fileStrFind(const std::string &line, const std::string &str)
Definition: file_utils.cpp:292
MESSAGE
#define MESSAGE(ed, fmt,...)
Definition: logger.h:50
teditor::OptionMap
std::unordered_map< std::string, std::string > OptionMap
Definition: editor.h:19
teditor::split
Strings split(const std::string &str, char delim)
Definition: utils.cpp:48
teditor::readFileInfo
FileInfo readFileInfo(const std::string &arg)
Definition: file_utils.cpp:274
teditor::listDir
Files listDir(const std::string &dir)
Definition: file_utils.cpp:202
teditor::StringChoices
Definition: cmd_msg_bar.h:38
teditor::FilePerm
Definition: file_utils.h:40
teditor::editor::ops::FileChoices
Definition: editor_ops.cpp:214
logger.h
teditor::str2num
int str2num(const std::string &str)
Definition: utils.cpp:73
teditor::slurp
std::string slurp(const std::string &file)
Definition: file_utils.cpp:81
teditor::isCurrentOrParentDir
bool isCurrentOrParentDir(const std::string &dir)
Definition: file_utils.cpp:302
teditor::strFind
bool strFind(const std::string &line, const std::string &str)
Definition: utils.cpp:237
teditor::Remote
Definition: file_utils.h:17
teditor::copyFromRemote
std::string copyFromRemote(const std::string &file)
Definition: file_utils.cpp:306
CMBAR_MSG
#define CMBAR_MSG(ed, fmt,...)
Definition: logger.h:52
teditor::FilePerm::perms
char perms[10]
Definition: file_utils.h:44
teditor::makeDir
void makeDir(const std::string &d)
Definition: file_utils.cpp:56
teditor::extension
std::string extension(const std::string &name)
Definition: file_utils.cpp:172
teditor::findFirstUpwards
std::string findFirstUpwards(const std::string &dir, const std::string &file)
Definition: file_utils.cpp:158
teditor::ChoicesFilter
bool(* ChoicesFilter)(const std::string &, const std::string &)
Definition: cmd_msg_bar.h:10
teditor::check_output
CmdStatus check_output(const std::string &cmd)
Definition: utils.cpp:110
teditor::FilePerm::name
std::string name
Definition: file_utils.h:43
teditor::urlHexify
std::string urlHexify(const std::string &url)
Hexify the input url.
Definition: utils.cpp:169
teditor::isDir
bool isDir(const std::string &f)
Definition: file_utils.cpp:34
teditor::listDirRel
Strings listDirRel(const std::string &dir)
Definition: file_utils.cpp:221
teditor::tempFileName
std::string tempFileName()
Definition: file_utils.cpp:148
teditor::DirCache
Definition: file_utils.h:72
teditor::editor::ops::FileChoices::getFinalStr
std::string getFinalStr(int idx, const std::string &str) const
Definition: editor_ops.cpp:226
teditor::Strings
std::vector< std::string > Strings
Definition: utils.h:42
teditor::Remote::Remote
Remote(const std::string &f)
Definition: file_utils.cpp:70
command.h
teditor::StringChoices::options
Strings options
Definition: cmd_msg_bar.h:46
DEF_OP
#define DEF_OP()
Definition: command.h:55
teditor::copyFile
void copyFile(const std::string &in, const std::string &out)
Definition: file_utils.cpp:257
utils.h
teditor::FilesHist::FilesHist
FilesHist(const std::string &file, int _max)
Definition: file_utils.h:101
teditor::downloadUrl
void downloadUrl(const std::string &url, const std::string &file, const std::string &dnldProg, const std::string &dnldProgOpts)
Download the content of input url into the given file.
Definition: utils.cpp:179
teditor::FileInfo
std::pair< std::string, int > FileInfo
Definition: file_utils.h:60
teditor::editor::ops::DEF_CMD
DEF_CMD(Quit, "quit", "editor_ops", DEF_OP() { ed.requestQuitEventLoop();})
teditor::DirCache::getInstance
static DirCache & getInstance()
Definition: file_utils.cpp:323
teditor::isReadOnly
bool isReadOnly(const char *f)
Definition: file_utils.cpp:52
teditor::FilesHist::add
void add(const std::string &file, int line)
Definition: file_utils.cpp:367
teditor::DirCache::getDirContents
static Strings & getDirContents(const std::string &dir)
Definition: file_utils.cpp:348
teditor::Remote::file
std::string file
Definition: file_utils.h:18
teditor::remoteFileType
std::string remoteFileType(const std::string &f)
Definition: file_utils.cpp:19
teditor::FilesHist::isDuplicate
bool isDuplicate(const std::string &a, const std::string &b) const override
Definition: file_utils.cpp:372
teditor::isRemoteDir
bool isRemoteDir(const std::string &type)
Definition: file_utils.cpp:30
ASSERT
#define ASSERT(check, fmt,...)
Macro to assert with runtime_error exception if the check fails.
Definition: utils.h:35
teditor::History::add
void add(const std::string &elem)
Definition: utils.cpp:285
file_utils.h
teditor::editor::ops::FileChoices::updateChoices
bool updateChoices(const std::string &str)
Definition: editor_ops.cpp:220
teditor::isAbs
bool isAbs(const std::string &file)
Definition: file_utils.cpp:116
teditor::getCmd
const Command & getCmd(const std::string &cmd)
Helper to return the command of interest.
Definition: command.cpp:23
teditor::basename
std::string basename(const std::string &file)
Definition: file_utils.cpp:133
teditor::editor::ops::FileChoices::FileChoices
FileChoices(const Strings &arr, ChoicesFilter cf=fileStrFind)
Definition: editor_ops.cpp:216
teditor::FilePerm::FilePerm
FilePerm(const std::string &n)
Definition: file_utils.cpp:181
teditor::History
Container to store history (files-visited or command-history, etc)
Definition: utils.h:115
teditor::FilePerm::DirModeFileOffset
static const int DirModeFileOffset
Definition: file_utils.h:47
teditor::Option::getStr
std::string getStr() const
Definition: option.cpp:26
teditor::StringChoices::at
const std::string & at(int idx) const
Definition: cmd_msg_bar.h:41
teditor::copyToRemote
void copyToRemote(const std::string &rfile, const std::string &local)
Definition: file_utils.cpp:315
teditor::rel2abs
std::string rel2abs(const std::string &pwd, const std::string &rel)
Definition: file_utils.cpp:118
net_utils.h
teditor::gitBranchName
std::string gitBranchName(const std::string &dir)
Definition: utils.cpp:99
teditor::FilesHist
Definition: file_utils.h:99
teditor::Remote::host
std::string host
Definition: file_utils.h:18
teditor::DirCache::forceUpdateAt
static void forceUpdateAt(const std::string &dir, const Files &fs)
Definition: file_utils.cpp:328
teditor::Option::get
static Option & get(const std::string &n)
Definition: option.cpp:40
teditor::slurpToArr
Strings slurpToArr(const std::string &file)
Definition: file_utils.cpp:97
option.h
teditor::Files
std::vector< FilePerm > Files
Definition: file_utils.h:50
editor.h
teditor::format
std::string format(const char *fmt, va_list &vl)
Definition: utils.cpp:22
teditor::dirname
std::string dirname(const std::string &file)
Definition: file_utils.cpp:139
teditor::FilePerm::size
size_t size
Definition: file_utils.h:45
teditor::getpwd
std::string getpwd()
Definition: file_utils.cpp:107
teditor::isFile
bool isFile(const std::string &f)
Definition: file_utils.cpp:43
CMBAR
#define CMBAR(ed, fmt,...)
Definition: logger.h:49
teditor::isRemote
bool isRemote(const std::string &f)
Definition: file_utils.cpp:79
teditor
Definition: any.hpp:10
teditor::listDir2str
std::string listDir2str(const std::string &dir)
Definition: file_utils.cpp:241
teditor::FileCompare
bool FileCompare(const FilePerm &a, const FilePerm &b)
Definition: file_utils.cpp:197