PageRenderTime 55ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 1ms

/tags/terraform-0.9.5/src/filelist.c

http://terraform.googlecode.com/
C | 474 lines | 366 code | 76 blank | 32 comment | 45 complexity | 161eac88a4a077031ba6472be651bc97 MD5 | raw file
Possible License(s): GPL-2.0
  1. /* Terraform - (C) 1997-2002 Robert Gasch (r.gasch@chello.nl)
  2. * - http://terraform.sourceforge.net
  3. *
  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 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * This file is copyright (c) 2000 David A. Bartold
  19. */
  20. #include <sys/types.h> /* OS/X requires this */
  21. #include <dirent.h>
  22. #include <regex.h>
  23. #include <sys/stat.h>
  24. #include <unistd.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include "filelist.h"
  28. #include "filenameops.h"
  29. static int
  30. str_cmp_func (const void *_v1_, const void *_v2_)
  31. {
  32. gchar *s1 = (gchar *)_v1_;
  33. gchar *s2 = (gchar *)_v2_;
  34. return g_strcasecmp (s1, s2);
  35. }
  36. static GList *
  37. file_list_add_file_sorted (GList *list,
  38. gchar *file,
  39. gchar *base,
  40. gboolean do_sort)
  41. {
  42. struct stat stat_buf;
  43. int (*fnptr) (const void *, const void *) = str_cmp_func;
  44. stat (file, &stat_buf);
  45. if (S_ISDIR (stat_buf.st_mode))
  46. return list;
  47. if (!S_ISREG (stat_buf.st_mode))
  48. {
  49. g_print ("Ignoring file '%s'. It might be a symlink.\n", file);
  50. return list;
  51. }
  52. /* prevents adding a filename to the list which is already there */
  53. if(g_list_find_custom(list, g_strdup (base), fnptr))
  54. return list;
  55. if (do_sort)
  56. return g_list_insert_sorted (list, g_strdup (base), fnptr);
  57. return g_list_append (list, g_strdup (base));
  58. }
  59. static GList *
  60. file_list_add_file (GList *list,
  61. gchar *file,
  62. gchar *base)
  63. {
  64. return file_list_add_file_sorted (list, file, base, TRUE);
  65. }
  66. static GList *
  67. file_list_add_path_with_regexp (GList *list,
  68. gchar *path,
  69. gchar *pattern)
  70. {
  71. DIR *dir;
  72. struct dirent *dirent;
  73. regex_t re;
  74. dir = opendir (path);
  75. if (dir == NULL)
  76. return list;
  77. if (regcomp (&re, pattern, REG_EXTENDED|REG_ICASE|REG_NOSUB) != 0)
  78. return list;
  79. dirent = readdir (dir);
  80. while (dirent != NULL)
  81. {
  82. gchar *filename, *base;
  83. int status;
  84. filename = g_strdup_printf ("%s/%s", path, dirent->d_name);
  85. base = filename_get_base (filename);
  86. status = regexec (&re, base, (size_t)0, NULL, 0);
  87. if (status == 0)
  88. list = file_list_add_file (list, filename, base);
  89. dirent = readdir (dir);
  90. }
  91. regfree (&re);
  92. closedir (dir);
  93. return list;
  94. }
  95. static GList *
  96. file_list_add_path_with_extension (GList *list,
  97. gchar *path,
  98. gchar *extension)
  99. {
  100. DIR *dir;
  101. struct dirent *dirent;
  102. dir = opendir (path);
  103. if (dir == NULL)
  104. return list;
  105. dirent = readdir (dir);
  106. while (dirent != NULL)
  107. {
  108. gchar *filename, *base, *ext;
  109. filename = g_strdup_printf ("%s/%s", path, dirent->d_name);
  110. base = filename_get_base (filename);
  111. ext = filename_get_extension (filename);
  112. if (extension && !strcmp (ext, extension))
  113. list = file_list_add_file (list, filename, base);
  114. else
  115. if (!extension)
  116. list = file_list_add_file (list, filename, base);
  117. g_free (ext);
  118. g_free (base);
  119. g_free (filename);
  120. dirent = readdir (dir);
  121. }
  122. closedir (dir);
  123. return list;
  124. }
  125. static GList *
  126. file_list_add_path (GList *list,
  127. gchar *path)
  128. {
  129. return file_list_add_path_with_extension (list, path, NULL);
  130. }
  131. GList *
  132. texture_list_new ()
  133. {
  134. gchar *home_dir;
  135. gchar *extension = "inc";
  136. home_dir = getenv ("HOME");
  137. if (home_dir != NULL){
  138. GList *textures_list;
  139. gchar *textures_path;
  140. textures_path = g_strdup_printf("%s/.terraform/include/earth_textures", home_dir);
  141. textures_list = file_list_add_path_with_extension (NULL, textures_path, extension);
  142. g_free (textures_path);
  143. return file_list_add_path_with_extension (textures_list,
  144. TERRAFORM_DATA_DIR "/include/earth_textures",
  145. extension);
  146. }
  147. else
  148. return file_list_add_path_with_extension (NULL,
  149. TERRAFORM_DATA_DIR "/include/earth_textures",
  150. extension);
  151. }
  152. /*
  153. * This part modified by Raymond Ostertag
  154. */
  155. GList *
  156. object_list_new ()
  157. {
  158. gchar *home_dir;
  159. gchar *re = "^[^_].*\\.inc$";
  160. home_dir = getenv ("HOME");
  161. if (home_dir != NULL){
  162. GList *object_list;
  163. gchar *object_path;
  164. object_path = g_strdup_printf("%s/.terraform/objects",
  165. home_dir);
  166. object_list = file_list_add_path_with_regexp (NULL, object_path, re);
  167. g_free (object_path);
  168. return file_list_add_path_with_regexp (object_list,
  169. TERRAFORM_DATA_DIR "/objects", re);
  170. }
  171. else
  172. return file_list_add_path_with_extension (NULL,
  173. TERRAFORM_DATA_DIR "/objects", re);
  174. }
  175. /*
  176. * End by Raymond Ostertag
  177. */
  178. GList *
  179. cloud_list_new ()
  180. {
  181. gchar *home_dir;
  182. gchar *re = "^clouds_.*\\.inc$";
  183. home_dir = getenv ("HOME");
  184. if (home_dir != NULL){
  185. GList *object_list;
  186. gchar *object_path;
  187. object_path = g_strdup_printf("%s/.terraform/include/skies/include",
  188. home_dir);
  189. object_list = file_list_add_path_with_regexp (NULL, object_path, re);
  190. g_free (object_path);
  191. return file_list_add_path_with_regexp (object_list,
  192. TERRAFORM_DATA_DIR "/include/skies/include", re);
  193. }
  194. else
  195. return file_list_add_path_with_extension (NULL,
  196. TERRAFORM_DATA_DIR "/include/skies/include", re);
  197. }
  198. GList *
  199. atmosphere_list_new ()
  200. {
  201. gchar *home_dir;
  202. gchar *re = "^earth_.*\\.inc$";
  203. home_dir = getenv ("HOME");
  204. if (home_dir != NULL){
  205. GList *object_list;
  206. gchar *object_path;
  207. object_path = g_strdup_printf("%s/.terraform/include/atmospheres",
  208. home_dir);
  209. object_list = file_list_add_path_with_regexp (NULL, object_path, re);
  210. g_free (object_path);
  211. return file_list_add_path_with_regexp (object_list,
  212. TERRAFORM_DATA_DIR "/include/atmospheres", re);
  213. }
  214. else
  215. return file_list_add_path_with_extension (NULL,
  216. TERRAFORM_DATA_DIR "/include/atmospheres", re);
  217. }
  218. GList *
  219. star_list_new ()
  220. {
  221. gchar *home_dir;
  222. gchar *re = "^stars_.*\\.inc$";
  223. home_dir = getenv ("HOME");
  224. if (home_dir != NULL){
  225. GList *object_list;
  226. gchar *object_path;
  227. object_path = g_strdup_printf("%s/.terraform/include/skies/include",
  228. home_dir);
  229. object_list = file_list_add_path_with_regexp (NULL, object_path, re);
  230. g_free (object_path);
  231. return file_list_add_path_with_regexp (object_list,
  232. TERRAFORM_DATA_DIR "/include/skies/include", re);
  233. }
  234. else
  235. return file_list_add_path_with_extension (NULL,
  236. TERRAFORM_DATA_DIR "/include/skies/include", re);
  237. }
  238. GList *
  239. water_list_new ()
  240. {
  241. gchar *home_dir;
  242. gchar *re = "^earth_.*\\.inc$";
  243. home_dir = getenv ("HOME");
  244. if (home_dir != NULL){
  245. GList *object_list;
  246. gchar *object_path;
  247. object_path = g_strdup_printf("%s/.terraform/include/water",
  248. home_dir);
  249. object_list = file_list_add_path_with_regexp (NULL, object_path, re);
  250. g_free (object_path);
  251. return file_list_add_path_with_regexp (object_list,
  252. TERRAFORM_DATA_DIR "/include/water", re);
  253. }
  254. else
  255. return file_list_add_path_with_extension (NULL,
  256. TERRAFORM_DATA_DIR "/include/water", re);
  257. }
  258. GList *
  259. light_list_new ()
  260. {
  261. gchar *home_dir;
  262. gchar *re = "^lights_.*\\.inc$";
  263. home_dir = getenv ("HOME");
  264. if (home_dir != NULL){
  265. GList *object_list;
  266. gchar *object_path;
  267. object_path = g_strdup_printf("%s/.terraform/include/skies/include",
  268. home_dir);
  269. object_list = file_list_add_path_with_regexp (NULL, object_path, re);
  270. g_free (object_path);
  271. return file_list_add_path_with_regexp (object_list,
  272. TERRAFORM_DATA_DIR "/include/skies/include", re);
  273. }
  274. else
  275. return file_list_add_path_with_extension (NULL,
  276. TERRAFORM_DATA_DIR "/include/skies/include", re);
  277. }
  278. GList *
  279. scene_theme_list_new ()
  280. {
  281. gchar *home_dir;
  282. gchar *re = "^scene_.*\\.inc$";
  283. home_dir = getenv ("HOME");
  284. if (home_dir != NULL){
  285. GList *object_list;
  286. gchar *object_path;
  287. object_path = g_strdup_printf("%s/.terraform/include",
  288. home_dir);
  289. object_list = file_list_add_path_with_regexp (NULL, object_path, re);
  290. g_free (object_path);
  291. return file_list_add_path_with_regexp (object_list,
  292. TERRAFORM_DATA_DIR "/include", re);
  293. }
  294. else
  295. return file_list_add_path_with_extension (NULL,
  296. TERRAFORM_DATA_DIR "/include", re);
  297. }
  298. GList *
  299. skycolor_list_new ()
  300. {
  301. gchar *home_dir;
  302. gchar *re = "^skycolor_.*\\.inc$";
  303. home_dir = getenv ("HOME");
  304. if (home_dir != NULL){
  305. GList *object_list;
  306. gchar *object_path;
  307. object_path = g_strdup_printf("%s/.terraform/include/skies/include",
  308. home_dir);
  309. object_list = file_list_add_path_with_regexp (NULL, object_path, re);
  310. g_free (object_path);
  311. return file_list_add_path_with_regexp (object_list,
  312. TERRAFORM_DATA_DIR "/include/skies/include", re);
  313. }
  314. else
  315. return file_list_add_path_with_extension (NULL,
  316. TERRAFORM_DATA_DIR "/include/skies/include", re);
  317. }
  318. GList *
  319. background_image_list_new ()
  320. {
  321. gchar *home_dir;
  322. gchar *re = "^sky_.*\\.tga$";
  323. home_dir = getenv ("HOME");
  324. if (home_dir != NULL){
  325. GList *object_list;
  326. gchar *object_path;
  327. object_path = g_strdup_printf("%s/.terraform/image_maps",
  328. home_dir);
  329. object_list = file_list_add_path_with_regexp (NULL, object_path, re);
  330. g_free (object_path);
  331. return file_list_add_path_with_regexp (object_list,
  332. TERRAFORM_DATA_DIR "/image_maps", re);
  333. }
  334. else
  335. return file_list_add_path_with_extension (NULL,
  336. TERRAFORM_DATA_DIR "/image_maps", re);
  337. }
  338. GList *
  339. moon_image_list_new ()
  340. {
  341. gchar *home_dir;
  342. gchar *re = "^moon_.*\\.png$";
  343. home_dir = getenv ("HOME");
  344. if (home_dir != NULL){
  345. GList *object_list;
  346. gchar *object_path;
  347. object_path = g_strdup_printf("%s/.terraform/image_maps",
  348. home_dir);
  349. object_list = file_list_add_path_with_regexp (NULL, object_path, re);
  350. g_free (object_path);
  351. return file_list_add_path_with_regexp (object_list,
  352. TERRAFORM_DATA_DIR "/image_maps", re);
  353. }
  354. else
  355. return file_list_add_path_with_extension (NULL,
  356. TERRAFORM_DATA_DIR "/image_maps", re);
  357. }
  358. /*
  359. * This part added for Trace Objects by Raymond Ostertag
  360. */
  361. GList *
  362. trace_object_list_new ()
  363. {
  364. gchar *home_dir;
  365. gchar *re = "^_.*\\.inc$";
  366. home_dir = getenv ("HOME");
  367. if (home_dir != NULL){
  368. GList *object_list;
  369. gchar *object_path;
  370. object_path = g_strdup_printf("%s/.terraform/objects",
  371. home_dir);
  372. object_list = file_list_add_path_with_regexp (NULL, object_path, re);
  373. g_free (object_path);
  374. return file_list_add_path_with_regexp (object_list,
  375. TERRAFORM_DATA_DIR "/objects", re);
  376. }
  377. else
  378. return file_list_add_path_with_extension (NULL,
  379. TERRAFORM_DATA_DIR "/objects", re);
  380. }
  381. /*
  382. * End for Trace Objects by Raymond Ostertag
  383. */
  384. void
  385. file_list_free (GList *list)
  386. {
  387. g_list_foreach (list, (GFunc) g_free, NULL);
  388. g_list_free (list);
  389. }
  390. GList *
  391. file_list_copy (GList *list)
  392. {
  393. GList *out;
  394. out = NULL;
  395. for (list = g_list_first (list); list != NULL; list = list->next)
  396. out = g_list_append (out, g_strdup (list->data));
  397. return out;
  398. }