TF++, Miscellaneous classes and modules in C++:
filetest.cc
Go to the documentation of this file.
1 
35 #define FILETEST_VERSION \
36  "FILETEST V1.0 test file access functions"
37 
38 #include <iostream>
39 #include <string>
40 #include <tfxx/commandline.h>
41 #include <tfxx/filestatus.h>
42 
43 using std::cout;
44 using std::cerr;
45 using std::endl;
46 
47 void applytest(const bool& f, const char* a, const char* b)
48 {
49  cout << "The file " << a << " ";
50  if (!f) { cout << "NOT "; }
51  cout << b << endl;
52 }
53 
54 void testfile(const std::string& s)
55 {
56  cout << endl;
57  cout << "testing file " << s << endl;
58  applytest(tfxx::file::exists(s.c_str()), "does", "exist");
59  applytest(tfxx::file::regular(s.c_str()), "is", "regular");
60  applytest(tfxx::file::readable(s.c_str()), "is", "readable");
61  applytest(tfxx::file::writable(s.c_str()), "is", "writable");
62  applytest(tfxx::file::creatable(s.c_str()), "is", "creatable");
63  cout << "a unique new filename would be "
64  << tfxx::file::uniquenew(s.c_str()) << endl;
65 }
66 
67 /*======================================================================*/
68 
69 int main(int iargc, char* argv[])
70 {
71 
72  // define usage information
73  char usage_text[]=
74  {
75  FILETEST_VERSION "\n"
76  "usage: filetest" "\n"
77  " or: filetest --help|-h" "\n"
78  };
79 
80  // define full help text
81  char help_text[]=
82  {
83  FILETEST_CVSID
84  };
85 
86  // define commandline options
87  using namespace tfxx::cmdline;
88  static Declare options[]=
89  {
90  // 0: print help
91  {"help",arg_no,"-"},
92  // 1: verbose mode
93  {"v",arg_no,"-"},
94  {NULL}
95  };
96 
97  // no arguments? print usage...
98  if (iargc<2)
99  {
100  cerr << usage_text << endl;
101  exit(0);
102  }
103 
104  // collect options from commandline
105  Commandline cmdline(iargc, argv, options);
106 
107  // help requested? print full help text...
108  if (cmdline.optset(0))
109  {
110  cerr << usage_text << endl;
111  cerr << help_text << endl;
112  exit(0);
113  }
114 
115  /*
116  // dummy operation: print option settings
117  for (int iopt=0; iopt<2; iopt++)
118  {
119  cout << "option: '" << options[iopt].opt_string << "'" << endl;
120  if (cmdline.optset(iopt)) { cout << " option was set"; }
121  else { cout << "option was not set"; }
122  cout << endl;
123  cout << " argument (string): '" << cmdline.string_arg(iopt) << "'" << endl;
124  cout << " argument (int): '" << cmdline.int_arg(iopt) << "'" << endl;
125  cout << " argument (long): '" << cmdline.long_arg(iopt) << "'" << endl;
126  cout << " argument (float): '" << cmdline.float_arg(iopt) << "'" << endl;
127  cout << " argument (double): '" << cmdline.double_arg(iopt) << "'" << endl;
128  cout << " argument (bool): '";
129  if (cmdline.bool_arg(iopt))
130  { cout << "true"; } else { cout << "false"; }
131  cout << "'" << endl;
132  }
133  while (cmdline.extra()) { cout << cmdline.next() << endl; }
134  */
135 
136  while (cmdline.extra()) { testfile(cmdline.next()); }
137 }
138 
139 /* ----- END OF filetest.cc ----- */
bool writable(const char *path)
Definition: filestatus.cc:82
bool regular(const char *path)
Definition: filestatus.cc:103
void testfile(const std::string &s)
Definition: filetest.cc:54
bool creatable(const char *path)
Definition: filestatus.cc:56
bool optset(const int &iopt) const
true if option # iopt was set on commandline
Definition: commandline.h:213
std::string uniquenew(const char *path)
Definition: filestatus.cc:113
void applytest(const bool &f, const char *a, const char *b)
Definition: filetest.cc:47
bool readable(const char *path)
Definition: filestatus.cc:92
char * next()
returns char-array of next commandline argument
Definition: commandline.h:243
Namespace containing all components of module commandline. ,.
Definition: commandline.cc:41
bool extra() const
true if there are more commandline arguments
Definition: commandline.h:240
#define FILETEST_VERSION
Definition: filetest.cc:35
Evaluates commandline by calling long_getopt. ,You may instantiate a Commandline object by passing th...
Definition: commandline.h:201
bool exists(const char *path)
Definition: filestatus.cc:71
struct to define options ,This struct is used to define a list of options. An example is: ...
Definition: commandline.h:136
int main(int iargc, char *argv[])
Definition: filetest.cc:69
option has no argument
Definition: commandline.h:100