file.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
  3. * All rights reserved.
  4. * This component and the accompanying materials are made available
  5. * under the terms of the License "Eclipse Public License v1.0"
  6. * which accompanies this distribution, and is available
  7. * at the URL "http://www.eclipse.org/legal/epl-v10.html".
  8. *
  9. * Initial Contributors:
  10. * Nokia Corporation - initial contribution.
  11. *
  12. * Contributors:
  13. *
  14. * Description:
  15. *
  16. */
  17. #include "../config.h"
  18. #include "file.h"
  19. #include "log.h"
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #ifdef HAS_MSVCRT
  24. #define WIN32_LEAN_AND_MEAN
  25. #include <windows.h>
  26. #endif
  27. /*
  28. * is_file - tests if the given string is an existing file
  29. * value: C string of a filename to check for existence. Note, that
  30. * no manipulation or "tidying up" is done, e.g. no double \'s are fixed, nor
  31. * is any conversion of / to \ done.
  32. * Returns -1 on error; 0 if the file does not exist; 1 if the file exists
  33. */
  34. int is_file(char name[])
  35. {
  36. int ret_val = 1;
  37. #ifdef HAS_MSVCRT
  38. /*
  39. * This can be changed to use fstat as this is available on Windows.
  40. */
  41. WIN32_FIND_DATA find_file_data;
  42. HANDLE handle;
  43. LPVOID msg_buff;
  44. handle = FindFirstFile(name, &find_file_data);
  45. if (handle == INVALID_HANDLE_VALUE)
  46. {
  47. DWORD last_error = GetLastError();
  48. FormatMessage(
  49. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  50. FORMAT_MESSAGE_FROM_SYSTEM |
  51. FORMAT_MESSAGE_IGNORE_INSERTS,
  52. NULL,
  53. last_error,
  54. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  55. (LPTSTR) &msg_buff,
  56. 0, NULL );
  57. if(last_error == ERROR_FILE_NOT_FOUND || last_error == ERROR_PATH_NOT_FOUND)
  58. {
  59. DEBUG(("File \"%s\" does not exist (error code: %d):\n%s\n",
  60. name, last_error, msg_buff));
  61. ret_val = 0;
  62. }
  63. else
  64. {
  65. DEBUG(("FindFirstFile failed (error code: %d)\n"
  66. "Unable to determine existence of \"%s\":\n%s\n",
  67. last_error, name, msg_buff));
  68. ret_val = -1;
  69. }
  70. LocalFree(msg_buff);
  71. return ret_val;
  72. }
  73. else
  74. {
  75. DEBUG(("The first file found is %s\n", find_file_data.cFileName));
  76. FindClose(handle);
  77. return 1;
  78. }
  79. #else
  80. return 0;
  81. #endif
  82. }
  83. char *get_cwd(void)
  84. {
  85. #if defined(HAS_GETCURRENTDIRECTORY)
  86. int limit = 32000;
  87. char * cwd = malloc(limit * sizeof(char));
  88. if(cwd)
  89. {
  90. GetCurrentDirectory(limit, cwd);
  91. DEBUG(("get_cwd: current working directory calculated as: %s\n", cwd));
  92. return cwd;
  93. }
  94. else
  95. return NULL;
  96. #else
  97. # error "Need a function for getting the current working directory"
  98. #endif
  99. }
  100. char *get_exe_location(void)
  101. {
  102. #if defined(HAS_MSVCRT)
  103. char *full_path_to_exe = malloc(32000*sizeof(char *));
  104. DWORD last_error;
  105. LPVOID msg_buff;
  106. if(full_path_to_exe)
  107. {
  108. // GetModuleFileName: giving NULL as the first argument retrieves the
  109. // path of the executable file of the current process.
  110. DWORD dw = GetModuleFileName(NULL, full_path_to_exe, 32000);
  111. if(dw != 0)
  112. {
  113. DEBUG(("Full path to this exe is: %s\n", full_path_to_exe));
  114. return full_path_to_exe;
  115. }
  116. else
  117. {
  118. last_error = GetLastError();
  119. FormatMessage(
  120. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  121. FORMAT_MESSAGE_FROM_SYSTEM |
  122. FORMAT_MESSAGE_IGNORE_INSERTS,
  123. NULL,
  124. last_error,
  125. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  126. (LPTSTR) &msg_buff,
  127. 0, NULL );
  128. DEBUG(("Failed to determine the full path to this exe: %s\n", msg_buff));
  129. LocalFree(msg_buff);
  130. return NULL;
  131. }
  132. }
  133. else
  134. return NULL;
  135. #else
  136. # error "Need a function for getting the full path of the currently execting process"
  137. #endif
  138. }
  139. char *get_parent_dir(char dir[])
  140. {
  141. if(dir == NULL)
  142. return NULL;
  143. int len = strlen(dir);
  144. int start = len - 1;
  145. /* If dir ends with /, skip it in the searching */
  146. if(dir[start] == SEP)
  147. start--;
  148. while(dir[start] != SEP)
  149. start--;
  150. char *parent_dir = malloc(len * sizeof(char));
  151. if(parent_dir)
  152. {
  153. strncpy(parent_dir, dir, start);
  154. parent_dir[start] = '\0';
  155. return parent_dir;
  156. }
  157. return NULL;
  158. }
  159. char *path_join(char component1[], char component2[])
  160. {
  161. if(component1 == NULL)
  162. return component2;
  163. if(component2 == NULL)
  164. return component1;
  165. // Enough space for strings and extra SEP characters
  166. int full_length = strlen(component1) + strlen(component2) + 5;
  167. char *joined_path = malloc(full_length);
  168. if(joined_path != NULL)
  169. {
  170. DEBUG(("Joining paths %s and %s\n", component1, component2));
  171. snprintf(joined_path, full_length, "%s%sc%s", component1, SEP, component2);
  172. DEBUG(("Joined path is: s\n", joined_path));
  173. return joined_path;
  174. }
  175. else
  176. {
  177. DEBUG(("Joining paths %s and %s failed.\n", component1, component2));
  178. return NULL;
  179. }
  180. }