2
0

file.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * 文件名: file.c
  3. * 目标: 关于文件读取的实用函数
  4. */
  5. #include <sys/stat.h>
  6. #include <string.h>
  7. #include <ctype.h>
  8. #include <assert.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include "tool.h"
  12. #ifndef S_ISREG
  13. #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
  14. #endif
  15. #ifndef S_ISDIR
  16. #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
  17. #endif
  18. /*
  19. * 函数名: checkFile
  20. * 目标判断文件类型, 若是普通文件返回1, 若是文件夹返回2, 其他遇到错误返回0
  21. */
  22. int checkFile(char *path){
  23. struct stat my_stat;
  24. if (path == NULL || stat(path, &my_stat) != 0)
  25. return 0;
  26. if (S_ISREG(my_stat.st_mode)) // 普通文件
  27. return 1;
  28. else if (S_ISDIR(my_stat.st_mode))
  29. return 2;
  30. else
  31. return 0;
  32. }
  33. time_t getFileMTime(char *path) {
  34. struct stat my_stat;
  35. if (path == NULL || stat(path, &my_stat) != 0)
  36. return 0;
  37. return my_stat.st_mtime;
  38. }
  39. char *joinPath(char *path, char *name, char *suffix) {
  40. char *name_suffix = strJoin(name, suffix, false, false);
  41. char *res = NULL;
  42. if (path != NULL && path[STR_LEN(path) - 1] == SEP_CH)
  43. res = strJoin(path, name_suffix, false, true);
  44. else if (path != NULL) {
  45. res = strJoin(path, SEP, false, false);
  46. res = strJoin(res, name_suffix, true, true);
  47. } else
  48. res = name_suffix;
  49. /* name_suffix已经在上述的strJoin释放 */
  50. return res;
  51. }
  52. /*
  53. * 函数: getFileName
  54. * 目标: 给定路径获取该路径所指定的文件名
  55. */
  56. char *getFileName(char *path_1){
  57. char *slash = NULL; // 名字开始的字符的指针
  58. char *point = NULL; // 后缀名.所在的字符的指针
  59. char *path = strCopy(path_1); // 复制数组, 避免path_1是常量字符串导致无法修改其值
  60. if (path[STR_LEN(path) - 1] == SEP_CH) // 若路径的最后一个字符为SEP, 则忽略此SEP
  61. path[STR_LEN(path) - 1] = NUL;
  62. if ((slash = strrchr(path, SEP_CH)) == NULL)
  63. slash = path;
  64. else
  65. slash++;
  66. if ((point = getFileSurfix(path)) != NULL)
  67. *point = NUL;
  68. char *res = strCopy(slash);
  69. free(path);
  70. return res;
  71. }
  72. /*
  73. * 函数名: getFileNameWithPath
  74. * 目标: 取出指定路径的文件后缀
  75. */
  76. char *getFileNameWithPath(char *path_1){
  77. char *point = NULL; // 后缀名.所在的字符的指针
  78. char *path = strCopy(path_1); // 复制数组, 避免path_1是常量字符串导致无法修改其值
  79. char *res;
  80. if ((point = getFileSurfix(path)) != NULL)
  81. *point = NUL;
  82. res = strCopy(path);
  83. free(path);
  84. return res;
  85. }
  86. char *getFilePath(char *path_1, int dep){
  87. char *slash = NULL; // 后缀名.所在的字符的指针
  88. char *path = strCopy(path_1); // 复制数组, 避免path_1是常量字符串导致无法修改其值
  89. char *res;
  90. if (path[STR_LEN(path) - 1] == SEP_CH) // 若路径的最后一个字符为SEP, 则忽略此SEP
  91. path[STR_LEN(path) - 1] = NUL;
  92. for (NULL; dep > 0; dep--) {
  93. if ((slash = strrchr(path, SEP_CH)) != NULL)
  94. *slash = NUL;
  95. }
  96. res = strCopy(path);
  97. free(path);
  98. return res;
  99. }
  100. /*
  101. * 函数名: getFileSurfix
  102. * 目标: 获取文件后缀 (不会生成新字符串)
  103. */
  104. char *getFileSurfix(char *path) {
  105. char *last_ = strrchr(path, SEP_CH);
  106. if (last_ != NULL)
  107. return strrchr(last_ + 1, '.');
  108. else
  109. return strrchr(path, '.');
  110. }
  111. /*
  112. * 函数名: fileNameToVar
  113. * 目标: 把一个文件名转换为合法的变量名(替换不合法字符为_)
  114. */
  115. char *fileNameToVar(char *name, bool need_free){
  116. char *var;
  117. if (need_free)
  118. var = name; // 在原数据上修改
  119. else
  120. var = strCopy(name); // 复制新的数据再修改
  121. if (!isalpha(*var) && *var != '_')
  122. var = strJoin("_", var, false, true);
  123. for (char *tmp = var; *tmp != 0; tmp++)
  124. if (!isalnum(*tmp) &&'_' != *tmp)
  125. *tmp = '_';
  126. return var;
  127. }
  128. /*
  129. * 函数名: findPath
  130. * 目标: 转换路径为合法路径(相对路径->绝对路径, 绝对路径保持不变)
  131. */
  132. char *findPath(char *path, char *env, bool need_free){
  133. assert(env[STR_LEN(env) - 1] == SEP_CH); // env 必须以 SEP 结尾
  134. #ifdef __linux
  135. if (path[0] != SEP_CH) { // 不以 / 开头
  136. #else
  137. if (!(isupper(path[0]) && (path)[1] == ':')) { // 不以盘符开头
  138. #endif
  139. return strJoin(env, path, false, need_free); // 调整为相对路径模式
  140. } else if (!need_free) { // 若设置了need_free为true, 则等于先复制在释放原来的, 等于没有复制, 所以不做操作
  141. return strCopy(path);
  142. } else {
  143. return path;
  144. }
  145. }
  146. /*
  147. * 函数名: 获取可执行程序目录
  148. * dep表示从可执行程序往回跳出的层数
  149. */
  150. char *getExedir(char *pgm, int dep) {
  151. if (pgm == NULL)
  152. return NULL;
  153. return getFilePath(pgm, dep + 1);
  154. }