字符串处理,判断sim卡状态


#include 
#include 
#include 
#include 
#include 

bool isNumber(char ch)
{
    if ((ch >= '0') && (ch <= '9'))
    {
        return true;
    }
    return false;
}
int getNumLen(const char* str, int index)
{
    int numLen = 0;
    while (1)
    {
        if (isNumber(str[index]))
        {
            index ++;
            numLen ++;
        }
        else
        {
            break;
        }
    }
    return numLen;
}
int getNumList(const char*str, uint32_t* array)
{
    int numIndex = 0;
    int pos = 0;
    //(1&&1 = 1)
    if ( !(str && array) )
    {
        return -1;
    }
    while(1)
    {
        if ( isNumber(str[numIndex]) )
        {
            int len = getNumLen(str, numIndex);
            if (len == 0)
            {
                break;
            }
            char tmpStr[len +1];
            memcpy(tmpStr, str + numIndex, len);
            tmpStr[len] = '\0';
            array[pos ++] = atoi(tmpStr);
            numIndex = numIndex + len;
        }
        else
        {
            numIndex++;
            if (numIndex > strlen(str))
            {
                break;
            }
        }
    }
    return pos;
}

typedef enum
{
    OE_SIM_INSERT = 0,
    OE_SIM_REMOVED = 1,
    OE_SIM_UNLOCK = 2,
    OE_SIM_LOCK = 3,
    OE_SIM_BUSY = 4,
    OE_SIM_STATE_UNKNOWN =5,
}oe_sim_States_t;

int main (){
    int array[128]= {0};
    int len = getNumList("+QSIMSTAT: 0,1", array);
    if (len > 0) {
        if (array[1] == 0) {
            printf("Sim card is removed\n");
            return OE_SIM_REMOVED;
        } else if (array[1] == 1) {
            printf("Sim card is insert\n");
            return OE_SIM_INSERT;
        }
    }
    return 0;
}

你可能感兴趣的:(c语言,java,算法)