helpers.c 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #include "helpers.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/types.h>
  6. #include <dirent.h>
  7. LIGHT_BOOL light_readUInt(char const * filename, unsigned int *i)
  8. {
  9. FILE* fileHandle;
  10. unsigned int iCopy;
  11. fileHandle = fopen(filename, "r");
  12. if(!fileHandle)
  13. {
  14. LIGHT_PERMERR("reading");
  15. return FALSE;
  16. }
  17. if(fscanf(fileHandle, "%u", &iCopy) != 1)
  18. {
  19. LIGHT_ERR_FMT("Couldn't parse a positive integer number from '%s'", filename);
  20. fclose(fileHandle);
  21. return FALSE;
  22. }
  23. *i = iCopy;
  24. fclose(fileHandle);
  25. return TRUE;
  26. }
  27. LIGHT_BOOL light_writeUInt(char const * filename, unsigned int i)
  28. {
  29. FILE* fileHandle;
  30. fileHandle = fopen(filename, "w");
  31. if(!fileHandle)
  32. {
  33. LIGHT_PERMERR("writing");
  34. return FALSE;
  35. }
  36. if(fprintf(fileHandle, "%u", i) < 0)
  37. {
  38. LIGHT_ERR("fprintf failed");
  39. fclose(fileHandle);
  40. return FALSE;
  41. }
  42. fclose(fileHandle);
  43. return TRUE;
  44. }
  45. LIGHT_BOOL light_readULong(char const * filename, unsigned long *i)
  46. {
  47. FILE* fileHandle;
  48. unsigned long iCopy;;
  49. fileHandle = fopen(filename, "r");
  50. if(!fileHandle)
  51. {
  52. LIGHT_PERMERR("reading");
  53. return FALSE;
  54. }
  55. if(fscanf(fileHandle, "%lu", &iCopy) != 1)
  56. {
  57. LIGHT_ERR_FMT("Couldn't parse a positive integer number from '%s'", filename);
  58. fclose(fileHandle);
  59. return FALSE;
  60. }
  61. *i = iCopy;
  62. fclose(fileHandle);
  63. return TRUE;
  64. }
  65. LIGHT_BOOL light_writeULong(char const * filename, unsigned long i)
  66. {
  67. FILE* fileHandle;
  68. fileHandle = fopen(filename, "w");
  69. if(!fileHandle)
  70. {
  71. LIGHT_PERMERR("writing");
  72. return FALSE;
  73. }
  74. if(fprintf(fileHandle, "%lu", i) < 0)
  75. {
  76. LIGHT_ERR("fprintf failed");
  77. fclose(fileHandle);
  78. return FALSE;
  79. }
  80. fclose(fileHandle);
  81. return TRUE;
  82. }
  83. LIGHT_BOOL light_readString(char const * filename, char *buffer, long* size)
  84. {
  85. FILE *fileHandle;
  86. long fileSize;
  87. long readSize;
  88. /* Make sure buffer pointer is null */
  89. if(buffer != NULL)
  90. {
  91. LIGHT_ERR("buffer passed to function isn't NULL");
  92. return FALSE;
  93. }
  94. /* Open file */
  95. fileHandle = fopen(filename, "r");
  96. if(!fileHandle)
  97. {
  98. LIGHT_PERMERR("reading");
  99. return FALSE;
  100. }
  101. /* Get the file size */
  102. fseek(fileHandle, 0L, SEEK_END);
  103. fileSize = ftell(fileHandle);
  104. rewind(fileHandle);
  105. /* Allocate the string and null-terminate it */
  106. buffer = malloc(sizeof(char)*(fileSize+1));
  107. memset(buffer, '\0', fileSize);
  108. if(buffer == NULL)
  109. {
  110. LIGHT_MEMERR();
  111. fclose(fileHandle);
  112. return FALSE;
  113. }
  114. /* Read the file */
  115. readSize = fread(buffer, sizeof(char), fileSize, fileHandle);
  116. if(readSize != fileSize)
  117. {
  118. LIGHT_ERR("read error");
  119. free(buffer);
  120. fclose(fileHandle);
  121. return FALSE;
  122. }
  123. /* Set the size */
  124. if(size != NULL)
  125. {
  126. *size = readSize;
  127. }
  128. /* All well, close handle and return TRUE */
  129. fclose(fileHandle);
  130. return TRUE;
  131. }
  132. LIGHT_BOOL light_isDir(char const * path)
  133. {
  134. DIR *dirHandle = opendir(path);
  135. if(!dirHandle)
  136. {
  137. return FALSE;
  138. }
  139. closedir(dirHandle);
  140. return TRUE;
  141. }
  142. LIGHT_BOOL light_isWritable(char const * filename)
  143. {
  144. FILE* fileHandle = fopen(filename, "r+");
  145. if(!fileHandle)
  146. {
  147. LIGHT_PERMWARN("writing");
  148. return FALSE;
  149. }
  150. fclose(fileHandle);
  151. return TRUE;
  152. }
  153. LIGHT_BOOL light_isReadable(char const * filename)
  154. {
  155. FILE* fileHandle = fopen(filename, "r");
  156. if(!fileHandle)
  157. {
  158. LIGHT_PERMWARN("reading");
  159. return FALSE;
  160. }
  161. fclose(fileHandle);
  162. return TRUE;
  163. }