resourceIniParse.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #include "stdafx.h"
  2. #include "resourceIniParse.h"
  3. #include <time.h>
  4. #include "BaseFun.h"
  5. #ifdef RVC_OS_WIN
  6. #include <windows.h>
  7. #include <tchar.h>
  8. #else
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <unistd.h>
  12. #endif
  13. #include <winpr/string.h>
  14. #include "SpUtility.h"
  15. //TODO: CrossPlaform 这个引入的库文件建议废除,用应用框架提供的替代 [Gifur@2025822]
  16. int ReadInterger(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szFileName, int iDefaultValue)
  17. {
  18. #ifdef RVC_OS_WIN
  19. return GetPrivateProfileInt(szSection, szKey, iDefaultValue, szFileName);
  20. #else
  21. //return GetPrivateProfileIntEx(szSection, szKey, iDefaultValue, szFileName);
  22. return inifile_read_int(szFileName, szSection, szKey, iDefaultValue);
  23. #endif
  24. }
  25. bool ReadBoolean(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szFileName, bool bDefaultValue)
  26. {
  27. TCHAR szResult[MAX_PATH] = {0};
  28. TCHAR szDefault[MAX_PATH] = {0};
  29. bool bolResult = false;
  30. #ifdef RVC_OS_WIN
  31. _stprintf_s(szDefault, _T("%s"), bDefaultValue ? _T("True") : _T("False"));
  32. GetPrivateProfileString(szSection, szKey, (LPCTSTR)szDefault, szResult, MAX_PATH, szFileName);
  33. bolResult = (_tcscmp(szResult, _T("True")) == 0 || _tcscmp(szResult, _T("true")) == 0) ? true : false;
  34. #else
  35. snprintf(szDefault, MAX_PATH, "%s", bDefaultValue ? "True" : "False");
  36. inifile_read_str_s(szSection, szKey, (LPCTSTR)szDefault, szResult, MAX_PATH, szFileName);
  37. //GetPrivateProfileStringEx(szSection, szKey, (LPCTSTR)szDefault, szResult, MAX_PATH, szFileName);
  38. bolResult = (strcmp(szResult, "True") == 0 || strcmp(szResult, "true") == 0) ? true : false;
  39. #endif
  40. return bolResult;
  41. }
  42. string ReadString(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szFileName, LPCTSTR strDefaultValue)
  43. {
  44. TCHAR tempResult[MAX_PATH*2] = {0};
  45. #ifdef RVC_OS_WIN
  46. GetPrivateProfileString(szSection, szKey, strDefaultValue, tempResult, MAX_PATH * 2, szFileName);
  47. #else
  48. inifile_read_str_s(szSection, szKey, strDefaultValue, tempResult, MAX_PATH * 2, szFileName);
  49. //GetPrivateProfileStringEx(szSection, szKey, strDefaultValue, tempResult, MAX_PATH * 2, szFileName);
  50. #endif
  51. return string(tempResult);
  52. }
  53. bool checkInPlayTime(string playTime, bool checkCurTime)
  54. {
  55. //get localTime
  56. struct tm local;
  57. time_t t;
  58. #ifdef RVC_OS_WIN
  59. t = time(NULL);
  60. localtime_s(&local, &t);
  61. #else
  62. time(&t);
  63. local = *localtime(&t);
  64. #endif
  65. int t_beginHour, t_beginMin, t_endHour, t_endMin;
  66. t_beginHour = t_beginMin = t_endHour = t_endMin = 0;
  67. try
  68. {
  69. sscanf_s(playTime.c_str(), "%d:%d-%d:%d", &t_beginHour, &t_beginMin, &t_endHour, &t_endMin);
  70. long curTime = local.tm_hour * 3600 + local.tm_min * 60 + local.tm_sec;
  71. long beginTime = t_beginHour * 3600 + t_beginMin * 60;
  72. long endTime = t_endHour * 3600 + t_endMin * 60 + 60;
  73. if (checkCurTime)
  74. {
  75. if (curTime > beginTime && curTime < endTime)
  76. return true;
  77. else
  78. return false;
  79. }
  80. }
  81. catch (exception* e)
  82. {
  83. return false;
  84. }
  85. return true;
  86. }
  87. bool checkInVaildTime(string vaildTime, bool checkCurData)
  88. {
  89. //get localTime
  90. struct tm local;
  91. time_t t;
  92. #ifdef RVC_OS_WIN
  93. t = time(NULL);
  94. localtime_s(&local, &t);
  95. #else
  96. time(&t);
  97. local = *localtime(&t);
  98. #endif // RVC_OS_WIN
  99. vector<string> dateList;
  100. SP::Utility::split2(vaildTime, ",", dateList);
  101. try {
  102. for (vector<string>::iterator i = dateList.begin(); i != dateList.end(); i++)
  103. {
  104. tm beginData = { 0 }, endData = {0};
  105. int tempYear, tempMon, tempDay, tempYear2, tempMon2, tempDay2;
  106. tempYear = tempMon = tempDay = tempYear2 = tempMon2 = tempDay2 = 0;
  107. if (-1 == i->find('-')) //单日期
  108. {
  109. sscanf_s(i->c_str(), "%d/%d/%d", &tempYear, &tempMon, &tempDay);
  110. if (checkCurData && (local.tm_year + 1900 == tempYear) && (local.tm_mon + 1 == tempMon) && (local.tm_mday == tempDay))
  111. return true;//find the data
  112. }
  113. else
  114. {
  115. sscanf_s(i->c_str(), "%d/%d/%d-%d/%d/%d", &tempYear, &tempMon, &tempDay, &tempYear2, &tempMon2, &tempDay2);
  116. beginData.tm_year = tempYear - 1900;
  117. beginData.tm_mon = tempMon - 1;
  118. beginData.tm_mday = tempDay;
  119. endData.tm_year = tempYear2 - 1900;
  120. endData.tm_mon = tempMon2 - 1;
  121. endData.tm_mday = tempDay2;
  122. time_t time_begin = mktime(&beginData), time_end = mktime(&endData) + 24 * 3600; //结束时间+24h
  123. if (checkCurData)
  124. {
  125. if (t > time_begin && t < time_end) {
  126. return true; //cur data in data region
  127. }
  128. else {
  129. return false;
  130. }
  131. }
  132. }
  133. }
  134. }
  135. catch (exception* e){
  136. return false;
  137. }
  138. return true;
  139. }
  140. bool parseResourceIni(const char* filePath, vector<ResourceParse> &ret)
  141. {
  142. /** 这段代码的意义在哪?如果只是判断文件在不在,直接用ExistFileA的跨平台函数即可 [Gifur@2025822]*/
  143. #ifdef RVC_OS_WIN
  144. if (INVALID_FILE_ATTRIBUTES == GetFileAttributes(filePath))
  145. {
  146. return false;
  147. }
  148. #else
  149. struct stat buf;
  150. if (stat(filePath, &buf)){
  151. return false;
  152. }
  153. else{
  154. if (!S_ISREG(buf.st_mode)) {
  155. return false;
  156. }
  157. }
  158. #endif
  159. ret.clear();
  160. int mediaNum = ReadInterger(SECTION_GENERAL, GENERAL_MEDIANUM, filePath, 0);
  161. for (int i = 0; i < mediaNum; i++)
  162. {
  163. //get section name
  164. TCHAR sectionMedia[30] = STR_NULL;
  165. sprintf_s(sectionMedia, 30, "%s%d", SECTION_MEDIA, i);
  166. ResourceParse* item = new ResourceParse();
  167. //获取type属性及校验
  168. string type = ReadString(sectionMedia, MEDIA_TYPE, filePath, STR_NULL);
  169. if (string("Pic") == type)
  170. item->type = 'P';
  171. else if (string("Video") == type)
  172. item->type = 'V';
  173. else//无法识别
  174. continue;
  175. item->fullScreen = ReadBoolean(sectionMedia, MEDIA_FULLSCREEN, filePath, false);
  176. item->primMonitor = ReadBoolean(sectionMedia, MEDIA_PRIMMONITOR, filePath, false);
  177. item->simpleMode = ReadBoolean(sectionMedia, MEDIA_SIMPLEMODE, filePath, false);
  178. item->playInterval = ReadInterger(sectionMedia, MEDIA_PLAYINTERVAL, filePath, false);
  179. item->videoNames = ReadString(sectionMedia, MEDIA_VIDIONAMES, filePath, STR_NULL);
  180. item->vaildTime = ReadString(sectionMedia, MEDIA_VAILDTIME, filePath, STR_NULL);
  181. item->playTime = ReadString(sectionMedia, MEDIA_PLAYTIME, filePath, STR_NULL);
  182. item->priority = ReadInterger(sectionMedia, MEDIA_PRIORITY, filePath, 0);
  183. if (checkInVaildTime(item->vaildTime) && checkInPlayTime(item->playTime))
  184. ret.push_back(*item);
  185. }
  186. if (0 == ret.size())
  187. return false;
  188. return true;
  189. }