void ktx_texture::get_keys(dynamic_string_array &keys) const { keys.resize(0); keys.reserve(m_key_values.size()); for (uint32_t i = 0; i < m_key_values.size(); i++) { const uint8_vec &v = m_key_values[i]; keys.enlarge(1)->set(reinterpret_cast<const char *>(v.get_ptr())); } }
bool command_line_params::load_string_file(const char *pFilename, dynamic_string_array &strings) { cfile_stream in_stream; if (!in_stream.open(pFilename, cDataStreamReadable | cDataStreamSeekable)) { console::error("Unable to open file \"%s\" for reading!\n", pFilename); return false; } dynamic_string ansi_str; for (;;) { if (!in_stream.read_line(ansi_str)) break; ansi_str.trim(); if (ansi_str.is_empty()) continue; strings.push_back(dynamic_string(ansi_str.get_ptr())); } return true; }
bool get_printable_backtrace(dynamic_string_array &strings) { strings.resize(0); console::debug("TODO: %s\n", VOGL_FUNCTION_INFO_CSTR); return false; }
bool get_printable_backtrace(dynamic_string_array &string_vec) { string_vec.resize(0); const int N = 128; uintptr_t pcs[N]; int n = backtrace_simple_get_pcs(pcs, N); if (!n) return false; VOGL_ASSERT(n <= N); if (n > N) return false; string_vec.reserve(n); for (int i = 0; i < n; i++) { stackframe_info info; utils::zero_object(info); if (backtrace_simple_resolve_pc(&info, pcs[i])) { string_vec.enlarge(1)->format("%u %s(%i), PC: 0x%llX, Ofs: 0x%llX, Mod: %s, Filename: \"%s\"", i, info.function ? demangle(info.function).get_ptr() : "?", info.linenumber, (uint64_t)info.pc, (uint64_t)info.offset, info.module ? info.module : "?", info.filename ? info.filename : "?"); } else { string_vec.push_back("?"); } } return true; }
bool get_printable_backtrace(dynamic_string_array &string_vec) { string_vec.resize(0); const uint32_t BUF_SIZE = 256; void *buffer[BUF_SIZE]; int nptrs = backtrace(buffer, BUF_SIZE); if (!nptrs) return false; char **strings = backtrace_symbols(buffer, nptrs); if (!strings) return false; string_vec.resize(nptrs); for (int i = 0; i < nptrs; i++) string_vec[i].set(strings[i] ? strings[i] : "?"); free(strings); return true; }
bool file_utils::read_text_file_crt(const char *pPath, dynamic_string_array &lines) { FILE *pFile = fopen(pPath, "r"); if (!pFile) return false; while (!feof(pFile)) { char buf[4096]; buf[0] = '\0'; fgets(buf, sizeof(buf), pFile); lines.push_back(buf); } fclose(pFile); return true; }
bool file_utils::write_text_file(const char *pPath, const dynamic_string_array &lines, bool unix_line_endings) { cfile_stream stream; if (!stream.open(pPath, cDataStreamWritable)) return false; for (uint i = 0; i < lines.size(); i++) { const dynamic_string &str = lines[i]; if (str.get_len()) stream.write(str.get_ptr(), str.get_len()); if (unix_line_endings) stream.write("\n", 1); else stream.write("\r\n", 2); } return !stream.get_error(); }
// This helper only works on files with valid file sizes (i.e. it won't work on some files under proc such as /proc/self/status). bool file_utils::read_text_file(const char *pPath, dynamic_string_array &lines, uint flags) { cfile_stream stream; if (!stream.open(pPath, cDataStreamReadable)) { if (flags & cRTFPrintErrorMessages) console::error("%s: Failed opening text file \"%s\" for reading\n", VOGL_FUNCTION_INFO_CSTR, pPath); else if (flags & cRTFPrintWarningMessages) console::warning("%s: Failed opening text file \"%s\" for reading\n", VOGL_FUNCTION_INFO_CSTR, pPath); return false; } int line_count = 0; dynamic_string line_str; while (stream.get_remaining()) { ++line_count; if (!stream.read_line(line_str)) { if (flags & cRTFPrintErrorMessages) console::error("%s: Failed reading from text file \"%s\"\n", VOGL_FUNCTION_INFO_CSTR, pPath); else if (flags & cRTFPrintWarningMessages) console::warning("%s: Failed reading from text file \"%s\"\n", VOGL_FUNCTION_INFO_CSTR, pPath); break; } if (flags & cRTFTrim) line_str.trim(); if (flags & cRTFTrimEnd) line_str.trim_end(); if (flags & cRTFIgnoreEmptyLines) { if (line_str.is_empty()) continue; } if ((flags & cRTFIgnoreCommentedLines) && (line_str.get_len() >= 2)) { bool found_comment = false; for (int i = 0; i < static_cast<int>(line_str.get_len()); ++i) { char c = line_str[i]; if ((c == ' ') || (c == '\t')) continue; else if ((c == '/') && (line_str[i + 1] == '/')) found_comment = true; break; } if (found_comment) continue; } lines.push_back(line_str); } return true; }
bool command_line_params::parse(const dynamic_string_array ¶ms, uint32_t total_param_descs, const command_line_param_desc *pParam_desc, const command_line_params::parse_config &config) { m_params = params; command_line_param_desc desc; desc.m_num_values = 0; desc.m_support_listing_file = false; desc.m_pName = ""; desc.m_pDesc = NULL; uint32_t arg_index = 0; while (arg_index < params.size()) { const uint32_t cur_arg_index = arg_index; const dynamic_string &src_param = params[arg_index++]; if (src_param.is_empty()) continue; bool is_param = false; uint32_t param_prefix_chars = 1; #if VOGL_CMD_LINE_ALLOW_SLASH_PARAMS is_param = (src_param[0] == '/'); #endif if ((src_param[0] == '-') && ((config.m_single_minus_params) || (config.m_double_minus_params))) { is_param = true; bool double_minus = (src_param[1] == '-'); if (double_minus) { if (config.m_double_minus_params) { param_prefix_chars = 2; } else { if (config.m_ignore_unrecognized_params) continue; console::error("Unrecognized command line parameter: \"%s\"\n", src_param.get_ptr()); return false; } } } if (is_param) { if (src_param.get_len() < (param_prefix_chars + 1)) { console::warning("Skipping invalid command line parameter: \"%s\"\n", src_param.get_ptr()); continue; } dynamic_string key_str(src_param); key_str.right(param_prefix_chars); if (config.m_pParam_ignore_prefix) { if (key_str.begins_with(config.m_pParam_ignore_prefix, true)) continue; } if (config.m_pParam_accept_prefix) { if (!key_str.begins_with(config.m_pParam_accept_prefix, true)) continue; } int modifier = 0; char c = key_str[key_str.get_len() - 1]; if (c == '+') modifier = 1; else if (c == '-') modifier = -1; if (modifier) key_str.left(key_str.get_len() - 1); if (total_param_descs) { uint32_t param_index; for (param_index = 0; param_index < total_param_descs; param_index++) if (key_str == pParam_desc[param_index].m_pName) break; if (param_index == total_param_descs) { if (config.m_ignore_unrecognized_params) continue; console::error("Unrecognized command line parameter: \"%s\"\n", src_param.get_ptr()); return false; } desc = pParam_desc[param_index]; } const uint32_t cMaxValues = 16; dynamic_string val_str[cMaxValues]; uint32_t num_val_strs = 0; if (desc.m_num_values) { VOGL_ASSERT(desc.m_num_values <= cMaxValues); if ((arg_index + desc.m_num_values) > params.size()) { console::error("Expected %u value(s) after command line parameter: \"%s\"\n", desc.m_num_values, src_param.get_ptr()); return false; } for (uint32_t v = 0; v < desc.m_num_values; v++) { val_str[num_val_strs++] = params[arg_index++]; } } dynamic_string_array strings; if ((desc.m_support_listing_file) && (val_str[0].get_len() >= 2) && (val_str[0][0] == '@')) { dynamic_string filename(val_str[0]); filename.right(1); filename.unquote(); if (!load_string_file(filename.get_ptr(), strings)) { console::error("Failed loading listing file \"%s\"!\n", filename.get_ptr()); return false; } } else { for (uint32_t v = 0; v < num_val_strs; v++) { val_str[v].unquote(); strings.push_back(val_str[v]); } } param_value pv; pv.m_values.swap(strings); pv.m_split_param_index = cur_arg_index; pv.m_modifier = (int8_t)modifier; m_param_map.insert(std::make_pair(key_str, pv)); } else if (!config.m_ignore_non_params) { if ((config.m_fail_on_non_params) && (cur_arg_index)) { console::error("Unrecognized command line argument: \"%s\"!\n", src_param.get_ptr()); return false; } param_value pv; pv.m_values.push_back(src_param); pv.m_values.back().unquote(); pv.m_split_param_index = cur_arg_index; m_param_map.insert(std::make_pair(get_empty_dynamic_string(), pv)); } } return true; }
bool split_command_line_params(const char *p, dynamic_string_array ¶ms) { bool within_param = false; bool within_quote = false; uint32_t ofs = 0; dynamic_string str; while (p[ofs]) { const char c = p[ofs]; if (within_param) { if (within_quote) { if (c == '"') within_quote = false; str.append_char(c); } else if ((c == ' ') || (c == '\t')) { if (!str.is_empty()) { params.push_back(str); str.clear(); } within_param = false; } else { if (c == '"') within_quote = true; str.append_char(c); } } else if ((c != ' ') && (c != '\t')) { within_param = true; if (c == '"') within_quote = true; str.append_char(c); } ofs++; } if (within_quote) { console::error("Unmatched quote in command line \"%s\"\n", p); return false; } if (!str.is_empty()) params.push_back(str); return true; }
bool command_line_params::parse(const dynamic_string_array& params, uint n, const param_desc* pParam_desc) { CRNLIB_ASSERT(n && pParam_desc); m_params = params; uint arg_index = 0; while (arg_index < params.size()) { const uint cur_arg_index = arg_index; const dynamic_string& src_param = params[arg_index++]; if (src_param.is_empty()) continue; #if CRNLIB_CMD_LINE_ALLOW_SLASH_PARAMS if ((src_param[0] == '/') || (src_param[0] == '-')) #else if (src_param[0] == '-') #endif { if (src_param.get_len() < 2) { console::error("Invalid command line parameter: \"%s\"", src_param.get_ptr()); return false; } dynamic_string key_str(src_param); key_str.right(1); int modifier = 0; char c = key_str[key_str.get_len() - 1]; if (c == '+') modifier = 1; else if (c == '-') modifier = -1; if (modifier) key_str.left(key_str.get_len() - 1); uint param_index; for (param_index = 0; param_index < n; param_index++) if (key_str == pParam_desc[param_index].m_pName) break; if (param_index == n) { console::error("Unrecognized command line parameter: \"%s\"", src_param.get_ptr()); return false; } const param_desc& desc = pParam_desc[param_index]; const uint cMaxValues = 16; dynamic_string val_str[cMaxValues]; uint num_val_strs = 0; if (desc.m_num_values) { CRNLIB_ASSERT(desc.m_num_values <= cMaxValues); if ((arg_index + desc.m_num_values) > params.size()) { console::error("Expected %u value(s) after command line parameter: \"%s\"", desc.m_num_values, src_param.get_ptr()); return false; } for (uint v = 0; v < desc.m_num_values; v++) val_str[num_val_strs++] = params[arg_index++]; } dynamic_string_array strings; if ((desc.m_support_listing_file) && (val_str[0].get_len() >= 2) && (val_str[0][0] == '@')) { dynamic_string filename(val_str[0]); filename.right(1); filename.unquote(); if (!load_string_file(filename.get_ptr(), strings)) { console::error("Failed loading listing file \"%s\"!", filename.get_ptr()); return false; } } else { for (uint v = 0; v < num_val_strs; v++) { val_str[v].unquote(); strings.push_back(val_str[v]); } } param_value pv; pv.m_values.swap(strings); pv.m_index = cur_arg_index; pv.m_modifier = (int8)modifier; m_param_map.insert(std::make_pair(key_str, pv)); } else { param_value pv; pv.m_values.push_back(src_param); pv.m_values.back().unquote(); pv.m_index = cur_arg_index; m_param_map.insert(std::make_pair(g_empty_dynamic_string, pv)); } } return true; }