📄 licensefile.cpp
字号:
BYTE newAttribute = 0x20; //ReadOnly strcpy(pFileName, psFilename); CFileStatus filestatus; CFile::GetStatus( pFileName, filestatus ); filestatus.m_attribute = newAttribute; CFile::SetStatus( pFileName, filestatus );#endif return TRUE;}/************************************************* Function: ~LicenseConfigFile Description: LicenseConfigFile的析构函数释放申请的资源 Calls: LicenseConfigFile::CloseFile Called By: 系统自动调用 Input: 无 Output: 无 Return: 无 *************************************************/LicenseConfigFile::~LicenseConfigFile(){ CloseFile();}/************************************************* Function: IsOpen Description: 返回读取配置文件是否成功的标志 Calls: 无 Called By: LicenseConfigFile::GetItemValue Input: 无 Output: 无 Return: 如果配置文件读取成功,返回TRUE,否则返回FALSE *************************************************/unsigned long LicenseConfigFile::IsOpen(){ return m_bIsOpen;}/************************************************* Function: GetItemValue数字 Description: 从内存中取指定的数字类型的键值,如果不存在,则使用指定的缺省值 Calls: LicenseConfigFile::GetItemValue字符串 Called By: License_Manager::initialize_license(LicenseConfigFile* cfgfile) Input: pszSectionName - 以NULL结尾的字符串指针,指示包含Key的片断 pszKeyName - 以NULL结尾的字符串指针,指示需要返回值的Key lDefaultValue - 取值失败后使用的缺省值 Output: ulReturnedValue - 指定用于接收结果的缓冲区地址 Return: 成功返回TRUE, 失败返回FALSE Others: 本函数不是UNICODE版本*************************************************/unsigned long LicenseConfigFile::GetItemValue(const char *pszSectionName, const char *pszKeyName, INT32 &lReturnedValue, INT32 lDefaultValue){ if (0 == GetItemValue(pszSectionName, pszKeyName, lReturnedValue)) { lReturnedValue = lDefaultValue; return FALSE; } return TRUE;}/************************************************* Function: GetItemValue数字 Description: 从内存中取指定的数字类型的键值 Calls: LicenseConfigFile::GetItemValue字符串 Called By: License_Manager::initialize_license(LicenseConfigFile* cfgfile) Input: pszSectionName - 以NULL结尾的字符串指针,指示包含Key的片断 pszKeyName - 以NULL结尾的字符串指针,指示需要返回值的Key Output: ulReturnedValue - 指定用于接收结果的缓冲区地址 Return: 成功返回TRUE, 失败返回FALSE Others: 本函数不是UNICODE版本*************************************************/unsigned long LicenseConfigFile::GetItemValue(const char *pszSectionName, const char *pszKeyName, INT32 &lReturnedValue){ char *tmpBuf; char szBuf[100] = { 0 }; if (0 == GetItemValue(pszSectionName, pszKeyName, szBuf, 100)) return FALSE; tmpBuf = szBuf; do { if(!isdigit(*tmpBuf)) return FALSE; tmpBuf++; } while( *tmpBuf != '\0' ); lReturnedValue = atol(szBuf); return TRUE; }/************************************************* Function: GetItemValue字符串 Description: 从内存中取指定的字符串类型的键值,如果不存在,则使用指定的缺省值 Calls: LicenseConfigFile::GetItemValue字符串 Called By: License_Manager::initialize_license(LicenseConfigFile* cfgfile) Input: pszSectionName - 以NULL结尾的字符串指针,指示包含Key的片断 pszKeyName - 以NULL结尾的字符串指针,指示需要返回值的Key nSize - 指定接收缓冲区的大小 pszDefaultValue - 取值失败后使用的缺省值 Output: pszReturnedString - 指定用于接收结果的缓冲区地址 Return: 返回缓冲区中的有效字符个数。不包括字符串结尾的NULL Others: 本函数不是UNICODE版本*************************************************/unsigned long LicenseConfigFile::GetItemValue(const char *pszSectionName, const char *pszKeyName, char *pszReturnedString, unsigned long nSize, const char *pszDefaultValue){ unsigned long len; if (nSize <=0 ) return 0; len = GetItemValue(pszSectionName, pszKeyName, pszReturnedString, nSize); if (0 == len) { strncpy(pszReturnedString, pszDefaultValue, nSize-1); pszReturnedString[nSize-1] = '\0'; return strlen(pszReturnedString); } return len;}/************************************************* Function: GetItemValue字符串 Description: 从内存缓冲区中找到KeyName,将值拷贝到指定的空间。 如果返回值大于空间的大小,则对字符串进行截尾处理, 并在缓冲区的最后一个字节加上NULL。 当缓冲区的最后两个字符是汉字编码时,将自动加上两个结束符 Calls: LicenseConfigFile::IsOpen, LicenseConfigFile::LocateKey, LicenseConfigFile::LocateSection Called By: License_Manager::initialize_license(LicenseConfigFile* cfgfile) Input: pszSectionName - 以NULL结尾的字符串指针,指示包含Key的片断 pszKeyName - 以NULL结尾的字符串指针,指示需要返回值的Key nSize - 指定接收缓冲区的大小 Output: pszReturnedString - 指定用于接收结果的缓冲区地址 Return: 返回缓冲区中的有效字符个数。不包括字符串结尾的NULL Others: 本函数不是UNICODE版本*************************************************/unsigned long LicenseConfigFile::GetItemValue(const char *pszSectionName, const char *pszKeyName, char *pszReturnedString, unsigned long nSize){ char *pszSectionBegin, *pszSectionEnd; char *pszValueBegin, *pszValueEnd; unsigned long dwCount; /* 检查对象是否初始化成功 */ if (FALSE == IsOpen()) { return (unsigned long)0; } /* 检查传入参数合法性 */ if ((NULL == pszSectionName) || (NULL == pszKeyName) || (NULL == pszReturnedString)) { return (unsigned long)0; } if (nSize == 0) { return (unsigned long)0; } /* 查找SectionName,定位Section的开始和结尾指针 */ if (FALSE == LocateSection(pszSectionName, pszSectionBegin, pszSectionEnd)) { return (unsigned long)0; } /* 在指定范围内定位KeyName的Value */ if (FALSE == LocateKeyValue( pszKeyName, pszSectionBegin, pszSectionEnd, pszValueBegin, pszValueEnd )) { /* Key没找到{,将pszDefault的值作为返回值} */ return (unsigned long)0; } /* 将需要的值拷贝到缓冲区中,并注意缓冲区长度 */ dwCount = 0; for (; pszValueBegin < pszValueEnd && dwCount < (nSize-1); pszValueBegin++, dwCount++) pszReturnedString[dwCount] = *pszValueBegin; pszReturnedString[dwCount] = '\0'; /* 字符串被截断,判断,最后一个字符是否为双字节 */ if ((dwCount == nSize-1) && ((BYTE)(pszReturnedString[dwCount-1]) > 0x7f)) { /* 将双字节的最后一个字符设置为'\0' */ /* 为了防止出现如下情况,导致以后的字符串处理越界 */ /* "\xa9" */ pszReturnedString[dwCount-1] = '\0'; dwCount --; } return (unsigned long)dwCount;}/************************************************* Function: LocateSection Description: 在指定的缓冲区范围中搜索Section,返回与Section的开始地址和结束地址。 Calls: LicenseConfigFile::LocateStr LicenseConfigFile::MapToContent LicenseConfigFile::ToLower Called By: LicenseConfigFile::GetItemValue LicenseConfigFile::SetItemValue Input: pszSectionName - 以NULL结尾的字符串指针,指示需要返回值的Section Output: pszSectionBegin - 接收返回值的开始地址 指向Section行的下一行第一个字节 pszSectionEnd - 接收返回值的结束地址 指向最后一个有效字节的下一个地址 Return: 定位成功,返回TRUE;失败,返回FALSE Others: 本函数不是UNICODE版本*************************************************/unsigned long LicenseConfigFile::LocateSection(const char *pszSectionName, char * &pszSectionBegin, char * &pszSectionEnd){ char *pszLowerSection; char *pszSectionBeginOnShadow; unsigned long bIsFirstValidCharOnLine; char *pR; /* 参数合法性检查 */ if (NULL == pszSectionName) { return FALSE; } /* 冗余检查代码,不用测试 */ if ((NULL == m_pszContent) || (NULL == m_pszShadow)) { return FALSE; } /* 将SectionName转换成小写 */ pszLowerSection = new char [strlen(pszSectionName) + 2 + 1]; sprintf(pszLowerSection, "[%s]", pszSectionName); ToLower(pszLowerSection, strlen(pszLowerSection)); /* 在Shadow中定位,然后,计算出正确的指针 */ /* 得到Key在Shadow中的位置 */ pszSectionBeginOnShadow = LocateStr( pszLowerSection, m_pszShadow, m_pszShadow + m_nSize ); if (NULL == pszSectionBeginOnShadow) { /* 释放资源 */ delete [] pszLowerSection; return FALSE; } pszSectionBegin = MapToContent(pszSectionBeginOnShadow) + strlen(pszLowerSection); /* 将SectionBegin指针指向Section的下一行行首字节 */ /* 过滤行末字符 */ for (; pszSectionBegin < (m_pszContent + m_nSize); pszSectionBegin++) { if ((*pszSectionBegin == '\r') || (*pszSectionBegin == '\n')) { break; } } /* 过滤行末回车换行 */ for (; pszSectionBegin < (m_pszContent + m_nSize); pszSectionBegin++) { if ((*pszSectionBegin != '\r') && (*pszSectionBegin != '\n')) { break; } } /* 释放资源 */ delete [] pszLowerSection; /* 寻找下一行有效字符以'['开头的行 */ bIsFirstValidCharOnLine = TRUE; pR = pszSectionBegin; for (; pR < (m_pszContent + m_nSize + 1); pR++) { if (bIsFirstValidCharOnLine && *pR == '[') { break; } if (*pR == '\0') { break; } if (*pR == '\r' || *pR == '\n') { bIsFirstValidCharOnLine = TRUE; /* pszSectionEnd = pR; */ } else if ((*pR != ' ') && (*pR != '\t')) { bIsFirstValidCharOnLine = FALSE; } } pszSectionEnd = pR; return TRUE;}/************************************************* Function: LocateKeyRange Description: 在指定的缓冲区范围中搜索Key,返回与Key匹配的值的开始地址和结束地址。 注意:指定范围的结尾地址和返回的取值结束地址,都是指向最后一个有效 空间后面的地址。 Calls: LicenseConfigFile::LocateKeyRange LicenseConfigFile::LocateStr LicenseConfigFile::MapToContent LicenseConfigFile::MapToShadow
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -