New feature about dir

pull/152/head
x86kernel 10 years ago
parent 2460f97a20
commit 105d3a7c92

@ -133,23 +133,23 @@ constexpr inline unsigned short eol_size()
inline bool dir_check(const std::string& filename, std::list<std::string>& dirs) inline bool dir_check(const std::string& filename, std::list<std::string>& dirs)
{ {
#ifdef __linux__ #ifdef __linux__
std::string::size_type index, previndex = 0, size; std::string::size_type index, previndex = 0, size;
index = filename.find("/", previndex); index = filename.find("/", previndex);
if(!index) if(!index)
return false; return false;
do { do {
dirs.push_back(filename.substr(previndex, index)); dirs.push_back(filename.substr(previndex, index));
previndex = index + 1; previndex = index + 1;
index = filename.find("/", previndex); index = filename.find("/", previndex);
}while(index != std::string::npos); }while(index != std::string::npos);
size = filename.length() - 1; size = filename.length() - 1;
if(previndex < size) if(previndex < size)
dirs.push_back(filename.substr(previndex, size)); dirs.push_back(filename.substr(previndex, size));
return true; return true;
#endif #endif
@ -160,29 +160,29 @@ inline bool dir_check(const std::string& filename, std::list<std::string>& dirs)
inline bool create_dirs(std::list<std::string>& dirs) inline bool create_dirs(std::list<std::string>& dirs)
{ {
#ifdef __linux__ #ifdef __linux__
std::string origin_path; std::string origin_path;
std::list<std::string>::iterator li; std::list<std::string>::iterator li;
int r; int r;
origin_path = get_current_dir_name(); origin_path = get_current_dir_name();
for(li = dirs.begin(); li != dirs.end(); li++) { for(li = dirs.begin(); li != dirs.end(); li++) {
r = mkdir((*li).c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); r = mkdir((*li).c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if(r) if(r)
{ {
if(errno == EEXIST) if(errno == EEXIST)
return 0; return 0;
} }
else return r; else return r;
r = chdir((*li).c_str()); r = chdir((*li).c_str());
if(r) return r; if(r) return r;
} }
r = chdir(origin_path.c_str()); r = chdir(origin_path.c_str());
if(r) return r; if(r) return r;
return 0; return 0;
#endif #endif
@ -195,17 +195,18 @@ inline int fopen_s(FILE** fp, const std::string& filename, const char* mode)
*fp = _fsopen((filename.c_str()), mode, _SH_DENYWR); *fp = _fsopen((filename.c_str()), mode, _SH_DENYWR);
return *fp == nullptr; return *fp == nullptr;
#else #else
std::list<std::string> dirs; std::list<std::string> dirs;
if(dir_check(filename, dirs)) if(dir_check(filename, dirs))
{ {
dirs.pop_back(); dirs.pop_back();
if(create_dirs(dirs)) if(create_dirs(dirs))
return true; return true;
} }
*fp = fopen((filename.c_str()), mode); *fp = fopen((filename.c_str()), mode);
return *fp == nullptr; return *fp == nullptr;
#endif #endif
} }

Loading…
Cancel
Save