Programming tasks to Scientific Computing I
aol.cpp
Go to the documentation of this file.
1 #include <aol.h>
2 
3 namespace aol {
4 
5 string strprintf(const char * format, ...) {
6  // declare variable argument list
7  va_list az;
8  // copy from my input into this list(second argument is nothing really used, but the last named argument of myself)
9  va_start(az, format);
10  // give this argument list variable to vscprintf instead of my own arguments (that is in what functions like vsprintf differ from functions like sprintf)
11  const int sizeNeeded = vsnprintf ( NULL, 0, format, az ) + 1;
12  // restore stack into clean state:
13  va_end(az);
14 
15  char *buffer = new char[sizeNeeded];
16 
17  va_start(az, format);
18  vsprintf (buffer, format, az);
19  va_end(az);
20 
21  // automatic return type conversion into string:
22  string ret = buffer;
23  delete[] buffer;
24  return ret;
25 }
26 
27 void makeDirectory ( const char *DirectoryName, bool verbose ) {
28  struct stat directory;
29  const int statReturn = stat ( DirectoryName, &directory );
30  if ( ( statReturn == - 1 ) || !S_ISDIR ( directory.st_mode ) ) {
31  string systemCommand = "mkdir \"";
32  systemCommand += DirectoryName;
33  systemCommand += "\"";
34  if ( system ( systemCommand.c_str() ) != EXIT_SUCCESS )
35  cerr << "aol::makeDirectory: Calling '" << systemCommand << "' returned an error.\n";
36  if ( verbose )
37  cerr << "Created directory " << DirectoryName << endl;
38  } else {
39  if ( verbose )
40  cerr << "Directory " << DirectoryName << " already exists\n";
41  }
42 }
43 
44 bool fileExists ( string filename ) {
45  struct stat buf;
46  return !stat ( filename.c_str (), &buf ) && S_ISREG ( buf.st_mode );
47 }
48 
49 } // namespace aol
bool fileExists(string filename)
Definition: aol.cpp:44
string strprintf(const char *format,...)
Give back formatted string, analogously to sprintf, but save the long way &#39;round with char arrays...
Definition: aol.cpp:5
void makeDirectory(const char *DirectoryName, bool verbose)
Definition: aol.cpp:27
Definition: aol.cpp:3