⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 inifile.h

📁 LINUX下实现对MYSQL数据的操作
💻 H
📖 第 1 页 / 共 2 页
字号:
                    szBuffer[i+k-j] = szBuffer[i];
                strncpy(szBuffer+iIdentPos, pValue, strlen(pValue));
                iBufferMore = k-j;
            }
        } else {
            i = strlen(" = \n")+strlen(pIdent)+strlen(pValue);
            for (j=iBufferLen-1; j>=iKeyPos; j--)
                szBuffer[j+i]=szBuffer[j];
            sprintf(szBuffer+iKeyPos, "%s = %s\n", pIdent, pValue);
            iBufferMore = i;
        }
        bBufferChanged = 1;
    } else {
        sprintf(szBuffer+iBufferLen, 
               "\n[%s]\n%s = %s", pSection, pIdent, pValue);
        iBufferMore = strlen("\n[]\n = ")+strlen(pSection)
                          +strlen(pIdent)+strlen(pValue);
        bBufferChanged = 1;
    }
    iBufferLen += iBufferMore;
    return 0;
}

/**********************************************
Name   : GetIniInteger
Input  : 
                char *pSection  :  Session Name
                char *pIdent:  Identity Name
                int:  Default Value  if an error exists.
Output :
        interger converted, if applicable
                Default value, if an error exists
                
Process: Normal 
***********************************************/
int GetIniInteger(const char* pSection, const char* pIdent, const int iDefVal)
{
    char szTempBuffer[MAX_VALUE_BUFFER_SIZE];
    if ( GetIniString(pSection, pIdent, szTempBuffer) == 0 ) {
     if (strlen(szTempBuffer)>2) {
         if ( (szTempBuffer[0]=='0') &&
              ( (szTempBuffer[1]=='x') || (szTempBuffer[1]=='X')) 
            ) return (int)(strtol(szTempBuffer, (char **)NULL, 16));
     }
     return atoi(szTempBuffer);
    } else return iDefVal;
}

/**********************************************
Name   : GetIniLong
Input  : 
                char *pSection  :  Session Name
                char *pIdent:  Identity Name
                long:  Default Value  if an error exists.
Output :
        Long converted, if applicable
                Default value, if an error exists
                
Process: Normal 
***********************************************/
long GetIniLong(const char* pSection, const char* pIdent, const long iDefVal)
{
    char szTempBuffer[MAX_VALUE_BUFFER_SIZE];
    if ( GetIniString(pSection, pIdent, szTempBuffer) == 0 ) {
    if (strlen(szTempBuffer)>2) {
         if ( (szTempBuffer[0]=='0') &&
              ( (szTempBuffer[1]=='x') || (szTempBuffer[1]=='X')) 
            ) return (strtol(szTempBuffer, (char **)NULL, 16));
     }
    return atol(szTempBuffer);
    } else return iDefVal;
}

/**********************************************
Name   : GetIniDouble
Input  : 
                char *pSection  :  Session Name
                char *pIdent:  Identity Name
                double:  Default Value  if an error exists.
Output :
        Double converted, if applicable
                Default value, if an error exists
                
Process: Normal 
***********************************************/
double GetIniDouble(const char* pSection, const char* pIdent, const double iDefVal)
{
    char szTempBuffer[MAX_VALUE_BUFFER_SIZE];
    if ( GetIniString(pSection, pIdent, szTempBuffer) == 0 ) {
return atof(szTempBuffer);
    } else return iDefVal;
}

/**********************************************
Name   : SetIniInteger
Input  : 
                char *pSection  :  Session Name
                char *pIdent:  Identity Name
                int:  Value to write
Output :
            0 : Successful
            -1: Error
                
Process: Normal 
***********************************************/
int SetIniInteger(const char *pSection, const char *pIdent, const int pValue)
{
    char szTempBuffer[MAX_VALUE_BUFFER_SIZE];
    sprintf(szTempBuffer, "%d", pValue);
    if ( SetIniString(pSection, pIdent, szTempBuffer) == 0 ) {
return 0;
    } else return -1;

}

/**********************************************
Name   : SetIniLong
Input  : 
                char *pSection  :  Session Name
                char *pIdent:  Identity Name
                long:  Value to write
Output :
            0 : Successful
            -1: Error
                
Process: Normal 
***********************************************/
int SetIniLong(const char *pSection, const char *pIdent, const long pValue) 
{
    char szTempBuffer[MAX_VALUE_BUFFER_SIZE];
    sprintf(szTempBuffer, "%ld", pValue);
    if ( SetIniString(pSection, pIdent, szTempBuffer) == 0 ) {
return 0;
    } else return -1;

}

/**********************************************
Name   : SetIniDouble
Input  : 
                char *pSection  :  Session Name
                char *pIdent:  Identity Name
                double:  Value to write
Output :
            0 : Successful
            -1: Error
                
Process: Normal 
***********************************************/
int SetIniDouble(const char *pSection, const char *pIdent, const double pValue)
{
    char szTempBuffer[MAX_VALUE_BUFFER_SIZE];
    sprintf(szTempBuffer, "%g", pValue);
    if ( SetIniString(pSection, pIdent, szTempBuffer) == 0 ) {
return 0;
    } else return -1;

}

/**********************************************
Name   : GetSectionValues
Input  : 
                char *pSection  :  Session Name
                char *pDelimiter:  Delimiter to separate values in the section
                char *pValues   :  Buffer to store returned values in the section
Output :
            0 : Successful
            -1: Error
                
Process: Normal 
***********************************************/
int GetSectionValues(const char *pSection, const char *pDelimiter, char *pValues)
{
    int i=0;
    int j=0; 
    int min;
    int iKeyFound=-1;
    int iDelimLen=strlen(pDelimiter);
    int iSum=0;
    
    if (!strlen(pSection) || !strlen(pDelimiter) || (fpIni == NULL)) return -1;
    
    while (i<iBufferLen) {
        while ((i<iBufferLen) && 
               ((szBuffer[i]==' ') || (szBuffer[i]=='\t'))) i++;
        if (i>=iBufferLen) break;
        if (szBuffer[i]=='#') { //ignore the lines beginning with '#'
            while ((i<iBufferLen) && (szBuffer[i] != '\n')) i++;
            if (i>=iBufferLen) break; 
            //Jump to the next line
            i++;
        } else {
            if (szBuffer[i]=='[') {
                i++;
                while ((i<iBufferLen) && 
                       ((szBuffer[i]==' ') || (szBuffer[i]=='\t'))) i++;
                if (i>=iBufferLen) break;
                if (strncmp(szBuffer+i, pSection, strlen(pSection))==0) { 
                    //key may be found, let's see
                    i += strlen(pSection);
                    while ((i<iBufferLen) && 
                           ((szBuffer[i]==' ') || (szBuffer[i]=='\t'))) i++;
                    if (i>=iBufferLen) break;
                    if (szBuffer[i]==']') {
                     if (iKeyFound==0) break; else iKeyFound=0;
                    } else { if (iKeyFound==0) break; }
                    i++;
                    //matched ] or not, ignore the line
                    while ((i<iBufferLen) && (szBuffer[i]!='\n')) i++;
                    if (i>=iBufferLen) break;
                    //Jump to the new line
                    i++; 
                } else { //ignore the line and forward
                    i += strlen(pSection);
                    while ((i<iBufferLen) && (szBuffer[i]!='\n') &&
                           (szBuffer[i]!=']')) i++;
                    if (i>=iBufferLen) break;
                    if (szBuffer[i] == '\n') { i++; continue; }
                    if ( iKeyFound == 0 ) break;
                    else {
                        while ((i<iBufferLen) && (szBuffer[i]!='\n')) i++;
                        if (i>=iBufferLen) break;
                        //Jump to the next line
                        i++;
                    }
                }
            } else { 
                if (iKeyFound != 0) { //key has not found, ignore the line
                    while ((i<iBufferLen) && (szBuffer[i] != '\n')) i++;
                    if (i>=iBufferLen) return -1;
                    //Jump to the new line
                    i++;
                } else { //it may be the Identity to be found, judge it
                    while ((i<iBufferLen) && (szBuffer[i] != '\n') &&
                           (szBuffer[i] != '=')) i++;
                    if (i>=iBufferLen) break;
                    if (szBuffer[i] == '\n') { i++; continue; }
                    i++;
                    while ((i<iBufferLen) && 
                           ((szBuffer[i]=='\t') || (szBuffer[i]==' '))) i++;
        if (i>=iBufferLen) break;
                    j=i;
                    while ((j<iBufferLen) &&
                           (szBuffer[j]!='\n')) j++; j--;
                    while ((szBuffer[j]==' ') || 
                           (szBuffer[j]=='\t')) j--;
                    min = j-i+1;
                    strncpy(pValues+iSum, szBuffer+i, min);
                    iSum += min;
                    strncpy(pValues+iSum, pDelimiter, iDelimLen);
                    iSum += iDelimLen;
                    //Jump to the new line
                    i=j+1;
                    while ((i<iBufferLen) && (szBuffer[i]!='\n')) i++;
                    if (i>=iBufferLen) break;
                    //Jump to the next line
                    i++;                             
                }
            }
        }
    }
    *(pValues+iSum)='\0';
    return 0;
}

/**********************************************
Name   : GetSections
Input  : 
                char *pDelimiter:  Delimiter to separate sections in the file
                char *pSection   : Buffer to store returned sections in the file
Output :
            0 : Successful
            -1: Error
                
Process: Normal 
***********************************************/
int GetSections(const char *pDelimiter, char *pSections)
{
    int i=0;
    int j=0; 
    int iSum=0;
    int iDelimLen=strlen(pDelimiter);
    
    if (!strlen(pDelimiter) || (fpIni == NULL)) return -1;
    
    while (i<iBufferLen) {
        while ((i<iBufferLen) && 
               ((szBuffer[i]==' ') || (szBuffer[i]=='\t'))) i++;
        if (i>=iBufferLen) break;
        if ( szBuffer[i]=='[' ) {
         i++;
            while ((i<iBufferLen) && 
                  ((szBuffer[i]==' ') || (szBuffer[i]=='\t'))) i++;
            if (i>=iBufferLen) break;
            j=i;
            while ((j<iBufferLen) && (szBuffer[j]!='\n') &&
                   (szBuffer[j]!=']')) j++; 
            if ((j>=iBufferLen)) break;
            if ((szBuffer[j]=='\n')) { i++; continue; }//Jump to the new line
            j--;
            while ((szBuffer[j]==' ') || (szBuffer[j]=='\t')) j--;
            strncpy(pSections+iSum, szBuffer+i, j-i+1);
            iSum += j-i+1;
            strncpy(pSections+iSum, pDelimiter, iDelimLen);
            iSum += iDelimLen;
            //Jump to the new line
            i=j+2;
            while ((i<iBufferLen) && (szBuffer[i] != '\n')) i++;
            if (i>=iBufferLen) break; 
            //Jump to the next line
            i++; 
        } else { //ignore the lines beginning with '#'
            while ((i<iBufferLen) && (szBuffer[i] != '\n')) i++;
            if (i>=iBufferLen) break; 
            //Jump to the next line
            i++;
        }
    }
    *(pSections+iSum)='\0';
    return 0;
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -