Enchant
Generic spell checking library
EnchantTestFixture.h
1 /* Copyright (c) 2007 Eric Scott Albright
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19  * THE SOFTWARE.
20  */
21 
22 #ifndef __ENCHANTTESTFIXTURE
23 #define __ENCHANTTESTFIXTURE
24 
25 #include <assert.h>
26 #include <glib.h>
27 #ifdef G_OS_UNIX
28 #include <fcntl.h> /* For creat; FIXME: should not be needed */
29 #endif
30 #include <glib/gstdio.h>
31 #include <sys/stat.h>
32 #include <unistd.h>
33 #include <stdlib.h>
34 #include <string.h>
35 
36 #include <string>
37 #include <locale>
38 #include <codecvt>
39 
40 #include "enchant.h"
41 #include "enchant-provider.h"
42 
44 {
45  //Setup
47  {
48  CleanUpFiles(); //just in case we stopped the process in the middle.
49  CreateDirectory(GetTempUserEnchantDir());
50  }
51 
52  //Teardown
54  CleanUpFiles();
55  }
56  void CleanUpFiles()
57  {
58  DeleteDirAndFiles(GetTempUserEnchantDir());
59  DeleteDirAndFiles(AddToPath(LIBDIR_SUBDIR, "enchant-" ENCHANT_MAJOR_VERSION));
60  DeleteDirAndFiles("share");
61  }
62 
63  std::string GetTempUserEnchantDir()
64  {
65  return getenv("ENCHANT_CONFIG_DIR");
66  }
67 
68  std::string GetEnchantConfigDir()
69  {
70  GSList *config_dirs = enchant_get_conf_dirs();
71  const char *pkgdatadir = (char *)g_slist_nth(config_dirs, 0)->data;
72  auto res = std::string(pkgdatadir);
73  g_slist_free_full(config_dirs, g_free);
74  return res;
75  }
76 
77  static void DeleteDirAndFiles(const std::string& dir)
78  {
79  GDir* gdir = g_dir_open(dir.c_str(), 0, NULL);
80  if(gdir != NULL)
81  {
82  const gchar* filename;
83  for(;;){
84  filename = g_dir_read_name(gdir);
85  if(filename == NULL)
86  {
87  break;
88  }
89  std::string filepath = AddToPath(dir, filename);
90  if(g_file_test(filepath.c_str(), G_FILE_TEST_IS_DIR)){
91  DeleteDirAndFiles(filepath);
92  }
93  else {
94  DeleteFile(filepath);
95  }
96  }
97  g_dir_close(gdir);
98  }
99  g_rmdir(dir.c_str());
100  }
101 
102  static std::string GetTemporaryFilename(const std::string & prefix){
103  char* tempFileName = tempnam(".", prefix.c_str());
104  std::string temp(tempFileName);
105  free(tempFileName);
106  return temp;
107  }
108 
109  static void CreateDirectory(const std::string& filepath)
110  {
111  g_mkdir_with_parents(filepath.c_str(), S_IRUSR | S_IWUSR | S_IXUSR);
112  }
113  static void CreateFile(const std::string& filepath)
114  {
115  int fh = g_creat(filepath.c_str(), S_IRUSR | S_IWUSR);
116  if(fh != -1) {
117  close(fh);
118  }
119  }
120  static void DeleteFile(const std::string& filepath)
121  {
122  if(FileExists(filepath)){
123  g_remove(filepath.c_str());
124  }
125  }
126  static bool FileExists(const std::string& filepath)
127  {
128  return(g_access(filepath.c_str(), 0)==0);
129  }
130 
131  std::string Convert(const std::wstring & ws)
132  {
133  std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> convert;
134  return convert.to_bytes(ws);
135  }
136 
137  std::wstring Convert(const std::string & s)
138  {
139  std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> convert;
140  return convert.from_bytes(s);
141  }
142 
143  static std::string AddToPath(const std::string & path, const std::string & fileOrDirName)
144  {
145  std::string result;
146  gchar* filename = g_build_filename(path.c_str(), fileOrDirName.c_str(), NULL);
147  result = std::string(filename);
148  g_free(filename);
149 
150  return result;
151  }
152 };
153 
154 #endif
Definition: EnchantTestFixture.h:44