Integer scanner function. (Useful for room tags.)

Results 1 to 2 of 2
  1. #1
    Valued Member DelPa is offline
    MemberRank
    May 2010 Join Date
    JapanLocation
    100Posts

    Integer scanner function. (Useful for room tags.)

    Code:
    int GetCustomValue(const char *_str, const char *_findSt, const char *_findEd = NULL);   // Function prototypes for header files.
    
    // If an error occurs, this function will returns -1.
    int GetCustomValue(const char *_str, const char *_findSt, const char *_findEd)  {
        const char *_findRes = strstr(_str, _findSt);   // Find the specified start string.
    
        if(_findRes == NULL)    {   // Error, not found.
            return -1;
        }
    
        int _findStLen = strlen(_findSt);   // Get length of the start string.
        const char *_findResSt = _findRes + _findStLen; // Go to integer position.
    
        if(_findResSt[0] == '\0')    {  // Error, last character is NULL.
            return -1;
        }
    
        if(_findResSt[0] == '0')    {   // Error, last character is zero.
            return -1;
        }
    
        char _strInteger[12];   // Will includes integer as string.
        int _nCount = 0;    // Read character count.
        bool _bSuccess = false; // Success or fail.
    
        for(int i = 0; i < (int)(sizeof(_strInteger) / sizeof(char)); i++)   { // Initialize buffer.
            _strInteger[i] = '\0';  // NULL character.
        }
    
        if(_findEd == NULL)    {    // End point is NULL.
            _bSuccess = true;
        }
    
        for(int i = 0; _findResSt[i] != '\0'; i++)   {  // Find integer.
            if(_findResSt[i] == '0' ||
               _findResSt[i] == '1' ||
               _findResSt[i] == '2' ||
               _findResSt[i] == '3' ||
               _findResSt[i] == '4' ||
               _findResSt[i] == '5' ||
               _findResSt[i] == '6' ||
               _findResSt[i] == '7' ||
               _findResSt[i] == '8' ||
               _findResSt[i] == '9')    {   // Check integer.
                   _strInteger[i] = _findResSt[i];  // Copy integer from the source string.
                   _nCount++;   // Copied integer count +1.
                   if(_nCount > 9) {    // Error, max digit over.
                       _bSuccess = false;
                       break;
                   }
            }   else {  // Check the end point.
                if(_findEd != NULL)    {    // Skip the check process if the end point is NULL.
                    for(size_t j = 0; j < strlen(_findEd); j++)   { // Find the end point.
                        if(_findResSt[i + j] == '\0')    {  // Error, NULL character before search ends.
                            _bSuccess = false;
                            break;
                        }   else if(_findResSt[i + j] != _findEd[j])   {    // Error, end point string is mismatch.
                            _bSuccess = false;
                            break;
                        }   else {  // Every check is success.
                            _bSuccess = true;
                        }
                    }
                }
                break;
            }
        }
    
        if(strlen(_strInteger) <= 0)    {   // Error, string is empty.
            _bSuccess = false;
        }
    
        if(_bSuccess == false)    { // Error.
            return -1;
        }
    
        return atoi(_strInteger);   // Done.
    }

    GetCustomValue function.

    Code:
    int GetCustomValue(
    const char *_str,           // String to search for.
    const char *_findSt,       // Starting string.
    const char *_findEd        // Ending string.
    );
    Arguments description...
    _str : String you want to search.
    _findSt : String you want to start search.
    _findEd : String you want to end search.

    Return value...
    If no error occurred, the function will returns found and converted to integer from string.
    Otherwise, the function will returns -1.

    Example code...
    The following is an example of how to use this function.
    Code:
    int main()  {   // Start point of the program.
        // Some samples.
        char szBuffer[256];
        int nResult;
    
        // Sample 1.
        strcpy(szBuffer, "Sample [A=5] string.");
        cout << szBuffer << '\n';
        nResult = GetCustomValue(szBuffer, "[A=", "]");
        cout << " - Result : " << nResult << '\n';
    
        // Sample 2.
        strcpy(szBuffer, "Sample string. [B=120]");
        cout << szBuffer << '\n';
        nResult = GetCustomValue(szBuffer, "[B=", "]");
        cout << " - Result : " << nResult << '\n';
    
        // Sample 3.
        strcpy(szBuffer, "[C=5000] Sample string. [D=1234]");
        cout << szBuffer << '\n';
        nResult = GetCustomValue(szBuffer, "[C=", "]");
        cout << " - Result 1 : " << nResult << '\n';
        nResult = GetCustomValue(szBuffer, "[D=", "]");
        cout << " - Result 2 : " << nResult << '\n';
    
        // Sample 4.
        strcpy(szBuffer, "Sample @e5678 string.");
        cout << szBuffer << '\n';
        nResult = GetCustomValue(szBuffer, "@e", NULL);
        cout << " - Result : " << nResult << '\n';
    
        // Sample 5.
        strcpy(szBuffer, "$$99999$$ Sample string.");
        cout << szBuffer << '\n';
        nResult = GetCustomValue(szBuffer, "$$", "$$");
        cout << " - Result : " << nResult << '\n';
    
        // Error Sample 1.
        strcpy(szBuffer, "Error Sample string.");
        cout << szBuffer << '\n';
        nResult = GetCustomValue(szBuffer, "[F=", "]");
        cout << " - Result : " << nResult << '\n';
    
        // Error Sample 2.
        strcpy(szBuffer, "Error Sample string. [G=4321");
        cout << szBuffer << '\n';
        nResult = GetCustomValue(szBuffer, "[G=", "]");
        cout << " - Result : " << nResult << '\n';
    
        // Error Sample 3.
        strcpy(szBuffer, "Error @h0123 Sample string.");
        cout << szBuffer << '\n';
        nResult = GetCustomValue(szBuffer, "@h", NULL);
        cout << " - Result : " << nResult << '\n';
    
        // Error Sample 4.
        strcpy(szBuffer, "Error Sample [i=1000000000] string.");
        cout << szBuffer << '\n';
        nResult = GetCustomValue(szBuffer, "[i=", "]");
        cout << " - Result : " << nResult << '\n';
    
        // Error Sample 5.
        strcpy(szBuffer, "Error Sample [J=8765] string.");
        cout << szBuffer << '\n';
        nResult = GetCustomValue(szBuffer, "[j=", "]");
        cout << " - Result : " << nResult << '\n';
    
        // Error Sample 6.
        strcpy(szBuffer, "Error @k Sample string.");
        cout << szBuffer << '\n';
        nResult = GetCustomValue(szBuffer, "@k", NULL);
        cout << " - Result : " << nResult << '\n';
    
    
    #ifdef _WIN32   // Pause the console if the running OS is windows.
        system("PAUSE");
    #endif
    
        return 0;
    }
    p.s. Wrote at MSDN style.
    If you find bad coding in this or there is good function than this, please post here.
    Thanks.

    (The original code is in attachment.)
    Attached Files Attached Files
    Last edited by DelPa; 01-07-12 at 01:51 PM. Reason: Bug fix.


  2. #2
    Developer / Patch Finder Tankado is offline
    MemberRank
    Oct 2011 Join Date
    The NetherlandsLocation
    451Posts

    Re: Integer scanner function. (Useful for room tags.)

    ty, you are awesome



Advertisement