52adb8d2152badc00d3a592efef7640b5c73497e
[platform/upstream/aspell.git] / common / file_data_util.cpp
1 #include "config.hpp"
2 #include "file_util.hpp"
3 #include "file_data_util.hpp"
4
5 namespace acommon {
6   
7   // Fill in values for a local data directory and a system data directory
8   // The values are stored under the keys local-data-dir and data-dir.
9   // If the is no local-data-dir value, use the directory from master-path.
10   // If there is no directory in master-path, use the current working dir.
11   // FIXME: The case when there is no "/" in the master-path should not
12   //   happen since it is an internal option.  Unofficially, it can still
13   //   be set by the user.  This needs to eventually be fixed.
14   void fill_data_dir(const Config * config, String & dir1, String & dir2) {
15     if (config->have("local-data-dir")) {
16       dir1 = config->retrieve("local-data-dir");
17       if (dir1[dir1.size()-1] != '/') dir1 += '/';
18     } else {
19       dir1 = config->retrieve("master-path");
20       size_t pos = dir1.rfind('/');
21       if (pos != String::npos)
22         dir1.resize(pos + 1);
23       else
24         dir1 = "./";
25     }
26     dir2 = config->retrieve("data-dir");
27     if (dir2[dir2.size()-1] != '/') dir2 += '/';
28   }
29   
30   const String & find_file(String & file,
31                            const String & dir1, const String & dir2, 
32                            const String & name, const char * extension)
33   {
34     file = dir1 + name + extension;
35     if (file_exists(file)) return dir1;
36     file = dir2 + name + extension;
37     return dir2;
38   }
39
40   bool find_file(String & file,
41                  const String & dir1, const String & dir2, 
42                  const String & name, 
43                  ParmString preext, ParmString ext)
44   {
45     bool try_name_only = false;
46     if (name.size() > ext.size() 
47         && memcmp(name.c_str() + name.size() - ext.size(), 
48                   ext, ext.size()) == 0) try_name_only = true;
49     if (!try_name_only) {
50       String n = name; n += preext; n += ext;
51       file = dir1 + n;
52       if (file_exists(file)) return true;
53       file = dir2 + n;
54       if (file_exists(file)) return true;
55
56       n = name; n += ext;
57       file = dir1 + n;
58       if (file_exists(file)) return true;
59       file = dir2 + n;
60       if (file_exists(file)) return true;
61     }
62
63     file = dir1 + name;
64     if (file_exists(file)) return true;
65     file = dir2 + name;
66     if (file_exists(file)) return true;
67
68     if (try_name_only) {file = dir1 + name;}
69     else               {file = dir1 + name; file += preext; file += ext;}
70     
71     return false;
72   }
73   
74   
75 }