pull/152/head
x86kernel 10 years ago
parent 21214f2860
commit a0a0a5eb6d

@ -47,16 +47,18 @@ public:
{
close();
std::list<std::string> dirs;
const char* mode = truncate ? "wb" : "ab";
_filename = fname;
for (int tries = 0; tries < open_tries; ++tries)
{
if (dir_check(fname))
if(!_dir_created)
{
get_dirlist(fname, dirs);
if(create_dirs(dirs))
break;
if (dir_check(fname))
{
get_dirlist(fname);
if(create_dirs())
break;
}
}
if(!os::fopen_s(&_fd, fname, mode))
@ -143,21 +145,26 @@ public:
std::string::size_type index = 0;
index = name.find("/", index);
if(index != std::string::npos)
if(index != std::string::npos)
{
if(index == 0)
_path_type = 1;
else
_path_type = 0;
return true;
}
return false;
}
//Get list contain directories name
void get_dirlist(const std::string &name, std::list<std::string>& dirs)
void get_dirlist(const std::string &name)
{
std::string directory;
std::string::size_type index, previndex = 0;
index = name.find("/", previndex);
if(!index)
dirs.push_back("/");
do {
if(index - previndex == 0)
@ -174,21 +181,37 @@ public:
}
//Create directories by referring to list
//Return 0 if success
bool create_dirs(std::list<std::string>& dirs)
std::string make_dirstring(int depth)
{
std::string origin_path, dirname;
std::string dirname;
std::list<std::string>::iterator li;
int r;
int i;
if(_path_type)
dirname = "/";
origin_path = os::getpwd();
for(i = 0, li = dirs.begin(); i <= depth; i++, li++)
dirname = dirname + *li + "/";
return dirname;
}
for(li = dirs.begin(); li != dirs.end(); ++li) {
dirname = *li;
//Create directories by referring to list
//Return 0 if success
bool create_dirs()
{
std::string dirname;
std::list<std::string>::iterator li;
int r, depth = 0;
for(li = dirs.begin(); li != dirs.end(); li++, depth++)
{
dirname = make_dirstring(depth);
r = os::_mkdir(dirname);
if(r)
{
#ifdef _WIN32
@ -198,13 +221,9 @@ public:
return r;
#endif
}
r = os::_chdir(dirname);
if(r) return r;
}
r = os::_chdir(origin_path);
if(r) return r;
_dir_created = 1;
return 0;
}
@ -213,6 +232,9 @@ public:
private:
FILE* _fd;
std::string _filename;
std::list<std::string> dirs;
bool _dir_created;
bool _path_type;
bool _force_flush;

@ -139,15 +139,6 @@ inline int get_lasterror()
#endif
}
inline std::string getpwd()
{
#ifdef _WIN32
//not yet
#else
return get_current_dir_name();
#endif
}
inline bool _mkdir(const std::string& dirname)
{
#ifdef _WIN32
@ -157,15 +148,6 @@ inline bool _mkdir(const std::string& dirname)
#endif
}
inline bool _chdir(const std::string& dirname)
{
#ifdef _WIN32
//not yet
#else
return chdir(dirname.c_str());
#endif
}
//fopen_s on non windows for writing
inline int fopen_s(FILE** fp, const std::string& filename, const char* mode)
{

Loading…
Cancel
Save