OSDN Git Service

catch file IO exceptions.
authorAiwota Programmer <aiwotaprog@tetteke.tk>
Sun, 21 Jun 2009 07:35:39 +0000 (16:35 +0900)
committerAiwota Programmer <aiwotaprog@tetteke.tk>
Sun, 21 Jun 2009 07:35:39 +0000 (16:35 +0900)
src/board_window.cxx
src/thread_idx_cache.cxx

index 403ca49..dad7202 100644 (file)
@@ -110,7 +110,7 @@ void BoardWindow::load() {
   std::vector<ThreadIdxCache> caches;
 
   const boost::filesystem::path dir(bbs_->get_board_idx_dir_path());
-  if (boost::filesystem::exists(dir) || !boost::filesystem::is_directory(dir))
+  if (boost::filesystem::exists(dir) && boost::filesystem::is_directory(dir))
     caches = dialektos::ThreadIdxCache::from_directory(dir);
 
   BOOST_FOREACH(const ThreadIdxCache& cache, caches) {
@@ -129,10 +129,22 @@ void BoardWindow::merge_subject_txt(
     boost::unordered_map<std::string, ModelColumns>& buffer) {
   const boost::filesystem::path sbj(bbs_->get_board_subject_path());
 
+  if (!boost::filesystem::exists(sbj) || !boost::filesystem::is_regular(sbj))
+    return;
+
   std::vector<SubjectItem> subjects;
   bbs_->load_subject(subjects);
 
-  const std::time_t last_modified = boost::filesystem::last_write_time(sbj);
+
+  // TODO use the last modified from a server.
+  boost::posix_time::ptime last_modified;
+  try {
+     std::time_t last_mod = boost::filesystem::last_write_time(sbj);
+     last_modified = boost::posix_time::from_time_t(last_mod);
+  } catch (const boost::filesystem::filesystem_error& e) {
+    std::cerr << e.what() << std::endl;
+    last_modified = boost::posix_time::second_clock::local_time();
+  }
 
   BOOST_FOREACH(const SubjectItem& item, subjects) {
     ModelColumns& cols = buffer[item.id_];
@@ -141,13 +153,17 @@ void BoardWindow::merge_subject_txt(
     model_column::field<model_column::ID>(cols) = item.id_;
     model_column::field<model_column::ResNum>(cols) = item.res_num_;
 
-    const std::time_t _start = boost::lexical_cast<std::time_t>(item.id_);
-    const boost::posix_time::ptime start = boost::posix_time::from_time_t(_start);
-    const boost::posix_time::ptime curr = boost::posix_time::from_time_t(last_modified);
-    const boost::posix_time::time_duration dur = curr - start;
-    const double sec = dur.total_seconds();
-    const double average = item.res_num_ / (sec/60.0/60.0/24.0);
-    model_column::field<model_column::Average>(cols) = average;
+    try {
+      const std::time_t _start = boost::lexical_cast<std::time_t>(item.id_);
+      const boost::posix_time::ptime start = boost::posix_time::from_time_t(_start);
+      const boost::posix_time::ptime curr = last_modified;
+      const boost::posix_time::time_duration dur = curr - start;
+      const double sec = dur.total_seconds();
+      const double average = item.res_num_ / (sec/60.0/60.0/24.0);
+      model_column::field<model_column::Average>(cols) = average;
+    } catch(const boost::bad_lexical_cast& e) {
+      std::cerr << e.what() << std::endl;
+    }
   }
 }
 
index b5492d4..b0bfae4 100644 (file)
@@ -51,9 +51,9 @@ std::vector<ThreadIdxCache> ThreadIdxCache::from_xml(
     try {
       boost::archive::xml_iarchive ia(ifs);
       ia >> boost::serialization::make_nvp("ThreadIdxCache", cache_vector);
-    } catch (const boost::exception& /*e*/) {
+    } catch (const boost::archive::archive_exception& e) {
       // TODO thread safety.
-      std::cerr << "cannot load " << cache_xml.file_string() << std::endl;
+      std::cerr << "ThreadIdxCache::from_xml(): " << e.what() << std::endl;
     }
     ifs.close();
   }
@@ -63,12 +63,14 @@ std::vector<ThreadIdxCache> ThreadIdxCache::from_xml(
 
 void ThreadIdxCache::to_xml(const boost::filesystem::path& cache_xml,
     const std::vector<ThreadIdxCache>& cache) {
+  if (cache.empty()) return;
+
   std::ofstream ofs(cache_xml.file_string().c_str());
   try {
     boost::archive::xml_oarchive oa(ofs);
     oa << boost::serialization::make_nvp("ThreadIdxCache", cache);
-  } catch (const boost::exception& /*e*/) {
-    std::cerr << "cannot save " << cache_xml.file_string() << std::endl;
+  } catch (const boost::archive::archive_exception& e) {
+    std::cerr << "ThreadIdxCache::to_xml(): " << e.what() << std::endl;
   }
   ofs.close();
 }
@@ -103,17 +105,25 @@ boost::unordered_set<ThreadIdxCache::ThreadID> ThreadIdxCache::get_exist_ids(
 
   boost::unordered_set<ThreadID> exist_ids;
 
-  const boost::filesystem::directory_iterator it_end;
-  for (boost::filesystem::directory_iterator it(idx_dir); it != it_end; ++it) {
+  if (!boost::filesystem::exists(idx_dir) ||
+      !boost::filesystem::is_directory(idx_dir))
+    return exist_ids;
+
+  try {
+    const boost::filesystem::directory_iterator it_end;
+    for (boost::filesystem::directory_iterator it(idx_dir); it != it_end; ++it) {
 
-    const boost::filesystem::path leaf = it->path();
-    if (!boost::filesystem::is_regular_file(leaf)) continue;
+      const boost::filesystem::path leaf = it->path();
+      if (!boost::filesystem::is_regular_file(leaf)) continue;
 
-    const std::string filename = leaf.filename();
-    smatch what;
-    if (!regex_match(filename, what, regex)) continue;
-    const ThreadID id = what[1];
-    exist_ids.insert(id);
+      const std::string filename = leaf.filename();
+      smatch what;
+      if (!regex_match(filename, what, regex)) continue;
+      const ThreadID id = what[1];
+      exist_ids.insert(id);
+    }
+  } catch(const boost::filesystem::filesystem_error& e) {
+    std::cerr << e.what() << std::endl;
   }
   return exist_ids;
 }