clutil.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. ---------------------------------------------------------------------------
  3. Copyright (c) 2003, Dominik Reichl <dominik.reichl@t-online.de>, Germany.
  4. All rights reserved.
  5. Distributed under the terms of the GNU General Public License v2.
  6. This software is provided 'as is' with no explicit or implied warranties
  7. in respect of its properties, including, but not limited to, correctness
  8. and/or fitness for purpose.
  9. ---------------------------------------------------------------------------
  10. */
  11. #include "clutil.h"
  12. #include "rehash.h"
  13. bool isArgument(char *pszString)
  14. {
  15. if(pszString == NULL) return false;
  16. if(pszString[0] == '-') return true;
  17. return false;
  18. }
  19. void fmtArgument(char *pszArg, char *pszDest)
  20. {
  21. unsigned int i = 0;
  22. int pos = 0;
  23. if(pszArg == NULL) return;
  24. if(pszDest == NULL) return;
  25. pszDest[0] = 0;
  26. // Filter special characters
  27. for(i = 0; i < strlen(pszArg); i++)
  28. {
  29. if(pszArg[i] == '-') continue;
  30. if(pszArg[i] == '#') continue;
  31. if(pszArg[i] == ' ') continue;
  32. if(pszArg[i] == '<') continue;
  33. if(pszArg[i] == '>') continue;
  34. if(pszArg[i] == '(') continue;
  35. if(pszArg[i] == ')') continue;
  36. if(pszArg[i] == '[') continue;
  37. if(pszArg[i] == ']') continue;
  38. if(pszArg[i] == '/') continue;
  39. if(pszArg[i] == '\\') continue;
  40. if(pszArg[i] == '\"') continue;
  41. pszDest[pos] = pszArg[i];
  42. pos++;
  43. }
  44. pszDest[pos] = 0;
  45. strlwr(pszDest);
  46. }
  47. void fmtPath(char *pszPath)
  48. {
  49. unsigned int i = 0;
  50. char chFind = 0;
  51. char chReplace = 0;
  52. if(pszPath == NULL) return;
  53. if(strlen(pszPath) == 0) return;
  54. #if(RH_TARGET_SYSTEM == RH_TARGET_SYSTEM_WINDOWS)
  55. chFind = '/';
  56. chReplace = '\\';
  57. #elif(RH_TARGET_SYSTEM == RH_TARGET_SYSTEM_LINUX)
  58. chFind = '\\';
  59. chReplace = '/';
  60. #endif
  61. for(i = 0; i < strlen(pszPath); i++)
  62. {
  63. if(pszPath[i] == chFind) pszPath[i] = chReplace;
  64. }
  65. }
  66. void catdirsep(char *pszPath)
  67. {
  68. unsigned int i = 0;
  69. if(pszPath == NULL) return;
  70. i = strlen(pszPath);
  71. if(pszPath[i-1] == SZ_DIR_CHAR) return; // Nothing to do
  72. strcat(pszPath, SZ_DIR_STR);
  73. }
  74. void pathonly(char *pszPath)
  75. {
  76. unsigned int i = 0;
  77. bool bReplaced = false;
  78. if(pszPath == NULL) return;
  79. i = strlen(pszPath) - 1;
  80. if(i == (unsigned int)-1) return;
  81. while(1)
  82. {
  83. if(pszPath[i] == SZ_DIR_CHAR)
  84. {
  85. pszPath[i] = 0;
  86. bReplaced = true;
  87. break;
  88. }
  89. i--;
  90. if(i == (unsigned int)-1) break;
  91. }
  92. if(bReplaced == false) pszPath[0] = 0;
  93. #if(RH_TARGET_SYSTEM == RH_TARGET_SYSTEM_WINDOWS)
  94. if(strlen(pszPath) == 2) catdirsep(pszPath);
  95. #endif
  96. }
  97. void fileonly(char *pszPath)
  98. {
  99. char szTemp[RH_MAX_PATH];
  100. unsigned int i = 0;
  101. unsigned int j = 0;
  102. if(pszPath == NULL) return;
  103. i = strlen(pszPath) - 1;
  104. if(i == (unsigned int)-1) return;
  105. if(i <= 1) return;
  106. while(1) // Reverse scan for path delimiter
  107. {
  108. if(pszPath[i] == SZ_DIR_CHAR) break;
  109. i--;
  110. if(i == 0) break;
  111. }
  112. if(i == 0) return;
  113. j = 0;
  114. i++;
  115. for( ; i < strlen(pszPath); i++) // Copy only filename to new buffer
  116. {
  117. szTemp[j] = pszPath[i];
  118. j++;
  119. }
  120. szTemp[j] = 0;
  121. strcpy(pszPath, szTemp); // Copy working buffer to return buffer
  122. }
  123. // Is this a <..> or <.> path env descriptor?
  124. bool ispathnav(char *pszPath)
  125. {
  126. unsigned int i = 0;
  127. if(pszPath == NULL) return false;
  128. i = strlen(pszPath);
  129. if(i == 0) return false;
  130. if((pszPath[i-1] == '.') && (i == 1)) return true;
  131. if((pszPath[i-1] == '.') && (pszPath[i-2] == '.') && (i == 2)) return true;
  132. if((pszPath[i-1] == '.') && (pszPath[i-2] == SZ_DIR_CHAR)) return true;
  133. if((pszPath[i-1] == '.') && (pszPath[i-2] == '.') && (pszPath[i-3] == SZ_DIR_CHAR)) return true;
  134. return false;
  135. }