Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
3.1 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. INFSCAN
  5. common.inl
  6. Abstract:
  7. Global functions (inlines) and global function templates
  8. History:
  9. Created July 2001 - JamieHun
  10. --*/
  11. template<class T>
  12. SafeString ConvertString(const T* arg)
  13. /*++
  14. Routine Description:
  15. Convert string pointer to SafeString
  16. (unnatural char type)
  17. Arguments:
  18. arg - string to convert
  19. Return Value:
  20. SafeString(arg)
  21. --*/
  22. {
  23. PCTSTR targ = CopyString(arg);
  24. SafeString final;
  25. if(targ) {
  26. final = targ;
  27. delete [] targ;
  28. }
  29. return final;
  30. }
  31. template<>
  32. inline SafeString ConvertString<TCHAR>(const TCHAR * arg)
  33. /*++
  34. Routine Description:
  35. Convert string pointer to SafeString
  36. (natural char type override)
  37. Arguments:
  38. arg - string to convert
  39. Return Value:
  40. SafeString(arg)
  41. --*/
  42. {
  43. return arg;
  44. }
  45. //
  46. // template/polymorphic stuff
  47. //
  48. inline
  49. bool MyIsAlpha(CHAR c)
  50. /*++
  51. Routine Description:
  52. typed version of isalpha (ANSI)
  53. used by GetFileNamePart
  54. Arguments:
  55. ANSI char
  56. Return Value:
  57. true if it's alpha
  58. --*/
  59. {
  60. return isalpha(c) != 0;
  61. }
  62. inline
  63. bool MyIsAlpha(WCHAR c)
  64. /*++
  65. Routine Description:
  66. typed version of isalpha (Unicode)
  67. used by GetFileNamePart
  68. Arguments:
  69. Unicode char
  70. Return Value:
  71. true if it's alpha
  72. --*/
  73. {
  74. return iswalpha(c) != 0;
  75. }
  76. inline
  77. LPSTR MyCharNext(LPCSTR lpsz)
  78. /*++
  79. Routine Description:
  80. typed version of CharNext (Ansi)
  81. used by GetFileNamePart
  82. Arguments:
  83. Ansi string pointer
  84. Return Value:
  85. Ansi string pointer pointing to next char if prev char non-null
  86. --*/
  87. {
  88. return CharNextA(lpsz);
  89. }
  90. inline
  91. LPWSTR MyCharNext(LPCWSTR lpsz)
  92. /*++
  93. Routine Description:
  94. typed version of CharNext (Unicode)
  95. used by GetFileNamePart
  96. Arguments:
  97. Unicode string pointer
  98. Return Value:
  99. Unicode string pointer pointing to next char if prev char non-null
  100. --*/
  101. {
  102. return CharNextW(lpsz);
  103. }
  104. template<class T>
  105. T * GetFileNamePart(const T * path)
  106. /*++
  107. Routine Description:
  108. Template:
  109. PCWSTR/PCSTR/PCTSTR version
  110. Find basename.ext part of a full path
  111. written as template to work in both ansi and unicode form
  112. Arguments:
  113. path - zero-terminated pathname
  114. Return Value:
  115. pointer to the filename part
  116. --*/
  117. {
  118. const T * mark = path;
  119. const T * name = path;
  120. if(MyIsAlpha(*mark) && *MyCharNext(mark) == ':') {
  121. mark = MyCharNext(MyCharNext(mark));
  122. name = mark;
  123. }
  124. while(*mark) {
  125. if((*mark == '/') || (*mark == '\\')) {
  126. name = MyCharNext(mark);
  127. }
  128. mark = MyCharNext(mark);
  129. }
  130. return (T*)name;
  131. }
  132. template<class T>
  133. SafeString_<T> GetFileNamePart(const SafeString_<T> & path)
  134. /*++
  135. Routine Description:
  136. Template:
  137. SafeStringX version of GetFileNamePart
  138. See GetFileNamePart above
  139. Arguments:
  140. path - pathname
  141. Return Value:
  142. filename part
  143. --*/
  144. {
  145. return GetFileNamePart(path.c_str());
  146. }