resourceIniParse.cpp 7.9 KB

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