SmartAudio/lichee/linux-4.9/drivers/input/touchscreen/ftxxxx/ini.c

425 lines
9.6 KiB
C
Raw Normal View History

2018-07-13 01:31:50 +00:00
/*
* drivers/input/touchscreen/ini.c
*
* FocalTech ft5x TouchScreen driver.
*
* Copyright (c) 2010 Focal tech Ltd.
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
#include <linux/string.h>
#include <asm/unistd.h>
#include <linux/vmalloc.h>
#include <linux/errno.h>
#include "ini.h"
char CFG_SSL = '['; /* <20><><EFBFBD><EFBFBD>־<EFBFBD><D6BE>Section Symbol --<2D>ɸ<EFBFBD><C9B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD> { }<7D><>*/
char CFG_SSR = ']'; /* <20><><EFBFBD><EFBFBD>־<EFBFBD><D6BE>Section Symbol --<2D>ɸ<EFBFBD><C9B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><C4A3><EFBFBD> { }<7D><>*/
char CFG_NIS = ':'; /* name <20><> index ֮<><D6AE><EFBFBD>ķָ<C4B7><D6B8><EFBFBD> */
char CFG_NTS = '#'; /* ע<>ͷ<EFBFBD>*/
static char * ini_str_trim_r(char * buf);
static char * ini_str_trim_l(char * buf);
static int ini_file_get_line(char *filedata, char *buffer, int maxlen);
static int ini_split_key_value(char *buf, char **key, char **val);
static long atol(char *nptr);
/*************************************************************
Function: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>key<EFBFBD><EFBFBD>ֵ
Input: char * filedata<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>char * section<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><EFBFBD>char * key<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
Output: char * value<EFBFBD><EFBFBD>key<EFBFBD><EFBFBD>ֵ
Return: 0 SUCCESS
-1 δ<EFBFBD>ҵ<EFBFBD>section
-2 δ<EFBFBD>ҵ<EFBFBD>key
-10 <EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD><EFBFBD>
-12 <EFBFBD><EFBFBD>ȡ<EFBFBD>ļ<EFBFBD>ʧ<EFBFBD><EFBFBD>
-14 <EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
-22 <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С
Note:
*************************************************************/
int ini_get_key(char *filedata, char * section, char * key, char * value)
{
char *buf1, *buf2;
char *key_ptr, *val_ptr;
int n, ret;
int dataoff = 0;
int i = 0;
buf1 = (char *)vmalloc(MAX_CFG_BUF + 1);
if (buf1 == NULL)
return -ENOMEM;
buf2 = (char *)vmalloc(MAX_CFG_BUF + 1); {
vfree(buf1);
return -ENOMEM;
}
*value='\0';
while(i<5) { /* <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>section */
ret = CFG_ERR_READ_FILE;
n = ini_file_get_line(filedata+dataoff, buf1, MAX_CFG_BUF);
dataoff += n;
if(n < -1)
goto r_cfg_end;
ret = CFG_SECTION_NOT_FOUND;
if(n < 0)
goto r_cfg_end; /* <20>ļ<EFBFBD>β<EFBFBD><CEB2>δ<EFBFBD><CEB4><EFBFBD><EFBFBD> */
n = strlen(ini_str_trim_l(ini_str_trim_r(buf1)));
if(n == 0 || buf1[0] == CFG_NTS)
continue; /* <20><><EFBFBD><EFBFBD> <20><> ע<><D7A2><EFBFBD><EFBFBD> */
ret = CFG_ERR_FILE_FORMAT;
if(n > 2 && ((buf1[0] == CFG_SSL && buf1[n-1] != CFG_SSR)))
goto r_cfg_end;
if(buf1[0] == CFG_SSL) {
buf1[n-1] = 0x00;
if(strcmp(buf1+1, section) == 0)
break; /* <20>ҵ<EFBFBD><D2B5><EFBFBD>section */
}
i++;
}
i = 0;
while(1){ /* <20><><EFBFBD><EFBFBD>key */
ret = CFG_ERR_READ_FILE;
n = ini_file_get_line(filedata+dataoff, buf1, MAX_CFG_BUF);
dataoff += n;
if(n < -1)
goto r_cfg_end;
ret = CFG_KEY_NOT_FOUND;
if(n < 0)
goto r_cfg_end;/* <20>ļ<EFBFBD>β<EFBFBD><CEB2>δ<EFBFBD><CEB4><EFBFBD><EFBFBD>key */
n = strlen(ini_str_trim_l(ini_str_trim_r(buf1)));
if(n == 0 || buf1[0] == CFG_NTS)
continue; /* <20><><EFBFBD><EFBFBD> <20><> ע<><D7A2><EFBFBD><EFBFBD> */
ret = CFG_KEY_NOT_FOUND;
if(buf1[0] == CFG_SSL)
goto r_cfg_end;
if(buf1[n-1] == '+') { /* <20><>+<2B>ű<EFBFBD>ʾ<EFBFBD><CABE>һ<EFBFBD>м<EFBFBD><D0BC><EFBFBD> */
buf1[n-1] = 0x00;
while(1) {
ret = CFG_ERR_READ_FILE;
n = ini_file_get_line(filedata+dataoff, buf2, MAX_CFG_BUF);
dataoff += n;
if(n < -1)
goto r_cfg_end;
if(n < 0)
break;/* <20>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD> */
n = strlen(ini_str_trim_r(buf2));
ret = CFG_ERR_EXCEED_BUF_SIZE;
if(n > 0 && buf2[n-1] == '+'){/* <20><>+<2B>ű<EFBFBD>ʾ<EFBFBD><CABE>һ<EFBFBD>м<EFBFBD><D0BC><EFBFBD> */
buf2[n-1] = 0x00;
if( (strlen(buf1) + strlen(buf2)) > MAX_CFG_BUF)
goto r_cfg_end;
strcat(buf1, buf2);
continue;
}
if(strlen(buf1) + strlen(buf2) > MAX_CFG_BUF)
goto r_cfg_end;
strcat(buf1, buf2);
break;
}
}
ret = CFG_ERR_FILE_FORMAT;
if(ini_split_key_value(buf1, &key_ptr, &val_ptr) != 1)
goto r_cfg_end;
ini_str_trim_l(ini_str_trim_r(key_ptr));
if(strcmp(key_ptr, key) != 0)
continue; /* <20><>keyֵ<79><D6B5>ƥ<EFBFBD><C6A5> */
strcpy(value, val_ptr);
break;
}
ret = CFG_OK;
r_cfg_end:
//if(fp != NULL) fclose(fp);
vfree(buf1);
vfree(buf2);
return ret;
}
/*************************************************************
Function: <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>section
Input: char *filename<EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>,int max <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɷ<EFBFBD><EFBFBD>ص<EFBFBD>section<EFBFBD>ĸ<EFBFBD><EFBFBD><EFBFBD>
Output: char *sections[]<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>section<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Return: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>section<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
-10 <EFBFBD>ļ<EFBFBD><EFBFBD>򿪳<EFBFBD><EFBFBD><EFBFBD>
-12 <EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
-14 <EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Note:
*************************************************************/
int ini_get_sections(char *filedata, unsigned char * sections[], int max)
{
//FILE *fp;
char buf1[MAX_CFG_BUF + 1];
int n, n_sections = 0, ret;
int dataoff = 0;
int i = 0;
// if((fp = fopen(filename, "rb")) == NULL)
// return CFG_ERR_OPEN_FILE;
while(i<5) {/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>section */
ret = CFG_ERR_READ_FILE;
n = ini_file_get_line(filedata+dataoff, buf1, MAX_CFG_BUF);
dataoff += n;
if(n < -1)
goto cfg_scts_end;
if(n < 0)
break;/* <20>ļ<EFBFBD>β */
n = strlen(ini_str_trim_l(ini_str_trim_r(buf1)));
if(n == 0 || buf1[0] == CFG_NTS)
continue; /* <20><><EFBFBD><EFBFBD> <20><> ע<><D7A2><EFBFBD><EFBFBD> */
ret = CFG_ERR_FILE_FORMAT;
if(n > 2 && ((buf1[0] == CFG_SSL && buf1[n-1] != CFG_SSR)))
goto cfg_scts_end;
if(buf1[0] == CFG_SSL) {
if (max!=0){
buf1[n-1] = 0x00;
strcpy((char *)sections[n_sections], buf1+1);
if (n_sections>=max)
break; /* <20><><EFBFBD><EFBFBD><EFBFBD>ɷ<EFBFBD><C9B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
}
n_sections++;
}
i++;
}
ret = n_sections;
cfg_scts_end:
// if(fp != NULL)
// fclose(fp);
return ret;
}
/*************************************************************
Function: ȥ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ұߵĿ<EFBFBD><EFBFBD>ַ<EFBFBD>
Input: char * buf <EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
Output:
Return: <EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
Note:
*************************************************************/
static char * ini_str_trim_r(char * buf)
{
int len,i;
char tmp[128];
memset(tmp, 0, sizeof(tmp));
len = strlen(buf);
// tmp = (char *)malloc(len);
memset(tmp,0x00,len);
for(i = 0;i < len;i++) {
if (buf[i] !=' ')
break;
}
if (i < len) {
strncpy(tmp,(buf+i),(len-i));
}
strncpy(buf,tmp,len);
// free(tmp);
return buf;
}
/*************************************************************
Function: ȥ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ߵĿ<EFBFBD><EFBFBD>ַ<EFBFBD>
Input: char * buf <EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
Output:
Return: <EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
Note:
*************************************************************/
static char * ini_str_trim_l(char * buf)
{
int len,i;
char tmp[128];
memset(tmp, 0, sizeof(tmp));
len = strlen(buf);
//tmp = (char *)malloc(len);
memset(tmp,0x00,len);
for(i = 0;i < len;i++) {
if (buf[len-i-1] !=' ')
break;
}
if (i < len) {
strncpy(tmp,buf,len-i);
}
strncpy(buf,tmp,len);
//free(tmp);
return buf;
}
/*************************************************************
Function: <EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD>ж<EFBFBD>ȡһ<EFBFBD><EFBFBD>
Input: FILE *fp <EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>int maxlen <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󳤶<EFBFBD>
Output: char *buffer һ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
Return: >0 ʵ<EFBFBD>ʶ<EFBFBD><EFBFBD>ij<EFBFBD><EFBFBD><EFBFBD>
-1 <EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
-2 <EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Note:
*************************************************************/
static int ini_file_get_line(char *filedata, char *buffer, int maxlen)
{
int i, j;
char ch1;
for(i=0, j=0; i<maxlen; j++) {
ch1 = filedata[j];
if(ch1 == '\n' || ch1 == 0x00)
break; /* <20><><EFBFBD><EFBFBD> */
if(ch1 == '\f' || ch1 == 0x1A) { /* '\f':<3A><>ҳ<EFBFBD><D2B3>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD>Ч<EFBFBD>ַ<EFBFBD> */
buffer[i++] = ch1;
break;
}
if(ch1 != '\r') buffer[i++] = ch1; /* <20><><EFBFBD>Իس<D4BB><D8B3><EFBFBD> */
}
buffer[i] = '\0';
return i+2;
}
/*************************************************************
Function: <EFBFBD><EFBFBD><EFBFBD><EFBFBD>key<EFBFBD><EFBFBD>value
key=val
jack = liaoyuewang
| | |
k1 k2 i
Input: char *buf
Output: char **key, char **val
Return: 1 --- ok
0 --- blank line
-1 --- no key, "= val"
-2 --- only key, no '='
Note:
*************************************************************/
static int ini_split_key_value(char *buf, char **key, char **val)
{
int i, k1, k2, n;
if((n = strlen((char *)buf)) < 1)
return 0;
for(i = 0; i < n; i++)
if(buf[i] != ' ' && buf[i] != '\t')
break;
if(i >= n)
return 0;
if(buf[i] == '=')
return -1;
k1 = i;
for(i++; i < n; i++)
if(buf[i] == '=')
break;
if(i >= n)
return -2;
k2 = i;
for(i++; i < n; i++)
if(buf[i] != ' ' && buf[i] != '\t')
break;
buf[k2] = '\0';
*key = buf + k1;
*val = buf + i;
return 1;
}
int my_atoi(const char *str)
{
int result = 0;
int signal = 1; /* Ĭ<><C4AC>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD> */
if((*str>='0'&&*str<='9')||*str=='-'||*str=='+') {
if(*str=='-'||*str=='+') {
if(*str=='-')
signal = -1; /*<2A><><EFBFBD><EFBFBD><EBB8BA>*/
str++;
}
}
else
return 0;
/*<2A><>ʼת<CABC><D7AA>*/
while(*str>='0' && *str<='9')
result = result*10 + (*str++ - '0' );
return signal*result;
}
int isspace(int x)
{
if(x==' '||x=='\t'||x=='\n'||x=='\f'||x=='\b'||x=='\r')
return 1;
else
return 0;
}
int isdigit(int x)
{
if(x<='9' && x>='0')
return 1;
else
return 0;
}
static long atol(char *nptr)
{
int c; /* current char */
long total; /* current total */
int sign; /* if ''-'', then negative, otherwise positive */
/* skip whitespace */
while ( isspace((int)(unsigned char)*nptr) )
++nptr;
c = (int)(unsigned char)*nptr++;
sign = c; /* save sign indication */
if (c == '-' || c == '+')
c = (int)(unsigned char)*nptr++; /* skip sign */
total = 0;
while (isdigit(c)) {
total = 10 * total + (c - '0'); /* accumulate digit */
c = (int)(unsigned char)*nptr++; /* get next char */
}
if (sign == '-')
return -total;
else
return total; /* return result, negated if necessary */
}
/***
*int atoi(char *nptr) - Convert string to long
*
*Purpose:
* Converts ASCII string pointed to by nptr to binary.
* Overflow is not detected. Because of this, we can just use
* atol().
*
*Entry:
* nptr = ptr to string to convert
*
*Exit:
* return int value of the string
*
*Exceptions:
* None - overflow is not detected.
*
*******************************************************************************/
int atoi(char *nptr)
{
return (int)atol(nptr);
}