esdl_utils.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #include <platform.h>
  14. #include "esdl_utils.hpp"
  15. #if defined (__linux__)
  16. #include <dirent.h>
  17. #endif
  18. // borrow from jlib
  19. bool es_checkDirExists(const char * filename)
  20. {
  21. #ifdef _WIN32
  22. DWORD attr = GetFileAttributes(filename);
  23. return (attr != (DWORD)-1)&&(attr & FILE_ATTRIBUTE_DIRECTORY);
  24. #else
  25. struct stat info;
  26. if (stat(filename, &info) != 0)
  27. return false;
  28. return S_ISDIR(info.st_mode);
  29. #endif
  30. }
  31. void es_createDirectory(const char* dir)
  32. {
  33. if (dir && *dir)
  34. {
  35. if (!es_checkDirExists(dir)) {
  36. #ifdef WIN32
  37. if (mkdir(dir)!=0)
  38. #else
  39. if (mkdir(dir,0755)!=0)
  40. #endif
  41. {
  42. if (!es_checkDirExists(dir))
  43. {
  44. fprintf(stderr,"Create directory %s failed", dir);
  45. exit(1);
  46. }
  47. }
  48. }
  49. }
  50. }
  51. int es_createFile(const char* src, const char* ext)
  52. {
  53. char * path = es_changeext(src,ext);
  54. //printf("Target: %s\n", path);
  55. int h = open(path,_O_WRONLY | _O_CREAT | _O_TRUNC | _O_TEXT , _S_IREAD|_S_IWRITE);
  56. if (h==-1)
  57. {
  58. printf("Could not open file for write: %s (current dir: %s)\n",path,getcwd(NULL,0));
  59. }
  60. free(path);
  61. return h;
  62. }
  63. char * es_gettail(const char *fn)
  64. {
  65. const char *e=NULL;
  66. const char *e1=fn;
  67. while((e1=strchr(e1,'.'))!=NULL)
  68. e = e1++;
  69. const char *s=fn;
  70. const char *s1;
  71. #ifdef _WIN32
  72. if (*s&&s[1]==':')
  73. s+=2;
  74. #endif
  75. for (s1 = s;*s1&&(s1!=e);s1++)
  76. #ifdef _WIN32
  77. if (*s1=='\\')
  78. #else
  79. if (*s1=='/')
  80. #endif
  81. s = s1+1;
  82. size_t l = s1-s;
  83. char *ret = (char *)malloc(l+1);
  84. memcpy(ret,s,l);
  85. ret[l] = 0;
  86. return ret;
  87. }
  88. char * es_changeext(const char *fn,const char *ext)
  89. {
  90. int preext, l;
  91. preext = l = strlen(fn);
  92. char *p;
  93. for (p=(char*)(fn+preext-1); p>=fn; p--)
  94. {
  95. if (*p == '.')
  96. {
  97. preext = p-fn;
  98. break;
  99. }
  100. }
  101. // char *ret=gettail(fn);
  102. // size_t l = strlen(ret);
  103. // ret = (char *)realloc(ret,l+strlen(ext)+2);
  104. char *ret = (char *)malloc(preext+strlen(ext)+2);
  105. memcpy(ret, fn, preext);
  106. ret[preext] = '.';
  107. strcpy(ret+preext+1,ext);
  108. return ret;
  109. }
  110. char * es_changetail(const char *fn,const char *tail, const char *ext)
  111. {
  112. int preext, l;
  113. preext = l = strlen(fn);
  114. char *p;
  115. for (p=(char*)(fn+preext-1); p>=fn; p--)
  116. {
  117. if (*p == '.')
  118. {
  119. preext = p-fn;
  120. break;
  121. }
  122. }
  123. char *ret = (char *)malloc(preext+strlen(tail)+strlen(ext)+2);
  124. memcpy(ret, fn, preext);
  125. ret[preext] = 0;
  126. strcat(ret,tail);
  127. strcat(ret,".");
  128. strcat(ret,ext);
  129. return ret;
  130. }
  131. bool es_hasext(const char *fn,const char *ext)
  132. { // assumes 3 char ext
  133. const char *s = strstr(fn,ext);
  134. if (!s)
  135. return false;
  136. return (s!=fn)&&(*(s-1)=='.')&&(s[4]==0);
  137. }
  138. int es_createFile(const char* src, const char* tail, const char* ext)
  139. {
  140. char * path=es_changetail(src,tail,ext);
  141. int h = open(path,_O_WRONLY | _O_CREAT | _O_TRUNC | _O_TEXT , _S_IREAD|_S_IWRITE);
  142. if (h==-1)
  143. {
  144. printf("Could not open file for write: %s (current dir: %s)\n",path,getcwd(NULL,0));
  145. }
  146. free(path);
  147. return h;
  148. }