1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- #include "sys.hpp" // for libcwd
- #include "debug.hpp" // for libcwd
- #include <sys/stat.h>
- #include <iostream>
- #include "stringutils.h"
- #include "filesystem.hpp"
- using namespace std;
- int fexist(const char *filename )
- {
- Dout(dc::utils, "fexist(const char *filename )");
- Dout(dc::utils, "filename=" << filename);
- struct stat buffer ;
- if ( stat( filename, &buffer ) == 0) return 1 ;
- return 0 ;
- }
- int FileSize(const char *filename )
- {
- struct stat buffer ;
- if ( stat( filename, &buffer ) != 0) {
- return 0 ;
- } else {
- return buffer.st_size;
- }
- }
- /** @brief create directory recursively
- *
- * usage: see MakeDirRecursively(const char *dirname)
- *
- *
- * @param Directory vector of hierarchy of subdirectories
- * @param true if absolute path, false if relative path
- * @return
- */
- bool MakeDirRecursively(vector<string> Directory, bool AbsolutePath)
- {
- string dirstring;
- if (AbsolutePath) {
- dirstring += "/";
- }
- for (vector<string>::iterator it=Directory.begin(); it!= Directory.end();++it) {
- dirstring += *it;
- dirstring += "/";
- }
- // cout << "Dirstring = "<< dirstring << "\n";
- if (mkdir(dirstring.c_str(),16877) != -1) {
- return true;
- } else {
- if (Directory.size() > 1) {
- vector<string> ParentDir(Directory);
- ParentDir.pop_back();
- if (MakeDirRecursively(ParentDir, AbsolutePath)) {
- return (mkdir(dirstring.c_str(),16877) != -1);
- }
- }
- }
- return false;
- }
- /** @brief create a directory. If parent directory doesn't exist, this will be created.
- *
- * @param dirname Name of directory to be created
- * @return
- */
- bool MakeDirRecursively(const char *dirname)
- {
- vector<string> Directory;
- bool AbsolutePath = ((dirname[0] == '/') || (dirname[0] == '\\'));
- Tokenize(dirname, Directory, "/\\");
- return MakeDirRecursively(Directory, AbsolutePath);
- }
|