/*代码的测试心得是声明不能用指针,要用数组带长度,否则,编译通过不能运行,显示段错误,以下是内部代码,没库的不能通过编译,旨在说明数组声明问题*/
- #include <stdio.h>
- #include <unistd.h>
- #include <string.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <sys/file.h>
- #include <sys/stat.h>
- #define CFO_PARA_ERR -1
- #define CFO_API_OK 0
- #define CFO_STRING_TYPE_UNKNOWN 0
- #define CFO_STRING_TYPE_STRING 1
- #define CFO_STRING_TYPE_INT 2
- int cfo_get_string_info_from_string(char *string, int *offset, size_t *size, unsigned char *type);
- main()
- {
- char *string = "210abcdefg";
- int offset[10];
- size_t size[10];
- unsigned char type[10];
- int i;
- i = cfo_get_string_info_from_string(string, offset, size, type);
- printf("fanhui: %d ", i);
- printf("type: %d ", type[0]);
- printf("size: %d ", size[0]);
- printf("offset: %d ", *offset);
- printf("/n");
- }
- int cfo_get_string_info_from_string(char *string, int *offset, size_t *size, unsigned char *type)
- {
- if ((string == NULL) || (offset == NULL) || (size == NULL) || (type == NULL))
- {
- return CFO_PARA_ERR;
- }
- size_t string_size = strlen(string);
- int offset_temp = 0;
- size_t size_temp;
- unsigned char type_temp;
- int i;
- for (i=0; i<string_size; i+=1)
- {
- /*............*/
- if ((string[i] >= '!') && (string[i] <= '~'))
- {
- break;
- }
- }
- /*...............*/
- if (i == string_size)
- {
- *offset = 0;
- *size = 0;
- *type = CFO_STRING_TYPE_UNKNOWN;
- type[0] = 10;
- return CFO_API_OK;
- }
- /*...................*/
- if ((string[i] == '/"')
- || (string[i] == '/'')
- || ((string[i] >= '0') && (string[i] <= '9')))
- {
- /*.........*/
- if ((string[i] == '0') && (string[i+1] == 'x'))
- {
- *offset = i;
- *type = CFO_STRING_TYPE_INT;
- size_temp = 2;
- for (i+=2; i<string_size; i+=1)
- {
- if (((string[i] >= '0') && (string[i] <= '9'))
- ||((string[i] >= 'a') && (string[i] <= 'f'))
- ||((string[i] >= 'A') && (string[i] <= 'F')))
- {
- size_temp += 1;
- }
- else
- {
- break;
- }
- }
- *size = size_temp;
- //type[0] = 10;
- return CFO_API_OK;
- }
- /*.......*/
- else if (((string[i] >= '0') && (string[i] <= '9')) && (string[i+1] != 'x'))
- {
- *offset = i;
- *type = CFO_STRING_TYPE_INT;
- size_temp = 1;
- for (i+=1; i<string_size; i+=1)
- {
- if ((string[i] >= '0') && (string[i] <= '9'))
- {
- size_temp += 1;
- }
- else
- {
- break;
- }
- }
- *size = size_temp;
- //type[0] = 10;
- return CFO_API_OK;
- }
- /*......*/
- else
- {
- offset_temp = i + 1;
- type_temp = CFO_STRING_TYPE_UNKNOWN;
- size_temp = 0;
- for (i+=1; i<string_size; i+=1)
- {
- /*............*/
- if ((string[i] == '/"') || (string[i] == '/''))
- {
- type_temp = CFO_STRING_TYPE_STRING;
- break;
- }
- /*...........*/
- else if ((string[i] == '#') || (string[i] == '/r') || (string[i] == '/n'))
- {
- type_temp = CFO_STRING_TYPE_UNKNOWN;
- break;
- }
- else
- {
- size_temp += 1;
- }
- }
- /*.............*/
- if (i == string_size)
- {
- type_temp = CFO_STRING_TYPE_UNKNOWN;
- }
- /*.......*/
- if (type_temp == CFO_STRING_TYPE_STRING)
- {
- *offset = offset_temp;
- *size = size_temp;
- *type = CFO_STRING_TYPE_STRING;
- //type[0] = 10;
- return CFO_API_OK;
- }
- else
- {
- *offset = offset_temp - 1;
- *size = 0;
- *type = CFO_STRING_TYPE_UNKNOWN;
- //type[0] = 10;
- return CFO_API_OK;
- }
- }
- }
- else
- {
- *offset = i;
- *size = 0;
- *type = CFO_STRING_TYPE_UNKNOWN;
- //type[0] = 10;
- return CFO_API_OK;
- }
- }