bool file_utils::full_path(dynamic_string &path) { #if defined(PLATFORM_WINDOWS) char buf[1024]; char *p = _fullpath(buf, path.get_ptr(), sizeof(buf)); if (!p) return false; #else char buf[PATH_MAX]; char *p; dynamic_string pn, fn; split_path(path.get_ptr(), pn, fn); if ((fn == ".") || (fn == "..")) { p = realpath(path.get_ptr(), buf); if (!p) return false; path.set(buf); } else { if (pn.is_empty()) pn = "./"; p = realpath(pn.get_ptr(), buf); if (!p) return false; combine_path(path, buf, fn.get_ptr()); } #endif return true; }
bool data_stream::write_line(const dynamic_string& str) { if (!str.is_empty()) return write(str.get_ptr(), str.get_len()) == str.get_len(); return true; }
bool file_utils::create_directories_from_full_path(const dynamic_string &fullpath) { bool got_unc = false; VOGL_NOTE_UNUSED(got_unc); dynamic_string cur_path; const int l = fullpath.get_len(); int n = 0; while (n < l) { const char c = fullpath.get_ptr()[n]; const bool sep = is_path_separator(c); const bool back_sep = is_path_separator(cur_path.back()); const bool is_last_char = (n == (l - 1)); if (((sep) && (!back_sep)) || (is_last_char)) { if ((is_last_char) && (!sep)) cur_path.append_char(c); bool valid = !cur_path.is_empty(); #if defined(PLATFORM_WINDOWS) // reject obvious stuff (drives, beginning of UNC paths): // c:\b\cool // \\machine\blah // \cool\blah if ((cur_path.get_len() == 2) && (cur_path[1] == ':')) valid = false; else if ((cur_path.get_len() >= 2) && (cur_path[0] == '\\') && (cur_path[1] == '\\')) { if (!got_unc) valid = false; got_unc = true; } else if (cur_path == "\\") valid = false; #endif if (cur_path == "/") valid = false; if (valid) { create_directory(cur_path.get_ptr()); } } cur_path.append_char(c); n++; } return true; }
bool file_utils::write_string_to_file(const char *pPath, const dynamic_string &str) { cfile_stream stream; if (!stream.open(pPath, cDataStreamWritable)) return false; if (str.get_len()) stream.write(str.get_ptr(), str.get_len()); return !stream.get_error(); }
void file_utils::combine_path(dynamic_string &dst, const char *pA, const char *pB, const char *pC) { combine_path(dst, pA, pB); combine_path(dst, dst.get_ptr(), pC); }