resourceIniParse.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. int ReadInterger(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szFileName, int iDefaultValue)
  14. {
  15. #ifdef RVC_OS_WIN
  16. return GetPrivateProfileInt(szSection, szKey, iDefaultValue, szFileName);
  17. #else
  18. return 0;
  19. #endif
  20. }
  21. bool ReadBoolean(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szFileName, bool bDefaultValue)
  22. {
  23. TCHAR szResult[255] = {0};
  24. TCHAR szDefault[255] = {0};
  25. bool bolResult = false;
  26. #ifdef RVC_OS_WIN
  27. _stprintf_s(szDefault, _T("%s"), bDefaultValue ? _T("True") : _T("False"));
  28. GetPrivateProfileString(szSection, szKey, (LPCTSTR)szDefault, szResult, 255, szFileName);
  29. bolResult = (_tcscmp(szResult, _T("True")) == 0 || _tcscmp(szResult, _T("true")) == 0) ? true : false;
  30. #else
  31. #endif
  32. return bolResult;
  33. }
  34. string ReadString(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szFileName, LPCTSTR strDefaultValue)
  35. {
  36. TCHAR tempResult[512] = {0};
  37. #ifdef RVC_OS_WIN
  38. GetPrivateProfileString(szSection, szKey, strDefaultValue, tempResult, 512, szFileName);
  39. #else
  40. #endif
  41. return string(tempResult);
  42. }
  43. void WriteBoolean(LPCTSTR szSection, LPCTSTR szKey, BOOL szBool, LPCTSTR szFileName)
  44. {
  45. TCHAR szDefault[255] = {0};
  46. #ifdef RVC_OS_WIN
  47. _stprintf_s(szDefault, _T("%d"), szBool);
  48. WritePrivateProfileString(szSection, szKey, szDefault, szFileName);
  49. #else
  50. #endif
  51. }
  52. void WriteInt(LPCTSTR szSection, LPCTSTR szKey, INT szInt, LPCTSTR szFileName)
  53. {
  54. TCHAR szDefault[255] = {0};
  55. #ifdef RVC_OS_WIN
  56. _stprintf_s(szDefault, _T("%d"), szInt);
  57. WritePrivateProfileString(szSection, szKey, szDefault, szFileName);
  58. #else
  59. #endif
  60. }
  61. void WriteString(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szString, LPCTSTR szFileName)
  62. {
  63. #ifdef RVC_OS_WIN
  64. WritePrivateProfileString(szSection, szKey, szString, szFileName);
  65. #else
  66. #endif
  67. }
  68. bool checkInPlayTime(string playTime, bool checkCurTime)
  69. {
  70. //get localTime
  71. struct tm *local;
  72. time_t t;
  73. #ifdef RVC_OS_WIN
  74. t = time(NULL);
  75. localtime_s(local, &t);
  76. #else
  77. time(&t);
  78. local = localtime(&t);
  79. #endif
  80. int t_beginHour, t_beginMin, t_endHour, t_endMin;
  81. t_beginHour = t_beginMin = t_endHour = t_endMin = 0;
  82. try
  83. {
  84. sscanf_s(playTime.c_str(), "%d:%d-%d:%d", &t_beginHour, &t_beginMin, &t_endHour, &t_endMin);
  85. long curTime = local->tm_hour * 3600 + local->tm_min * 60 + local->tm_sec;
  86. long beginTime = t_beginHour * 3600 + t_beginMin * 60;
  87. long endTime = t_endHour * 3600 + t_endMin * 60 + 60;
  88. if (checkCurTime)
  89. {
  90. if (curTime > beginTime && curTime < endTime)
  91. return true;
  92. else
  93. return false;
  94. }
  95. }
  96. catch (exception* e)
  97. {
  98. return false;
  99. }
  100. return true;
  101. }
  102. bool checkInVaildTime(string vaildTime, bool checkCurData)
  103. {
  104. //get localTime
  105. struct tm* local;
  106. time_t t;
  107. #ifdef RVC_OS_WIN
  108. t = time(NULL);
  109. localtime_s(local, &t);
  110. #else
  111. time(&t);
  112. local = localtime(&t);
  113. #endif // RVC_OS_WIN
  114. vector<string> dateList;
  115. split(vaildTime, ",", dateList);
  116. try {
  117. for (vector<string>::iterator i = dateList.begin(); i != dateList.end(); i++)
  118. {
  119. tm beginData, endData;
  120. int tempYear, tempMon, tempDay, tempYear2, tempMon2, tempDay2;
  121. tempYear = tempMon = tempDay = tempYear2 = tempMon2 = tempDay2 = 0;
  122. ZeroMemory(&beginData, sizeof(tm));
  123. ZeroMemory(&endData, sizeof(tm));
  124. if (-1 == i->find('-')) //单日期
  125. {
  126. sscanf_s(i->c_str(), "%d/%d/%d", &tempYear, &tempMon, &tempDay);
  127. if (checkCurData && (local->tm_year + 1900 == tempYear) && (local->tm_mon + 1 == tempMon) && (local->tm_mday == tempDay))
  128. return true;//find the data
  129. }
  130. else
  131. {
  132. sscanf_s(i->c_str(), "%d/%d/%d-%d/%d/%d", &tempYear, &tempMon, &tempDay, &tempYear2, &tempMon2, &tempDay2);
  133. beginData.tm_year = tempYear - 1900;
  134. beginData.tm_mon = tempMon - 1;
  135. beginData.tm_mday = tempDay;
  136. endData.tm_year = tempYear2 - 1900;
  137. endData.tm_mon = tempMon2 - 1;
  138. endData.tm_mday = tempDay2;
  139. time_t time_begin = mktime(&beginData), time_end = mktime(&endData) + 24 * 3600; //结束时间+24h
  140. if (checkCurData)
  141. {
  142. if (t > time_begin && t < time_end)
  143. return true; //cur data in data region
  144. else
  145. return false;
  146. }
  147. }
  148. }
  149. }
  150. catch (exception* e)
  151. {
  152. return false;
  153. }
  154. return true;
  155. }
  156. bool parseResourceIni(LPCTSTR filePath, vector<ResourceParse> &ret)
  157. {
  158. #ifdef RVC_OS_WIN
  159. if (INVALID_FILE_ATTRIBUTES == GetFileAttributes(filePath))
  160. {
  161. return FALSE;
  162. }
  163. #else
  164. struct stat buf;
  165. if (stat(filePath, &buf))
  166. {
  167. return FALSE;
  168. }
  169. #endif
  170. ret.clear();
  171. int mediaNum = ReadInterger(SECTION_GENERAL, GENERAL_MEDIANUM, filePath, 0);
  172. for (int i = 0; i < mediaNum; i++)
  173. {
  174. //get section name
  175. TCHAR sectionMedia[30] = STR_NULL;
  176. #ifdef RVC_OS_WIN
  177. _stprintf_s(sectionMedia, _T("%s%d"), SECTION_MEDIA, i);
  178. #else
  179. sprintf_s(sectionMedia, 30, "%s%d", SECTION_MEDIA, i);
  180. #endif // RVC_OS_WIN
  181. ResourceParse curResource;
  182. //获取type属性及校验
  183. string type = ReadString(sectionMedia, MEDIA_TYPE, filePath, STR_NULL);
  184. if (string("Pic") == type)
  185. curResource.type = 'P';
  186. else if (string("Video") == type)
  187. curResource.type = 'V';
  188. else//无法识别
  189. continue;
  190. curResource.fullScreen = ReadBoolean(sectionMedia, MEDIA_FULLSCREEN, filePath, false);
  191. curResource.primMonitor = ReadBoolean(sectionMedia, MEDIA_PRIMMONITOR, filePath, false);
  192. curResource.simpleMode = ReadBoolean(sectionMedia, MEDIA_SIMPLEMODE, filePath, false);
  193. curResource.playInterval = ReadInterger(sectionMedia, MEDIA_PLAYINTERVAL, filePath, false);
  194. curResource.videoNames = ReadString(sectionMedia, MEDIA_VIDIONAMES, filePath, STR_NULL);
  195. curResource.vaildTime = ReadString(sectionMedia, MEDIA_VAILDTIME, filePath, STR_NULL);
  196. curResource.playTime = ReadString(sectionMedia, MEDIA_PLAYTIME, filePath, STR_NULL);
  197. curResource.priority = ReadInterger(sectionMedia, MEDIA_PRIORITY, filePath, 0);
  198. if (checkInVaildTime(curResource.vaildTime) && checkInPlayTime(curResource.playTime))
  199. ret.push_back(curResource);
  200. }
  201. if (0 == ret.size())
  202. return false;
  203. return true;
  204. }