private static void SaveCacheData() { BibliographerSettings settings; string datadir; settings = new BibliographerSettings("apps.bibliographer"); datadir = settings.GetString("data-directory"); Cleanup_invalid_dirs(); try { Monitor.Enter(sections); StreamWriter stream = new StreamWriter(new FileStream(datadir + "/cachedata", FileMode.OpenOrCreate, FileAccess.Write)); for (int section = 0; section < sections.Count; section++) { stream.WriteLine("[{0}]", ((CacheSection)sections[section]).section); for (int key = 0; key < ((CacheSection)sections[section]).keys.Count; key++) { stream.WriteLine("{0} {1}", ((KeySection)((CacheSection)sections[section]).keys[key]).key, ((KeySection)((CacheSection)sections[section]).keys[key]).filename); } } stream.Close(); Monitor.Exit(sections); } catch (DirectoryNotFoundException e) { WriteLine(10, e.Message); WriteLine(1, "Directory ~/.local/share/bibliographer/ not found! Creating it..."); Directory.CreateDirectory(datadir); } catch (Exception e) { WriteLine(1, "Unhandled exception whilst trying to save cache: {0}", e); } }
public static string AddToCache(string section, string key) { BibliographerSettings settings; KeySection kSection; CacheSection cSection; Random random; string datadir, filename; settings = new BibliographerSettings("apps.bibliographer"); kSection = LookupKey(section, key); if (kSection != null) { return(kSection.filename); } cSection = LookupSection(section); if (cSection == null) { // add a new section sections.Add(new CacheSection(section)); sections.Sort(new SectionCompare()); cSection = LookupSection(section); if (cSection == null) { WriteLine(5, "Failing to add a new section in cache, that's messed up... :-("); return(""); } } datadir = settings.GetString("data-directory"); random = new Random(); do { filename = datadir + "/cache/"; const string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; for (int i = 0; i < 20; i++) { filename = filename + chars[random.Next() % chars.Length]; } bool ok = false; try { FileStream stream = new FileStream(filename, FileMode.CreateNew, FileAccess.Write); stream.Close(); ok = true; } catch (DirectoryNotFoundException e) { WriteLine(10, e.Message); try { Directory.CreateDirectory(datadir); } catch (Exception e2) { WriteLine(10, e2.Message); } try { Directory.CreateDirectory(datadir + "/cache"); } catch (Exception e2) { WriteLine(10, e2.Message); WriteLine(1, "Failed to create directory {0}", datadir + "/cache"); } } catch (IOException e) { WriteLine(10, e.Message); // file already exists } if (ok) { break; } } while (true); cSection.keys.Add(new KeySection(key, filename)); cSection.keys.Sort(new KeyCompare()); SaveCacheData(); return(filename); }
private static void LoadCacheData() { BibliographerSettings settings; CacheSection curSection; StreamReader stream; string datadir; settings = new BibliographerSettings("apps.bibliographer"); datadir = settings.GetString("data-directory"); sections = new ArrayList(); try { stream = new StreamReader(new FileStream(datadir + "/cachedata", FileMode.Open, FileAccess.Read)); // cache opened! let's read some data... curSection = null; while (stream.Peek() > -1) { string line = stream.ReadLine(); if (line != "") { if (line[0] == '[') { // new section char[] splits = { '[', ']' }; string sectionName = line.Split(splits)[1]; // check that we don't already have this section name bool found = false; for (int i = 0; i < sections.Count; i++) { if (((CacheSection)sections[i]).section == sectionName) { found = true; break; } } if (found) { WriteLine(5, "Duplicate section {0} in cache file!", sectionName); curSection = null; continue; } if (curSection != null) { curSection.keys.Sort(new KeyCompare()); sections.Add(curSection); } curSection = new CacheSection(sectionName); } else { if (curSection == null) { // no active section, so skip continue; } string[] fields = line.Split(' '); curSection.keys.Add(new KeySection(fields[0], fields[1])); } } } stream.Close(); if (curSection != null) { sections.Add(curSection); } sections.Sort(new SectionCompare()); } catch (DirectoryNotFoundException e) { WriteLine(10, e.Message); WriteLine(1, "Directory ~/.local/share/bibliographer/ not found! Creating it..."); Directory.CreateDirectory(datadir); } catch (FileNotFoundException e) { WriteLine(10, e.Message); // no cache, no problem-o :-) } }