sp_dir.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. #include "precompile.h"
  2. #include <time.h>
  3. #include "sp_dir.h"
  4. #include "sp_def.h"
  5. #include "shm_mem.h"
  6. #include "fileutil.h"
  7. int sp_dir_create(sp_dir_t **p_dir)
  8. {
  9. sp_dir_t *dir;
  10. int rc = Error_Unexpect;
  11. char tmp[MAX_PATH];
  12. dir = shm_malloc(sizeof(sp_dir_t));
  13. memset(dir, 0, sizeof(sp_dir_t));
  14. //TODO(Gifur): duplicate path value set, which would makd memory leak
  15. GetModuleFileNameA(NULL, tmp, MAX_PATH);
  16. *strrchr(tmp, SPLIT_SLASH) = 0;
  17. dir->bin_path = shm_strdup(tmp);
  18. *strrchr(tmp, SPLIT_SLASH) = 0;
  19. dir->base_path = shm_strdup(tmp);
  20. *strrchr(tmp, SPLIT_SLASH) = 0;
  21. dir->root_ver_path = shm_strdup(tmp);
  22. *strrchr(tmp, SPLIT_SLASH) = 0;
  23. dir->root_path = shm_strdup(tmp);
  24. *strrchr(tmp, SPLIT_SLASH) = 0;
  25. dir->rvc_path = shm_strdup_printf("%s" SPLIT_SLASH_STR "rvc", tmp);
  26. //TODO: duplicate
  27. dir->root_ver_path = shm_strdup_printf("%s" SPLIT_SLASH_STR "version", dir->root_path);
  28. dir->root_hardwarecfg_path = shm_strdup_printf("%s" SPLIT_SLASH_STR "hardwarecfg", dir->root_path);
  29. dir->root_runinfo_path = shm_strdup_printf("%s" SPLIT_SLASH_STR "runinfo", dir->root_path);
  30. dir->dep_path = shm_strdup_printf("%s" SPLIT_SLASH_STR "dep", dir->base_path);
  31. dir->cfg_path = shm_strdup_printf("%s" SPLIT_SLASH_STR "cfg", dir->base_path);
  32. dir->mod_path = shm_strdup_printf("%s" SPLIT_SLASH_STR "mod", dir->base_path);
  33. dir->obj_path = shm_strdup_printf("%s" SPLIT_SLASH_STR "obj", dir->base_path);
  34. dir->dbg_path = shm_strdup_printf("%s" SPLIT_SLASH_STR "dbg", dir->rvc_path);
  35. dir->slv_path = shm_strdup_printf("%s" SPLIT_SLASH_STR "slv", dir->rvc_path);
  36. dir->dmp_path = shm_strdup_printf("%s" SPLIT_SLASH_STR "dmp", dir->rvc_path);
  37. dir->ad0_path = shm_strdup_printf("%s" SPLIT_SLASH_STR "ad0", dir->rvc_path);
  38. if(!ExistsDirA(dir->root_hardwarecfg_path))
  39. CreateDirRecursiveA(dir->root_hardwarecfg_path);
  40. if (!ExistsDirA(dir->rvc_path)) {
  41. CreateDirRecursiveA(dir->rvc_path);
  42. }
  43. if (!ExistsDirA(dir->dmp_path)) {
  44. CreateDirRecursiveA(dir->dmp_path);
  45. }
  46. if (!ExistsDirA(dir->dbg_path)) {
  47. CreateDirRecursiveA(dir->dbg_path);
  48. }
  49. if (!ExistsDirA(dir->slv_path)) {
  50. CreateDirRecursiveA(dir->slv_path);
  51. }
  52. memset(tmp, 0, sizeof(tmp));
  53. sprintf(tmp, "%s" SPLIT_SLASH_STR "runcfg", dir->root_runinfo_path);
  54. if (!ExistsDirA(tmp))
  55. CreateDirRecursiveA(tmp);
  56. *p_dir = dir;
  57. rc = 0;
  58. return rc;
  59. }
  60. void sp_dir_destroy(sp_dir_t* dir)
  61. {
  62. if (dir) {
  63. if (dir->base_path)
  64. shm_free(dir->base_path);
  65. if (dir->rvc_path)
  66. shm_free(dir->rvc_path);
  67. if (dir->bin_path)
  68. shm_free(dir->bin_path);
  69. if (dir->dep_path)
  70. shm_free(dir->dep_path);
  71. if (dir->cfg_path)
  72. shm_free(dir->cfg_path);
  73. if (dir->mod_path)
  74. shm_free(dir->mod_path);
  75. if (dir->obj_path)
  76. shm_free(dir->obj_path);
  77. if (dir->dbg_path)
  78. shm_free(dir->dbg_path);
  79. if (dir->slv_path)
  80. shm_free(dir->slv_path);
  81. if (dir->dmp_path)
  82. shm_free(dir->dmp_path);
  83. if (dir->root_path)
  84. shm_free(dir->root_path);
  85. if (dir->root_hardwarecfg_path)
  86. shm_free(dir->root_hardwarecfg_path);
  87. if (dir->root_runinfo_path)
  88. shm_free(dir->root_runinfo_path);
  89. if (dir->root_ver_path)
  90. shm_free(dir->root_ver_path);
  91. shm_free(dir);
  92. }
  93. }
  94. static int __dir_get_path(char *base_dir, int flag, const char *cat_name, char *buf, int len, const char *site, int mode)
  95. {
  96. int rc = 0;
  97. int needed = strlen(base_dir);
  98. if (flag == SP_DIR_ENTITY_INI) {
  99. needed += strlen(cat_name) + 6;
  100. } else if (flag == SP_DIR_SHELL_INI) {
  101. needed += 11;
  102. } else if (flag == SP_DIR_SHELLVAR_INI) {
  103. needed += 22;
  104. } else if (flag == SP_DIR_MODULE_BIN) {
  105. needed += strlen(cat_name) + 6;
  106. } else if (flag == SP_DIR_ROOT_INI) {
  107. needed += strlen("root.ini") + 10;
  108. } else if (flag == SP_DIR_DEVICE_ENTITY_INI) {
  109. needed += strlen(cat_name) + 6;
  110. } else if (flag == SP_DIR_RUNINFO_INI) {
  111. needed += strlen(cat_name) + 6 + 7;
  112. } else if (flag == SP_DIR_RUNINFO_GLOBAL_INI) {
  113. needed += strlen("Global.ini") + 2;
  114. } else if (flag == SP_DIR_RUNINFO_BOOT_LOG) {
  115. needed += strlen(cat_name) + 6 + 8;
  116. } else if (flag == SP_DIR_INSTALL_INI) {
  117. needed += 13;
  118. } else if (flag == SP_DIR_EXPLORER_INI) {
  119. needed += 16;
  120. } else if (flag == SP_DIR_CENTER_SETTING_INI){
  121. needed += 18;
  122. } else if (flag == SP_DIR_ENTITY_DBG_LOG) {
  123. needed += strlen(cat_name) + 2 + 4 + 8;
  124. }
  125. else if (flag == SP_DIR_ENV_INI) {
  126. needed += strlen("env.ini") + 10;
  127. }
  128. else {
  129. rc = Error_Param;
  130. }
  131. if (buf) {
  132. if (len && len < needed)
  133. return Error_TooSmallBuffer;
  134. } else {
  135. return needed;
  136. }
  137. if (flag == SP_DIR_ENTITY_INI || flag == SP_DIR_DEVICE_ENTITY_INI)
  138. {
  139. strcpy(buf, base_dir);
  140. strcat(buf, SPLIT_SLASH_STR);
  141. strcat(buf, cat_name);
  142. strcat(buf, ".ini");
  143. }
  144. else if (flag == SP_DIR_RUNINFO_INI)
  145. {
  146. strcpy(buf, base_dir);
  147. strcat(buf, SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR);
  148. strcat(buf, cat_name);
  149. strcat(buf, ".ini");
  150. }
  151. else if (flag == SP_DIR_RUNINFO_GLOBAL_INI) {
  152. strcpy(buf, base_dir);
  153. strcat(buf, SPLIT_SLASH_STR "Global.ini");
  154. }
  155. else if (flag == SP_DIR_RUNINFO_BOOT_LOG) {
  156. strcpy(buf, base_dir);
  157. strcat(buf, SPLIT_SLASH_STR "bootrec" SPLIT_SLASH_STR);
  158. strcat(buf, cat_name);
  159. strcat(buf, ".log");
  160. } else if (flag == SP_DIR_ENTITY_DBG_LOG ) {
  161. time_t timep;
  162. struct tm *p;
  163. char td[128] = { 0 };
  164. time(&timep);
  165. p = gmtime(&timep);
  166. sprintf(td, "%04d%02d%02d.log", 1900+p->tm_year, p->tm_mon+1, p->tm_mday);
  167. strcpy(buf, base_dir);
  168. strcat(buf, SPLIT_SLASH_STR);
  169. strcat(buf, cat_name);
  170. strcat(buf, SPLIT_SLASH_STR);
  171. strcat(buf, td);
  172. }
  173. else if (flag == SP_DIR_SHELLVAR_INI){
  174. strcpy(buf, base_dir);
  175. strcat(buf, SPLIT_SLASH_STR "runcfg" SPLIT_SLASH_STR "shellVar.ini");
  176. }else if (flag == SP_DIR_SHELL_INI) {
  177. strcpy(buf, base_dir);
  178. strcat(buf, SPLIT_SLASH_STR "shell.ini");
  179. }
  180. else if (flag == SP_DIR_ENV_INI) {
  181. strcpy(buf, base_dir);
  182. strcat(buf, SPLIT_SLASH_STR "env.ini");
  183. }
  184. else if (flag == SP_DIR_MODULE_BIN) {
  185. strcpy(buf, base_dir);
  186. strcat(buf, SPLIT_SLASH_STR);
  187. #if defined(RVC_OS_WIN)
  188. strcat(buf, cat_name);
  189. strcat(buf, ".dll");
  190. #else
  191. strcat(buf, "lib");
  192. strcat(buf, cat_name);
  193. strcat(buf, ".so");
  194. #endif //RVC_OS_WIN
  195. } else if (flag == SP_DIR_ROOT_INI) {
  196. strcpy(buf, base_dir);
  197. if(mode == 1) strcat(buf, SPLIT_SLASH_STR "root-ST.ini");
  198. else if(mode == 2) strcat(buf, SPLIT_SLASH_STR "root-UAT.ini");
  199. else strcat(buf, SPLIT_SLASH_STR "root.ini");
  200. } else if (flag == SP_DIR_INSTALL_INI) {
  201. strcpy(buf, base_dir);
  202. strcat(buf, SPLIT_SLASH_STR "install.ini");
  203. }
  204. else if (flag == SP_DIR_CENTER_SETTING_INI)
  205. {
  206. strcpy(buf, base_dir);
  207. #if defined(_MSC_VER)
  208. strcat(buf, SPLIT_SLASH_STR "CenterSetting.ini");
  209. #else
  210. strcat(buf, SPLIT_SLASH_STR "CenterSetting.LAN.ini");
  211. #endif //_MSC_VER
  212. }
  213. return rc;
  214. }
  215. int sp_dir_get_path(sp_dir_t* dir, int flag, const char *cat_name, char *buf, int len)
  216. {
  217. return sp_dir_get_path_new(dir, flag, cat_name, buf, len, 0);
  218. }
  219. int sp_dir_get_path_new(sp_dir_t* dir, int flag, const char *cat_name, char *buf, int len, int mode)
  220. {
  221. char* base_path = NULL;
  222. switch (flag)
  223. {
  224. case SP_DIR_ROOT_INI:
  225. case SP_DIR_DEVICE_ENTITY_INI:
  226. base_path = dir->root_hardwarecfg_path;
  227. break;
  228. case SP_DIR_SHELL_INI:
  229. case SP_DIR_ENTITY_INI:
  230. case SP_DIR_CENTER_SETTING_INI:
  231. case SP_DIR_ENV_INI:
  232. base_path = dir->cfg_path;
  233. break;
  234. case SP_DIR_MODULE_BIN:
  235. base_path = dir->mod_path;
  236. break;
  237. case SP_DIR_RUNINFO_INI:
  238. case SP_DIR_SHELLVAR_INI:
  239. case SP_DIR_RUNINFO_GLOBAL_INI:
  240. case SP_DIR_RUNINFO_BOOT_LOG:
  241. base_path = dir->root_runinfo_path;
  242. break;
  243. case SP_DIR_INSTALL_INI:
  244. base_path = dir->base_path;
  245. break;
  246. case SP_DIR_EXPLORER_INI:
  247. base_path = dir->root_path;
  248. break;
  249. case SP_DIR_ENTITY_DBG_LOG:
  250. base_path = dir->dbg_path;
  251. break;
  252. default:
  253. return Error_Param;
  254. }
  255. return __dir_get_path(base_path, flag, cat_name, buf, len, NULL, mode);
  256. }
  257. int sp_dir_get_path_version(sp_dir_t* dir, int major, int minor, int revision, int build, int flag, const char *cat_name, char *buf, int len, int mode)
  258. {
  259. char tmp[MAX_PATH];
  260. char *base_path = NULL;
  261. if (flag == SP_DIR_ROOT_INI || flag == SP_DIR_DEVICE_ENTITY_INI) {
  262. base_path = dir->root_hardwarecfg_path;
  263. } else if (flag == SP_DIR_RUNINFO_INI || flag == SP_DIR_RUNINFO_BOOT_LOG) {
  264. base_path = dir->root_runinfo_path;
  265. } else if (flag == SP_DIR_SHELL_INI || flag == SP_DIR_ENTITY_INI) {
  266. sprintf(tmp, "%s" SPLIT_SLASH_STR "%d.%d.%d.%d" SPLIT_SLASH_STR "cfg", dir->root_ver_path, major, minor, revision, build);
  267. base_path = tmp;
  268. } else if (flag == SP_DIR_MODULE_BIN) {
  269. sprintf(tmp, "%s" SPLIT_SLASH_STR "%d.%d.%d.%d" SPLIT_SLASH_STR "mod", dir->root_ver_path, major, minor, revision, build);
  270. base_path = tmp;
  271. } else if (flag == SP_DIR_INSTALL_INI) {
  272. sprintf(tmp, "%s" SPLIT_SLASH_STR "%d.%d.%d.%d", dir->root_ver_path, major, minor, revision, build);
  273. base_path = tmp;
  274. } else {
  275. return Error_Param;
  276. }
  277. return __dir_get_path(base_path, flag, cat_name, buf, len, NULL, mode);
  278. }
  279. void sp_dir_get_cur_drive(char* path)
  280. {
  281. char drive[_MAX_DRIVE] = {'\0'};
  282. char dir[_MAX_DIR] = { '\0' };
  283. char fname[_MAX_FNAME] = { '\0' };
  284. char ext[_MAX_EXT] = { '\0' };
  285. char chpath[MAX_PATH] = { '\0' };
  286. GetModuleFileNameA(NULL, (LPSTR)chpath, sizeof(chpath));
  287. _splitpath(chpath, drive, dir, fname, ext);
  288. memcpy(path, drive, sizeof(drive));
  289. }