123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- #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 0;
- #endif
- }
- bool ReadBoolean(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szFileName, bool bDefaultValue)
- {
- TCHAR szResult[255] = {0};
- TCHAR szDefault[255] = {0};
- bool bolResult = false;
-
- #ifdef RVC_OS_WIN
- _stprintf_s(szDefault, _T("%s"), bDefaultValue ? _T("True") : _T("False"));
- GetPrivateProfileString(szSection, szKey, (LPCTSTR)szDefault, szResult, 255, szFileName);
- bolResult = (_tcscmp(szResult, _T("True")) == 0 || _tcscmp(szResult, _T("true")) == 0) ? true : false;
- #else
- #endif
- return bolResult;
- }
- string ReadString(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szFileName, LPCTSTR strDefaultValue)
- {
- TCHAR tempResult[512] = {0};
- #ifdef RVC_OS_WIN
- GetPrivateProfileString(szSection, szKey, strDefaultValue, tempResult, 512, szFileName);
- #else
- #endif
- return string(tempResult);
- }
- void WriteBoolean(LPCTSTR szSection, LPCTSTR szKey, BOOL szBool, LPCTSTR szFileName)
- {
- TCHAR szDefault[255] = {0};
- #ifdef RVC_OS_WIN
- _stprintf_s(szDefault, _T("%d"), szBool);
- WritePrivateProfileString(szSection, szKey, szDefault, szFileName);
- #else
- #endif
- }
- void WriteInt(LPCTSTR szSection, LPCTSTR szKey, INT szInt, LPCTSTR szFileName)
- {
- TCHAR szDefault[255] = {0};
- #ifdef RVC_OS_WIN
- _stprintf_s(szDefault, _T("%d"), szInt);
- WritePrivateProfileString(szSection, szKey, szDefault, szFileName);
- #else
- #endif
- }
- void WriteString(LPCTSTR szSection, LPCTSTR szKey, LPCTSTR szString, LPCTSTR szFileName)
- {
- #ifdef RVC_OS_WIN
- WritePrivateProfileString(szSection, szKey, szString, szFileName);
- #else
- #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
- {
- sscanf_s(playTime.c_str(), "%d:%d-%d:%d", &t_beginHour, &t_beginMin, &t_endHour, &t_endMin);
- 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, endData;
- 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('-')) //单日期
- {
- sscanf_s(i->c_str(), "%d/%d/%d", &tempYear, &tempMon, &tempDay);
- if (checkCurData && (local->tm_year + 1900 == tempYear) && (local->tm_mon + 1 == tempMon) && (local->tm_mday == tempDay))
- return true;//find the data
- }
- else
- {
- sscanf_s(i->c_str(), "%d/%d/%d-%d/%d/%d", &tempYear, &tempMon, &tempDay, &tempYear2, &tempMon2, &tempDay2);
- 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(LPCTSTR 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;
- }
- #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
- sprintf_s(sectionMedia, 30, "%s%d", SECTION_MEDIA, i);
- #endif // RVC_OS_WIN
- ResourceParse curResource;
- //获取type属性及校验
- string type = ReadString(sectionMedia, MEDIA_TYPE, filePath, STR_NULL);
- if (string("Pic") == type)
- curResource.type = 'P';
- else if (string("Video") == type)
- curResource.type = 'V';
- else//无法识别
- continue;
- curResource.fullScreen = ReadBoolean(sectionMedia, MEDIA_FULLSCREEN, filePath, false);
- curResource.primMonitor = ReadBoolean(sectionMedia, MEDIA_PRIMMONITOR, filePath, false);
- curResource.simpleMode = ReadBoolean(sectionMedia, MEDIA_SIMPLEMODE, filePath, false);
- curResource.playInterval = ReadInterger(sectionMedia, MEDIA_PLAYINTERVAL, filePath, false);
- curResource.videoNames = ReadString(sectionMedia, MEDIA_VIDIONAMES, filePath, STR_NULL);
- curResource.vaildTime = ReadString(sectionMedia, MEDIA_VAILDTIME, filePath, STR_NULL);
- curResource.playTime = ReadString(sectionMedia, MEDIA_PLAYTIME, filePath, STR_NULL);
- curResource.priority = ReadInterger(sectionMedia, MEDIA_PRIORITY, filePath, 0);
- if (checkInVaildTime(curResource.vaildTime) && checkInPlayTime(curResource.playTime))
- ret.push_back(curResource);
- }
- if (0 == ret.size())
- return false;
- return true;
- }
|