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 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 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; }
// 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 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; }