Profile.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Profile.cpp: implementation of the Profile class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include<stdio.h>
  5. #include<string.h>
  6. #include "Profile.h"
  7. #include<ctype.h>
  8. //////////////////////////////////////////////////////////////////////
  9. // Construction/Destruction
  10. //////////////////////////////////////////////////////////////////////
  11. char* _strlwr(char *string)
  12. {
  13. for (char *p = string; p < string + strlen(string); p++)
  14. {
  15. if (isupper(*p))
  16. {
  17. *p = tolower(*p);
  18. }
  19. }
  20. return string;
  21. }
  22. CProfile::CProfile()
  23. {
  24. }
  25. CProfile::~CProfile()
  26. {
  27. }
  28. /*
  29. *return value:-3:open file failed;-2:parameter error;-1:system error;other:return character string length
  30. */
  31. DWORD CProfile::GetPrivateProfileString(
  32. const char* lpAppName, // section name
  33. const char* lpKeyName, // key name
  34. const char* lpDefault, // default string
  35. char* lpReturnedString, // destination buffer
  36. DWORD nSize, // size of destination buffer
  37. const char* lpFileName // initialization file name
  38. )
  39. {
  40. if(NULL == lpReturnedString || nSize <= 0)
  41. {
  42. return -2;
  43. }
  44. memset(lpReturnedString, 0, nSize);
  45. fpos_t pos = {0};
  46. char* pBuf = NULL;
  47. FILE* pFile = fopen(lpFileName, "r");
  48. if(NULL == pFile)
  49. {
  50. return -3;
  51. }
  52. fseek(pFile, 0, SEEK_END);
  53. if( fgetpos(pFile, &pos) != 0)
  54. {
  55. fclose(pFile);
  56. return -1;
  57. }
  58. int npos = 0;
  59. #if (defined(WIN32)||defined(WIN64))
  60. npos = pos;
  61. #else
  62. #if defined(__APPLE__) && defined(__MACH__)
  63. #include <TargetConditionals.h>
  64. #if TARGET_OS_MAC == 1
  65. npos = pos;
  66. #endif
  67. #else
  68. npos = pos.__pos;
  69. #endif
  70. #endif
  71. if(npos <= 0)
  72. {
  73. fclose(pFile);
  74. return -1;
  75. }
  76. pBuf = new char[npos+1];
  77. if(NULL == pBuf)
  78. {
  79. return -1;
  80. }
  81. memset(pBuf, 0, npos+1);
  82. fseek(pFile, 0, SEEK_SET);
  83. int nRead = fread(pBuf, sizeof(char), (int)npos, pFile);
  84. fclose(pFile);
  85. _strlwr(pBuf);
  86. char szAppName[128] = {0};
  87. char szKeyName[128] = {0};
  88. strncpy(szAppName, lpAppName, sizeof(szAppName)-1);
  89. strncpy(szKeyName, lpKeyName, sizeof(szKeyName)-1);
  90. _strlwr(szAppName);
  91. _strlwr(szKeyName);
  92. char* pLeftSeciton = NULL;
  93. char* pRightSection = NULL;
  94. char* pKeySection = NULL;
  95. DWORD dwSize = 0;
  96. pLeftSeciton = strstr(pBuf, szAppName);
  97. if(pLeftSeciton != NULL)
  98. {
  99. pRightSection = strstr(pLeftSeciton, "[");
  100. if(pRightSection == NULL)
  101. {
  102. pRightSection = pBuf + nRead;
  103. }
  104. pKeySection = strstr(pLeftSeciton, szKeyName);
  105. }
  106. if(NULL == pLeftSeciton || NULL == pKeySection || pKeySection < pLeftSeciton || pKeySection >pRightSection)//关键字不在范围之内
  107. {
  108. dwSize = nSize<strlen(lpDefault) ? nSize-1 : strlen(lpDefault);
  109. memcpy(lpReturnedString, lpDefault, dwSize);
  110. delete []pBuf;
  111. return dwSize;
  112. }
  113. pLeftSeciton = strstr(pKeySection, "=");
  114. pRightSection = strstr(pKeySection, "\n");
  115. dwSize = nSize<(pRightSection-pLeftSeciton-1) ? nSize-1 : (pRightSection-pLeftSeciton-1);
  116. memcpy(lpReturnedString, pLeftSeciton+1, dwSize);
  117. delete []pBuf;
  118. return dwSize;
  119. }