Coverage Report

Created: 2024-09-08 06:23

/src/aspell/common/file_data_util.cpp
Line
Count
Source (jump to first uncovered line)
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
2.17k
  void fill_data_dir(const Config * config, String & dir1, String & dir2) {
15
2.17k
    if (config->have("local-data-dir")) {
16
0
      dir1 = config->retrieve("local-data-dir");
17
0
      if (!dir1.empty() && dir1[dir1.size()-1] != '/') dir1 += '/';
18
2.17k
    } else {
19
2.17k
      dir1 = config->retrieve("master-path");
20
2.17k
      size_t pos = dir1.rfind('/');
21
2.17k
      if (pos != String::npos)
22
2.17k
        dir1.resize(pos + 1);
23
0
      else
24
0
        dir1 = "./";
25
2.17k
    }
26
2.17k
    dir2 = config->retrieve("data-dir");
27
2.17k
    if (dir2[dir2.size()-1] != '/') dir2 += '/';
28
2.17k
  }
29
  
30
  const String & find_file(String & file,
31
                           const String & dir1, const String & dir2, 
32
                           const String & name, const char * extension)
33
2.90k
  {
34
2.90k
    file = dir1 + name + extension;
35
2.90k
    if (file_exists(file)) return dir1;
36
15
    file = dir2 + name + extension;
37
15
    return dir2;
38
2.90k
  }
39
40
  bool find_file(String & file,
41
                 const String & dir1, const String & dir2, 
42
                 const String & name, 
43
                 ParmString preext, ParmString ext)
44
13
  {
45
13
    bool try_name_only = false;
46
13
    if (name.size() > ext.size() 
47
13
        && memcmp(name.c_str() + name.size() - ext.size(), 
48
13
                  ext, ext.size()) == 0) try_name_only = true;
49
13
    if (!try_name_only) {
50
0
      String n = name; n += preext; n += ext;
51
0
      file = dir1 + n;
52
0
      if (file_exists(file)) return true;
53
0
      file = dir2 + n;
54
0
      if (file_exists(file)) return true;
55
56
0
      n = name; n += ext;
57
0
      file = dir1 + n;
58
0
      if (file_exists(file)) return true;
59
0
      file = dir2 + n;
60
0
      if (file_exists(file)) return true;
61
0
    }
62
63
13
    file = dir1 + name;
64
13
    if (file_exists(file)) return true;
65
0
    file = dir2 + name;
66
0
    if (file_exists(file)) return true;
67
68
0
    if (try_name_only) {file = dir1 + name;}
69
0
    else               {file = dir1 + name; file += preext; file += ext;}
70
    
71
0
    return false;
72
0
  }
73
  
74
  
75
}