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; }
void file_utils::combine_path_and_extension(dynamic_string &dst, const char *pA, const char *pB, const char *pC, const char *pExt) { combine_path(dst, pA, pB, pC); if ((!dst.ends_with(".")) && (pExt[0]) && (pExt[0] != '.')) dst.append_char('.'); dst.append(pExt); }
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 get_command_line(dynamic_string& cmd_line, int argc, char *argv[]) { argc, argv; #if CRNLIB_USE_WIN32_API cmd_line.set(GetCommandLineA()); #else cmd_line.clear(); for (int i = 0; i < argc; i++) { dynamic_string tmp(argv[i]); if ((tmp.front() != '"') && (tmp.front() != '-') && (tmp.front() != '@')) tmp = "\"" + tmp + "\""; if (cmd_line.get_len()) cmd_line += " "; cmd_line += tmp; } #endif }
void get_command_line_as_single_string(dynamic_string& cmd_line, int argc, char *argv[]) { argc, argv; #if false && CRNLIB_USE_WIN32_API // THIS DOES NOT WORK PROPERLY, ADDING TOO MANY QUOTES, SO WE IGNORE IT. cmd_line.set(GetCommandLineA()); #else cmd_line.clear(); for (int i = 0; i < argc; i++) { dynamic_string tmp(argv[i]); if ((tmp.front() != '"') && (tmp.front() != '-') && (tmp.front() != '@')) tmp = "\"" + tmp + "\""; if (cmd_line.get_len()) cmd_line += " "; cmd_line += tmp; } #endif }
bool file_utils::add_default_extension(dynamic_string &path, const char *pExt) { dynamic_string ext(path); get_extension(ext); if (ext.is_empty()) { path.append(pExt); return true; } return false; }
bool file_utils::remove_extension(dynamic_string &filename) { int sep = -1; #if defined(PLATFORM_WINDOWS) int rightmost_backslash = filename.find_right('\\'); int rightmost_forwardslash = filename.find_right('/'); sep = VOGL_MAX(rightmost_backslash, rightmost_forwardslash); #endif if (sep < 0) sep = filename.find_right('/'); int dot = filename.find_right('.'); if (dot < sep) return false; filename.left(dot); return true; }
bool command_line_params::get_value_as_string(const char* pKey, uint index, dynamic_string& value, uint value_index) const { param_map_const_iterator it = get_param(pKey, index); if ((it == end()) || (value_index >= it->second.m_values.size())) { value.empty(); return false; } value = it->second.m_values[value_index]; return true; }
void file_utils::combine_path(dynamic_string &dst, const char *pA, const char *pB) { dynamic_string temp(pA); if ((!temp.is_empty()) && (!is_path_separator(pB[0]))) { char c = temp[temp.get_len() - 1]; if (!is_path_separator(c)) temp.append_char(VOGL_PATH_SEPERATOR_CHAR); } temp += pB; dst.swap(temp); }
bool ktx_texture::get_key_value_as_string(const char *pKey, dynamic_string &str) const { const uint8_vec *p = find_key(pKey); if (!p) { str.clear(); return false; } const uint32_t ofs = vogl_strlen(pKey) + 1; const uint8_t *pValue = p->get_ptr() + ofs; const uint32_t n = p->size() - ofs; uint32_t i; for (i = 0; i < n; i++) if (!pValue[i]) break; str.set_from_buf(pValue, i); return true; }
bool data_stream::read_line(dynamic_string &str) { str.empty(); for (;;) { const int c = read_byte(); const bool prev_got_cr = m_got_cr; m_got_cr = false; if (c < 0) { if (!str.is_empty()) break; return false; } // ignores DOS EOF, assumes it's at the very end of the file else if ((26 == c) || (!c)) continue; else if (13 == c) { m_got_cr = true; break; } else if (10 == c) { if (prev_got_cr) continue; break; } str.append_char(static_cast<char>(c)); } return true; }
bool data_stream::read_line(dynamic_string &str) { str.empty(); for (;;) { // Can't be const--need to deal with Mac OS9 style line endings via substition const int c = read_byte(); if (c < 0) { if (!str.is_empty()) break; return false; } // ignores DOS EOF, assumes it's at the very end of the file else if ((26 == c) || (!c)) continue; else if (c_CR == c) { // This path handles OS9 and Windows style line endings. // Check to see if the next character is a LF, if so eat that one too. if (c_LF == peek_byte()) { read_byte(); } break; } else if (c_LF == c) { // UNIX style line endings. break; } str.append_char(static_cast<char>(c)); } return true; }
bool command_line_params::get_value_as_string(dynamic_string &value, const char *pKey, uint32_t key_index, const char *pDef, uint32_t value_index) const { param_map_const_iterator it = get_param(pKey, key_index); if (it == end()) return false; if (value_index >= it->second.m_values.size()) { vogl::console::debug("%s: Trying to retrieve value %u of command line parameter %s, but this parameter only has %u values\n", VOGL_FUNCTION_INFO_CSTR, value_index, pKey, it->second.m_values.size()); value.set(pDef); return false; } value = it->second.m_values[value_index]; return true; }
void file_utils::trim_trailing_seperator(dynamic_string &path) { if ((path.get_len()) && (is_path_separator(path.back()))) path.truncate(path.get_len() - 1); }
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); }