mirror of
git://projects.qi-hardware.com/gmenu2x.git
synced 2024-11-25 17:58:27 +02:00
Made text file reading more efficient and robust
Instead of reading the file line by line and then concatenating those lines, just load the entire thing in one go. And pay more attention to error conditions.
This commit is contained in:
parent
d0de870180
commit
485bab3b48
@ -409,45 +409,27 @@ void GMenu2X::initMenu() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void GMenu2X::about() {
|
void GMenu2X::about() {
|
||||||
string str, line;
|
string text(readFileAsString(GMENU2X_SYSTEM_DIR "/about.txt"));
|
||||||
string fn(GMENU2X_SYSTEM_DIR);
|
string build_date("Build date: " __DATE__);
|
||||||
string build_date("Build date: ");
|
TextDialog td(this, "GMenu2X", build_date, "icons/about.png", text);
|
||||||
fn.append("/about.txt");
|
|
||||||
build_date.append(__DATE__);
|
|
||||||
|
|
||||||
ifstream inf(fn.c_str(), ios_base::in);
|
|
||||||
|
|
||||||
while(getline(inf, line, '\n')) {
|
|
||||||
str.append(line).append("\n");
|
|
||||||
}
|
|
||||||
inf.close();
|
|
||||||
|
|
||||||
TextDialog td(this, "GMenu2X", build_date, "icons/about.png", str);
|
|
||||||
td.exec();
|
td.exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
void GMenu2X::viewLog() {
|
void GMenu2X::viewLog() {
|
||||||
string logfile = LOG_FILE;
|
string text(readFileAsString(LOG_FILE));
|
||||||
if (fileExists(logfile)) {
|
|
||||||
ifstream inf(logfile.c_str(), ios_base::in);
|
|
||||||
if (inf.is_open()) {
|
|
||||||
string str, line;
|
|
||||||
while (getline(inf, line, '\n')) {
|
|
||||||
str.append(line).append("\n");
|
|
||||||
}
|
|
||||||
inf.close();
|
|
||||||
|
|
||||||
TextDialog td(this, tr["Log Viewer"], tr["Displays last launched program's output"], "icons/ebook.png", str);
|
TextDialog td(this, tr["Log Viewer"],
|
||||||
td.exec();
|
tr["Displays last launched program's output"],
|
||||||
|
"icons/ebook.png", text);
|
||||||
|
td.exec();
|
||||||
|
|
||||||
MessageBox mb(this, tr["Do you want to delete the log file?"], "icons/ebook.png");
|
MessageBox mb(this, tr["Do you want to delete the log file?"],
|
||||||
mb.setButton(InputManager::ACCEPT, tr["Yes"]);
|
"icons/ebook.png");
|
||||||
mb.setButton(InputManager::CANCEL, tr["No"]);
|
mb.setButton(InputManager::ACCEPT, tr["Yes"]);
|
||||||
if (mb.exec() == InputManager::ACCEPT) {
|
mb.setButton(InputManager::CANCEL, tr["No"]);
|
||||||
unlink(logfile.c_str());
|
if (mb.exec() == InputManager::ACCEPT) {
|
||||||
menu->deleteSelectedLink();
|
unlink(LOG_FILE);
|
||||||
}
|
menu->deleteSelectedLink();
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -491,18 +491,9 @@ void LinkApp::showManual() {
|
|||||||
|
|
||||||
// Txt manuals
|
// Txt manuals
|
||||||
if (manual.substr(manual.size()-8,8)==".man.txt") {
|
if (manual.substr(manual.size()-8,8)==".man.txt") {
|
||||||
string str, line;
|
string text(readFileAsString(manual.c_str()));
|
||||||
ifstream infile(manual.c_str(), ios_base::in);
|
TextManualDialog tmd(gmenu2x, getTitle(), getIconPath(), text);
|
||||||
if (infile.is_open()) {
|
tmd.exec();
|
||||||
while (getline(infile, line, '\n')) {
|
|
||||||
str.append(line).append("\n");
|
|
||||||
}
|
|
||||||
infile.close();
|
|
||||||
|
|
||||||
TextManualDialog tmd(gmenu2x, getTitle(), getIconPath(), str);
|
|
||||||
tmd.exec();
|
|
||||||
}
|
|
||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,6 +56,31 @@ string rtrim(const string& s) {
|
|||||||
return e == string::npos ? "" : string(s, 0, e + 1);
|
return e == string::npos ? "" : string(s, 0, e + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// See this article for a performance comparison of different approaches:
|
||||||
|
// http://insanecoding.blogspot.com/2011/11/how-to-read-in-file-in-c.html
|
||||||
|
string readFileAsString(const char *filename) {
|
||||||
|
ifstream in(filename, ios::in | ios::binary);
|
||||||
|
if (!in) {
|
||||||
|
return "<error opening " + string(filename) + ">";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get file size.
|
||||||
|
in.seekg(0, ios::end);
|
||||||
|
auto size = max(int(in.tellg()), 0); // tellg() returns -1 on errors
|
||||||
|
in.seekg(0, ios::beg);
|
||||||
|
|
||||||
|
string contents(size, '\0');
|
||||||
|
in.read(&contents[0], contents.size());
|
||||||
|
in.close();
|
||||||
|
|
||||||
|
if (in.fail()) {
|
||||||
|
return "<error reading " + string(filename) + ">";
|
||||||
|
} else {
|
||||||
|
contents.shrink_to_fit();
|
||||||
|
return contents;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bool fileExists(const string &file) {
|
bool fileExists(const string &file) {
|
||||||
fstream fin;
|
fstream fin;
|
||||||
fin.open(file.c_str() ,ios::in);
|
fin.open(file.c_str() ,ios::in);
|
||||||
|
@ -46,6 +46,9 @@ std::string ltrim(const std::string& s);
|
|||||||
/** Returns the string with whitespace stripped from the end. */
|
/** Returns the string with whitespace stripped from the end. */
|
||||||
std::string rtrim(const std::string& s);
|
std::string rtrim(const std::string& s);
|
||||||
|
|
||||||
|
/** Returns the contents of the given file as a string. */
|
||||||
|
std::string readFileAsString(const char *filename);
|
||||||
|
|
||||||
std::string strreplace(std::string orig, const std::string &search, const std::string &replace);
|
std::string strreplace(std::string orig, const std::string &search, const std::string &replace);
|
||||||
std::string cmdclean(std::string cmdline);
|
std::string cmdclean(std::string cmdline);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user