aboutsummaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
Diffstat (limited to 'util')
-rw-r--r--util/fluxbox-update_configs.cc35
1 files changed, 26 insertions, 9 deletions
diff --git a/util/fluxbox-update_configs.cc b/util/fluxbox-update_configs.cc
index 43dd1f2..90a40d0 100644
--- a/util/fluxbox-update_configs.cc
+++ b/util/fluxbox-update_configs.cc
@@ -44,6 +44,8 @@
44 #include <string.h> 44 #include <string.h>
45#endif 45#endif
46 46
47#include <sys/stat.h>
48
47#include <iostream> 49#include <iostream>
48#include <fstream> 50#include <fstream>
49#include <set> 51#include <set>
@@ -63,8 +65,8 @@ using std::list;
63using std::exit; 65using std::exit;
64using std::getenv; 66using std::getenv;
65 67
66string read_file(string filename); 68string read_file(const string& filename);
67void write_file(string filename, string &contents); 69void write_file(const string& filename, const string &contents);
68void save_all_files(); 70void save_all_files();
69 71
70int run_updates(int old_version, FbTk::ResourceManager &rm) { 72int run_updates(int old_version, FbTk::ResourceManager &rm) {
@@ -455,18 +457,28 @@ int main(int argc, char **argv) {
455 return 0; 457 return 0;
456} 458}
457 459
458static set<string> modified_files; 460namespace {
461
462set<string> modified_files;
459// we may want to put a size limit on this cache, so it doesn't grow too big 463// we may want to put a size limit on this cache, so it doesn't grow too big
460static map<string,string> file_cache; 464map<string,string> file_cache;
465
466};
461 467
462// returns the contents of the file given, either from the cache or by reading 468// returns the contents of the file given, either from the cache or by reading
463// the file from disk 469// the file from disk
464string read_file(string filename) { 470string read_file(const string& filename) {
465 // check if we already have the file in memory 471 // check if we already have the file in memory
466 map<string,string>::iterator it = file_cache.find(filename); 472 map<string,string>::iterator it = file_cache.find(filename);
467 if (it != file_cache.end()) 473 if (it != file_cache.end())
468 return it->second; 474 return it->second;
469 475
476 stat s;
477 stat(filename.c_str(), &s);
478
479 if (! (s.st_mode & S_IFREG))
480 return "";
481
470 // nope, we'll have to read the file 482 // nope, we'll have to read the file
471 ifstream infile(filename.c_str()); 483 ifstream infile(filename.c_str());
472 string whole_file = ""; 484 string whole_file = "";
@@ -474,10 +486,15 @@ string read_file(string filename) {
474 if (!infile) // failed to open file 486 if (!infile) // failed to open file
475 return whole_file; 487 return whole_file;
476 488
489 string linebuffer;
477 while (!infile.eof()) { 490 while (!infile.eof()) {
478 string linebuffer;
479
480 getline(infile, linebuffer); 491 getline(infile, linebuffer);
492
493 // check if we read something at all. if its 0, its a strange file
494 // (eg a directory) or we are at the end
495 if (infile.gcount() == 0) {
496 break;
497 }
481 whole_file += linebuffer + "\n"; 498 whole_file += linebuffer + "\n";
482 } 499 }
483 infile.close(); 500 infile.close();
@@ -488,7 +505,7 @@ string read_file(string filename) {
488 505
489#ifdef NOT_USED 506#ifdef NOT_USED
490// remove the file from the cache, writing to disk if it's been changed 507// remove the file from the cache, writing to disk if it's been changed
491void forget_file(string filename) { 508void forget_file(const string& filename) {
492 map<string,string>::iterator cache_it = file_cache.find(filename); 509 map<string,string>::iterator cache_it = file_cache.find(filename);
493 // check if we knew about the file to begin with 510 // check if we knew about the file to begin with
494 if (cache_it == file_cache.end()) 511 if (cache_it == file_cache.end())
@@ -511,7 +528,7 @@ void forget_file(string filename) {
511 528
512// updates the file contents in the cache and marks the file as modified so it 529// updates the file contents in the cache and marks the file as modified so it
513// gets saved later 530// gets saved later
514void write_file(string filename, string &contents) { 531void write_file(const string& filename, const string &contents) {
515 modified_files.insert(filename); 532 modified_files.insert(filename);
516 file_cache[filename] = contents; 533 file_cache[filename] = contents;
517} 534}