Source code of Windows XP (NT5)
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.

157 lines
3.7 KiB

  1. #include "utils.h"
  2. //***************************************************************************
  3. //*
  4. //* purpose:
  5. //*
  6. //***************************************************************************
  7. LPSTR StripWhitespace( LPSTR pszString )
  8. {
  9. LPSTR pszTemp = NULL;
  10. if ( pszString == NULL )
  11. {
  12. return NULL;
  13. }
  14. while ( *pszString == ' ' || *pszString == '\t' )
  15. {
  16. pszString += 1;
  17. }
  18. // Catch case where string consists entirely of whitespace or empty string.
  19. if ( *pszString == '\0' )
  20. {
  21. return pszString;
  22. }
  23. pszTemp = pszString;
  24. pszString += lstrlenA(pszString) - 1;
  25. while ( *pszString == ' ' || *pszString == '\t' )
  26. {
  27. *pszString = '\0';
  28. pszString -= 1;
  29. }
  30. return pszTemp;
  31. }
  32. //***************************************************************************
  33. //*
  34. //* purpose: return back a Alocated wide string from a ansi string
  35. //* caller must free the returned back pointer with GlobalFree()
  36. //*
  37. //***************************************************************************
  38. LPWSTR MakeWideStrFromAnsi(UINT uiCodePage, LPSTR psz)
  39. {
  40. LPWSTR pwsz;
  41. int i;
  42. // make sure they gave us something
  43. if (!psz)
  44. {
  45. return NULL;
  46. }
  47. // compute the length
  48. i = MultiByteToWideChar(uiCodePage, 0, psz, -1, NULL, 0);
  49. if (i <= 0) return NULL;
  50. // allocate memory in that length
  51. pwsz = (LPWSTR) GlobalAlloc(GPTR,i * sizeof(WCHAR));
  52. if (!pwsz) return NULL;
  53. // clear out memory
  54. memset(pwsz, 0, wcslen(pwsz) * sizeof(WCHAR));
  55. // convert the ansi string into unicode
  56. i = MultiByteToWideChar(uiCodePage, 0, (LPSTR) psz, -1, pwsz, i);
  57. if (i <= 0)
  58. {
  59. GlobalFree(pwsz);
  60. pwsz = NULL;
  61. return NULL;
  62. }
  63. // make sure ends with null
  64. pwsz[i - 1] = 0;
  65. // return the pointer
  66. return pwsz;
  67. }
  68. BOOL IsFileExist(LPCTSTR szFile)
  69. {
  70. // Check if the file has expandable Environment strings
  71. LPTSTR pch = NULL;
  72. pch = _tcschr( (LPTSTR) szFile, _T('%'));
  73. if (pch)
  74. {
  75. TCHAR szValue[_MAX_PATH];
  76. _tcscpy(szValue,szFile);
  77. if (!ExpandEnvironmentStrings( (LPCTSTR)szFile, szValue, sizeof(szValue)/sizeof(TCHAR)))
  78. {_tcscpy(szValue,szFile);}
  79. return (GetFileAttributes(szValue) != 0xFFFFFFFF);
  80. }
  81. else
  82. {
  83. return (GetFileAttributes(szFile) != 0xFFFFFFFF);
  84. }
  85. }
  86. void AddPath(LPTSTR szPath, LPCTSTR szName )
  87. {
  88. LPTSTR p = szPath;
  89. // Find end of the string
  90. while (*p){p = _tcsinc(p);}
  91. // If no trailing backslash then add one
  92. if (*(_tcsdec(szPath, p)) != _T('\\'))
  93. {_tcscat(szPath, _T("\\"));}
  94. // if there are spaces precluding szName, then skip
  95. while ( *szName == ' ' ) szName = _tcsinc(szName);;
  96. // Add new name to existing path string
  97. _tcscat(szPath, szName);
  98. }
  99. int DoesThisSectionExist(IN HINF hFile, IN LPCTSTR szTheSection)
  100. {
  101. int iReturn = FALSE;
  102. INFCONTEXT Context;
  103. // go to the beginning of the section in the INF file
  104. if (SetupFindFirstLine(hFile, szTheSection, NULL, &Context))
  105. {iReturn = TRUE;}
  106. return iReturn;
  107. }
  108. void DoExpandEnvironmentStrings(LPTSTR szFile)
  109. {
  110. TCHAR szValue[_MAX_PATH];
  111. _tcscpy(szValue,szFile);
  112. // Check if the file has expandable Environment strings
  113. LPTSTR pch = NULL;
  114. pch = _tcschr( (LPTSTR) szFile, _T('%'));
  115. if (pch)
  116. {
  117. if (!ExpandEnvironmentStrings( (LPCTSTR)szFile, szValue, sizeof(szValue)/sizeof(TCHAR)))
  118. {
  119. _tcscpy(szValue,szFile);
  120. }
  121. }
  122. _tcscpy(szFile,szValue);
  123. return;
  124. }