123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- #include "stdafx.h"
- #include "resourceIniParse.h"
- #include <time.h>
- #include "BaseFun.h"
- #ifdef RVC_OS_WIN
- #include <windows.h>
- #include <tchar.h>
- #else
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #endif
- int ReadInterger(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szFileName, int iDefaultValue)
- {
- #ifdef RVC_OS_WIN
- return GetPrivateProfileInt(szSection, szKey, iDefaultValue, szFileName);
- #else
- //return GetPrivateProfileIntEx(szSection, szKey, iDefaultValue, szFileName);
- return inifile_read_int(szFileName, szSection, szKey, iDefaultValue);
- #endif
- }
- bool ReadBoolean(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szFileName, bool bDefaultValue)
- {
- TCHAR szResult[MAX_PATH] = {0};
- TCHAR szDefault[MAX_PATH] = {0};
- bool bolResult = false;
-
- #ifdef RVC_OS_WIN
- _stprintf_s(szDefault, _T("%s"), bDefaultValue ? _T("True") : _T("False"));
- GetPrivateProfileString(szSection, szKey, (LPCTSTR)szDefault, szResult, MAX_PATH, szFileName);
- bolResult = (_tcscmp(szResult, _T("True")) == 0 || _tcscmp(szResult, _T("true")) == 0) ? true : false;
- #else
- snprintf(szDefault, MAX_PATH, "%s", bDefaultValue ? "True" : "False");
- inifile_read_str_s(szSection, szKey, (LPCTSTR)szDefault, szResult, MAX_PATH, szFileName);
- //GetPrivateProfileStringEx(szSection, szKey, (LPCTSTR)szDefault, szResult, MAX_PATH, szFileName);
- bolResult = (strcmp(szResult, "True") == 0 || strcmp(szResult, "true") == 0) ? true : false;
- #endif
- return bolResult;
- }
- string ReadString(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szFileName, LPCTSTR strDefaultValue)
- {
- TCHAR tempResult[MAX_PATH*2] = {0};
- #ifdef RVC_OS_WIN
- GetPrivateProfileString(szSection, szKey, strDefaultValue, tempResult, MAX_PATH * 2, szFileName);
- #else
- inifile_read_str_s(szSection, szKey, strDefaultValue, tempResult, MAX_PATH * 2, szFileName);
- //GetPrivateProfileStringEx(szSection, szKey, strDefaultValue, tempResult, MAX_PATH * 2, szFileName);
- #endif
- return string(tempResult);
- }
- void WriteBoolean(LPCTSTR szSection, LPCTSTR szKey, BOOL szBool, LPCTSTR szFileName)
- {
- TCHAR szDefault[MAX_PATH] = {0};
- #ifdef RVC_OS_WIN
- _stprintf_s(szDefault, _T("%d"), szBool);
- WritePrivateProfileString(szSection, szKey, szDefault, szFileName);
- #else
- snprintf(szDefault, MAX_PATH, "%d", szBool);
- inifile_write_str(szSection, szKey, szDefault, szFileName);
- //WritePrivateProfileStringEx(szSection, szKey, szDefault, szFileName);
- #endif
- }
- void WriteInt(LPCTSTR szSection, LPCTSTR szKey, int szInt, LPCTSTR szFileName)
- {
- TCHAR szDefault[MAX_PATH] = { 0 };
- #ifdef RVC_OS_WIN
- _stprintf_s(szDefault, _T("%d"), szInt);
- WritePrivateProfileString(szSection, szKey, szDefault, szFileName);
- #else
- //snprintf(szDefault, MAX_PATH, "%d", szInt);
- inifile_write_int(szFileName,szSection, szKey, szInt);
- //WritePrivateProfileStringEx(szSection, szKey, szDefault, szFileName);
- #endif
- }
- void WriteString(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szString, LPCTSTR szFileName)
- {
- #ifdef RVC_OS_WIN
- WritePrivateProfileString(szSection, szKey, szString, szFileName);
- #else
- inifile_write_str(szSection, szKey, szString, szFileName);
- //WritePrivateProfileStringEx(szSection, szKey, szString, szFileName);
- #endif
- }
- bool checkInPlayTime(string playTime, bool checkCurTime)
- {
- //get localTime
- struct tm local;
- time_t t;
- #ifdef RVC_OS_WIN
- t = time(NULL);
- localtime_s(&local, &t);
- #else
- time(&t);
- local = *localtime(&t);
- #endif
- int t_beginHour, t_beginMin, t_endHour, t_endMin;
- t_beginHour = t_beginMin = t_endHour = t_endMin = 0;
- try
- {
- #ifdef RVC_OS_WIN
- sscanf_s(playTime.c_str(), "%d:%d-%d:%d", &t_beginHour, &t_beginMin, &t_endHour, &t_endMin);
- #else
- sscanf(playTime.c_str(), "%d:%d-%d:%d", &t_beginHour, &t_beginMin, &t_endHour, &t_endMin);
- #endif // RVC_OS_WIN
- long curTime = local.tm_hour * 3600 + local.tm_min * 60 + local.tm_sec;
- long beginTime = t_beginHour * 3600 + t_beginMin * 60;
- long endTime = t_endHour * 3600 + t_endMin * 60 + 60;
- if (checkCurTime)
- {
- if (curTime > beginTime && curTime < endTime)
- return true;
- else
- return false;
- }
- }
- catch (exception* e)
- {
- return false;
- }
- return true;
- }
- bool checkInVaildTime(string vaildTime, bool checkCurData)
- {
- //get localTime
- struct tm local;
- time_t t;
-
- #ifdef RVC_OS_WIN
- t = time(NULL);
- localtime_s(&local, &t);
- #else
- time(&t);
- local = *localtime(&t);
- #endif // RVC_OS_WIN
- vector<string> dateList;
- split(vaildTime, ",", dateList);
- try {
- for (vector<string>::iterator i = dateList.begin(); i != dateList.end(); i++)
- {
- tm beginData = { 0 }, endData = {0};
- int tempYear, tempMon, tempDay, tempYear2, tempMon2, tempDay2;
- tempYear = tempMon = tempDay = tempYear2 = tempMon2 = tempDay2 = 0;
- //ZeroMemory(&beginData, sizeof(tm));
- //ZeroMemory(&endData, sizeof(tm));
- if (-1 == i->find('-')) //单日期
- {
- #ifdef RVC_OS_WIN
- sscanf_s(i->c_str(), "%d/%d/%d", &tempYear, &tempMon, &tempDay);
- #else
- sscanf(i->c_str(), "%d/%d/%d", &tempYear, &tempMon, &tempDay);
- #endif // RVC_OS_WIN
-
- if (checkCurData && (local.tm_year + 1900 == tempYear) && (local.tm_mon + 1 == tempMon) && (local.tm_mday == tempDay))
- return true;//find the data
- }
- else
- {
- #ifdef RVC_OS_WIN
- sscanf_s(i->c_str(), "%d/%d/%d-%d/%d/%d", &tempYear, &tempMon, &tempDay, &tempYear2, &tempMon2, &tempDay2);
- #else
- sscanf(i->c_str(), "%d/%d/%d-%d/%d/%d", &tempYear, &tempMon, &tempDay, &tempYear2, &tempMon2, &tempDay2);
- #endif // RVC_OS_WIN
- beginData.tm_year = tempYear - 1900;
- beginData.tm_mon = tempMon - 1;
- beginData.tm_mday = tempDay;
- endData.tm_year = tempYear2 - 1900;
- endData.tm_mon = tempMon2 - 1;
- endData.tm_mday = tempDay2;
- time_t time_begin = mktime(&beginData), time_end = mktime(&endData) + 24 * 3600; //结束时间+24h
- if (checkCurData)
- {
- if (t > time_begin && t < time_end) {
- return true; //cur data in data region
- }
- else {
- return false;
- }
- }
- }
- }
- }
- catch (exception* e){
- return false;
- }
- return true;
- }
- bool parseResourceIni(const char* filePath, vector<ResourceParse> &ret)
- {
- #ifdef RVC_OS_WIN
- if (INVALID_FILE_ATTRIBUTES == GetFileAttributes(filePath))
- {
- return false;
- }
- #else
- struct stat buf;
- if (stat(filePath, &buf)){
- return false;
- }
- else{
- if (!S_ISREG(buf.st_mode)) {
- return false;
- }
- }
- #endif
-
- ret.clear();
- int mediaNum = ReadInterger(SECTION_GENERAL, GENERAL_MEDIANUM, filePath, 0);
- for (int i = 0; i < mediaNum; i++)
- {
- //get section name
- TCHAR sectionMedia[30] = STR_NULL;
- #ifdef RVC_OS_WIN
- _stprintf_s(sectionMedia, _T("%s%d"), SECTION_MEDIA, i);
- #else
- snprintf(sectionMedia, 30, "%s%d", SECTION_MEDIA, i);
- #endif // RVC_OS_WIN
- //ResourceParse curResource;
- ResourceParse* item = new ResourceParse();
- //获取type属性及校验
- string type = ReadString(sectionMedia, MEDIA_TYPE, filePath, STR_NULL);
- if (string("Pic") == type)
- item->type = 'P';
- else if (string("Video") == type)
- item->type = 'V';
- else//无法识别
- continue;
- item->fullScreen = ReadBoolean(sectionMedia, MEDIA_FULLSCREEN, filePath, false);
- item->primMonitor = ReadBoolean(sectionMedia, MEDIA_PRIMMONITOR, filePath, false);
- item->simpleMode = ReadBoolean(sectionMedia, MEDIA_SIMPLEMODE, filePath, false);
- item->playInterval = ReadInterger(sectionMedia, MEDIA_PLAYINTERVAL, filePath, false);
- item->videoNames = ReadString(sectionMedia, MEDIA_VIDIONAMES, filePath, STR_NULL);
- item->vaildTime = ReadString(sectionMedia, MEDIA_VAILDTIME, filePath, STR_NULL);
- item->playTime = ReadString(sectionMedia, MEDIA_PLAYTIME, filePath, STR_NULL);
- item->priority = ReadInterger(sectionMedia, MEDIA_PRIORITY, filePath, 0);
- if (checkInVaildTime(item->vaildTime) && checkInPlayTime(item->playTime))
- ret.push_back(*item);
- }
- if (0 == ret.size())
- return false;
- return true;
- }
- int ReleaseCResourceArrs(CResourceParse** resourceArr, uint32_t uSize)
- {
- int iRet = 0;
- for (int i = 0; i < uSize; i++) {
- if (resourceArr && resourceArr[i]){
- CResourceParse* pData = resourceArr[i];
- delete pData;
- resourceArr[i] = NULL;
- }
- }
- return iRet;
- }
|