fname.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. XCC Utilities and Library
  3. Copyright (C) 2000 Olaf van der Spek <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "stdafx.h"
  16. #include "fname.h"
  17. Cfname::Cfname(const string& s)
  18. {
  19. *this = string_view(s);
  20. }
  21. string Cfname::get_fname() const
  22. {
  23. return title + ext;
  24. }
  25. string Cfname::get_ftitle() const
  26. {
  27. return title;
  28. }
  29. string Cfname::get_fext() const
  30. {
  31. return ext;
  32. }
  33. string Cfname::get_path() const
  34. {
  35. return path;
  36. }
  37. string Cfname::get_all() const
  38. {
  39. return path + title + ext;
  40. }
  41. /*
  42. void Cfname::expand()
  43. {
  44. char t[MAX_PATH];
  45. _fullpath(t, get_all().c_str(), MAX_PATH);
  46. Cfname(<string>(t));
  47. }
  48. */
  49. Cfname GetModuleFileName(HMODULE hModule)
  50. {
  51. char s[MAX_PATH];
  52. if (GetModuleFileNameA(hModule, s, MAX_PATH))
  53. return Cfname(s);
  54. return {};
  55. }
  56. string get_temp_path()
  57. {
  58. char temp_dir[MAX_PATH];
  59. return GetTempPathA(MAX_PATH, temp_dir) ? temp_dir : ".\\";
  60. }
  61. string get_temp_fname(string path)
  62. {
  63. char temp_fname[MAX_PATH];
  64. return GetTempFileNameA(path.c_str(), "XCC", 0, temp_fname) ? temp_fname : "";
  65. }
  66. string get_temp_fname()
  67. {
  68. return get_temp_fname(get_temp_path());
  69. }
  70. void Cfname::set_title(string_view s)
  71. {
  72. title = s;
  73. }
  74. void Cfname::set_ext(string_view s)
  75. {
  76. ext = s;
  77. }
  78. void Cfname::use_default_ext(string_view s)
  79. {
  80. if (ext == "")
  81. ext = s;
  82. }
  83. void Cfname::set_path(string_view s)
  84. {
  85. path = s;
  86. if (!path.empty() && path[path.length() - 1] != '\\')
  87. path += '\\';
  88. }
  89. void Cfname::use_default_path(string_view s)
  90. {
  91. if (path.empty())
  92. set_path(s);
  93. }
  94. void Cfname::make_path()
  95. {
  96. if ((title + ext).empty())
  97. return;
  98. path += title + ext + '\\';
  99. title = ext = "";
  100. }
  101. bool Cfname::exists() const
  102. {
  103. HANDLE h;
  104. WIN32_FIND_DATAA d;
  105. h = FindFirstFileA(get_all().c_str(), &d);
  106. if (h == INVALID_HANDLE_VALUE)
  107. return false;
  108. FindClose(h);
  109. return true;
  110. }
  111. const Cfname& Cfname::operator=(string_view s)
  112. {
  113. long p1 = s.rfind('\\');
  114. long p2 = s.rfind('.');
  115. char t[MAX_PATH];
  116. if (p1 != string::npos)
  117. {
  118. //copy last \ also
  119. t[s.copy(t, p1 + 1)] = 0;
  120. path = t;
  121. }
  122. t[s.copy(t, p2 - p1 - 1, p1 + 1)] = 0;
  123. title = t;
  124. if (p2 != s.npos && p1 < p2)
  125. {
  126. t[s.copy(t, s.npos, p2)] = 0;
  127. ext = t;
  128. }
  129. return *this;
  130. }
  131. string operator+(const string& a, const Cfname& b)
  132. {
  133. return a + static_cast<string>(b);
  134. }
  135. int create_dir(const string& dir)
  136. {
  137. return !CreateDirectoryA(dir.c_str(), NULL);
  138. }
  139. void create_deep_dir(string dir, const string& name)
  140. {
  141. int a = 0;
  142. int b;
  143. while ((b = name.find_first_of("/\\", a)) != string::npos)
  144. {
  145. dir += '/' + name.substr(a, b - a);
  146. create_dir(dir);
  147. a = b + 1;
  148. }
  149. }
  150. int copy_file(string s, string d)
  151. {
  152. return !CopyFileA(s.c_str(), d.c_str(), false);
  153. }
  154. int delete_file(string fname)
  155. {
  156. return !DeleteFileA(fname.c_str());
  157. }
  158. int move_file(string s, string d)
  159. {
  160. return !MoveFileA(s.c_str(), d.c_str());
  161. }
  162. bool fname_filter(const string& fname, const string& filter)
  163. {
  164. size_t i;
  165. for (i = 0; i < filter.size(); i++)
  166. {
  167. char c = filter[i];
  168. if (c == '*')
  169. {
  170. if (filter.find('*', i + 1) == string::npos)
  171. {
  172. int j = fname.length() - filter.length() + 1;
  173. return j < 0 ? false : fname_filter(fname.substr(i + j), filter.substr(i + 1));
  174. }
  175. // for (int j = 0; j < min(fname.length(), filter.length()) - i; j++)
  176. for (size_t j = 0; j < fname.size(); j++)
  177. {
  178. if (fname_filter(fname.substr(i + j), filter.substr(i + 1)))
  179. return true;
  180. }
  181. return false;
  182. }
  183. if (c != '?' && c != fname[i])
  184. return false;
  185. }
  186. return fname.length() == i;
  187. }