/*============================================================================= | |
** FileName: function_common.cpp | |
** Desc: about function test | |
** Author: Warren | |
** Version: V1.0 | |
** LastChange: 2021-02-26 | |
** History: | |
=============================================================================*/ | |
/*Get private menu,take only the first two parameters, and return the head pointer of the remaining string*/ | |
#include <stdio.h> | |
#include <string.h> | |
#include "function_common.h" | |
#include <sys/time.h> | |
#include <unistd.h> | |
void set_timer(int it_interval_sec, int it_interval_usec,int it_value_sec,int it_value_usec) | |
{ | |
struct itimerval itv, oldtv; | |
itv.it_interval.tv_sec = it_interval_sec; | |
itv.it_interval.tv_usec = it_interval_usec; | |
itv.it_value.tv_sec = it_value_sec; | |
itv.it_value.tv_usec = it_value_usec; | |
setitimer(ITIMER_REAL, &itv, &oldtv); | |
} | |
int sleep_with_restart(int second) | |
{ | |
int left = second; | |
while (left > 0) | |
{ | |
left = sleep(left); | |
} | |
return 0; | |
} | |
int millli_sleep_with_restart(int millisecond) | |
{ | |
int left = millisecond*1000; | |
while (left > 0) | |
{ | |
left = usleep(left); | |
} | |
return 0; | |
} | |
//Get private menu,take only the first two parameters, and return the head pointer of the remaining string | |
char* getMenu(char * str,char *argv[]) | |
{ | |
char* pos = str; | |
int menu = 0; | |
int num = 0; | |
if (str == NULL) | |
{ | |
return NULL; | |
} | |
while (1) | |
{ | |
menu = 0; | |
if (num == 2) | |
{ | |
break; | |
} | |
while (1) | |
{ | |
if (*pos == '"') | |
{ | |
if (menu == 0) | |
{ | |
menu++; | |
argv[num++] = pos+1; | |
} | |
else if (menu == 1) | |
{ | |
*pos++ = '\0'; | |
break; | |
} | |
else | |
{ | |
break; | |
} | |
} | |
pos++; | |
} | |
} | |
printf("moudle = %s\nAPI = %s\n", argv[0], argv[1]); | |
return pos; | |
} | |
/*parse the parameters,and then stroge every parameter to argv.*/ | |
/*return the number of parameters.*/ | |
int getParam(char * str,char *argv[]) | |
{ | |
char* pos = str; | |
int num = 0; | |
if (str == NULL) | |
{ | |
return 0; | |
} | |
while (1) | |
{ | |
if (*pos == '\0') | |
{ | |
break; | |
} | |
while (1) | |
{ | |
if (*(pos - 1) == '=' && *pos == '"')// "="" as the basis for starting the parameter. | |
{ | |
argv[num++] = pos+1; | |
} | |
else if (*(pos - 1) == '"' && *pos == '&')// "="" as the basis for the end of the parameter | |
{ | |
*(pos - 1) = '\0'; | |
pos++; | |
break; | |
} | |
else if (*pos == '\0'&&*(pos-1) == '"')//the string end. | |
{ | |
*(pos - 1) = '\0'; | |
break; | |
} | |
else if (*pos == '\0')//the string end. | |
{ | |
break; | |
} | |
pos++; | |
} | |
} | |
// for (int i = 0;i < num;i++) | |
// { | |
// printf("argv[%d] = %s\n",i,argv[i]); | |
// } | |
return num; | |
} | |
/*remove the escape character '\'*/ | |
int removeTage(const int length,char* argv[]) | |
{ | |
char* temp = NULL; | |
char* temp1 = NULL; | |
char** pos = argv; | |
for (int i = 0;i < length;i++) | |
{ | |
temp1 = pos[i]; | |
while (1) | |
{ | |
if (*pos[i] == '\0') | |
{ | |
break; | |
} | |
if ((*pos[i] == '"') && (*(pos[i] - 1) == '\\'))//Remove "\"" when encountering '\' | |
{ | |
temp = pos[i]; | |
while (1) | |
{ | |
*(pos[i] - 1) = *(pos[i]); | |
if (*(pos[i]) == '\0') | |
{ | |
break; | |
} | |
pos[i]++; | |
} | |
pos[i] = temp; | |
} | |
pos[i]++; | |
} | |
pos[i] = temp1; | |
} | |
return 0; | |
} | |
int parseParameters(char *str,char *argv[]) | |
{ | |
int argc = 0; | |
argc = getParam(str,argv); | |
removeTage(argc,argv); | |
// for (int i = 0;i < argc; i++) | |
// { | |
// printf("param %d = %s\n",i, argv[i]); | |
// } | |
return 0; | |
} | |
RIL_COMMAND*find_command (char *name,RIL_COMMAND *Class) | |
{ | |
register int i; | |
for (i = 0; Class[i].name; i++) | |
if (strcmp (name, Class[i].name) == 0) | |
return (&Class[i]); | |
return ((RIL_COMMAND *)NULL); | |
} | |