/* 删除两边的空格 */ char *a_trim(char * szOutput, const char * szInput){ char *p = NULL; assert(szInput != NULL); assert(szOutput != NULL); l_trim(szOutput, szInput); for(p = szOutput + strlen(szOutput) - 1;p >= szOutput && isspace(*p); --p); *(++p) = '\0'; return szOutput; }
static void lr_trim(char* output, char* input) { char *p = NULL; assert(input != NULL); assert(output != NULL); l_trim(output, input); for(p = output + strlen(output) - 1; p >= output && isspace(*p); --p) { ; } *(++p) = '\0'; }
int syslk_parse_keyval(FILE *fp, char* KeyName, char* value) { char buf_o[KEYVALLEN], buf_i[KEYVALLEN]; char *buf = NULL; char *c; char keyname[KEYVALLEN]; char KeyVal[KEYVALLEN]; int ret = -1; while( !feof(fp) && fgets(buf_i,KEYVALLEN,fp) != NULL) { l_trim(buf_o, buf_i); if( strlen(buf_o) <= 0 ) { continue; } buf = NULL; buf = buf_o; if ( buf[0] == '#') { continue; } else { if( (c = (char*)strchr(buf, '=')) == NULL ) { continue; } sscanf( buf, "%[^=|^ |^\t]", keyname ); if( strcmp(keyname, KeyName) == 0 ) { sscanf( ++c, "%[^\n]", KeyVal ); char *KeyVal_o = (char *)malloc(strlen(KeyVal) + 1); if(KeyVal_o != NULL) { memset((void *)KeyVal_o, 0, sizeof(KeyVal_o)); lr_trim(KeyVal_o, KeyVal); if(KeyVal_o && strlen(KeyVal_o) > 0) { strcpy(value, KeyVal_o); } free(KeyVal_o); KeyVal_o = NULL; ret = 0; break; } } } } return ret; }
/*获取配置文件信息 */ int GetProfileString(char *profile, char *AppName, char *KeyName, char *KeyVal){ char appname[32], keyname[32]; char *buf, *c; char buf_i[KEYVALLEN], buf_o[KEYVALLEN]; FILE *fp; int found=0; /* 1 AppName 2 KeyName */ if((fp=fopen(profile,"r"))==NULL){ printf("openfile [%s] error [%s]\n", profile, strerror(errno)); return(-1); } fseek(fp, 0, SEEK_SET); memset(appname, 0, sizeof(appname)); sprintf( appname,"[%s]", AppName ); while(!feof(fp) && fgets(buf_i, KEYVALLEN, fp)!=NULL){ l_trim(buf_o, buf_i); if(strlen(buf_o) <= 0) continue; buf = NULL; buf = buf_o; if(found == 0){ if(buf[0] != '[') { continue; }else if(strncmp(buf, appname, strlen(appname))==0){ found = 1; continue; } } else if(found == 1){ if(buf[0] == '#'){ continue; }else if(buf[0] == '['){ break; }else{ if((c = (char*)strchr(buf, '=')) == NULL) continue; memset(keyname, 0, sizeof(keyname)); sscanf(buf, "%[^=|^ |^\t]", keyname); if(strcmp(keyname, KeyName) == 0){ sscanf(++c, "%[^\n]", KeyVal); char *KeyVal_o = (char *)malloc(strlen(KeyVal) + 1); if(KeyVal_o != NULL){ memset(KeyVal_o, 0, sizeof(KeyVal_o)); a_trim(KeyVal_o, KeyVal); if(KeyVal_o && strlen(KeyVal_o) > 0) strcpy(KeyVal, KeyVal_o); free(KeyVal_o); KeyVal_o = NULL; } found = 2; break; }else{ continue; } } } } fclose(fp); if(found == 2) return(0); else return(-1); }
void read_programs() { int i, j, cnt; char command[100], *ptr, *dest, *operand1, op, *operand2; var_num = 0; for(i = 0; i < 2; i++) { cnt = 0; while(fgets(command, 100, stdin)) { if(*(ptr = l_trim(command)) == '\0') continue; dest = ptr; while(isalnum(*ptr)) ++ptr; *ptr++ = '\0'; str_to_lower(dest); if(strcmp(dest, "end") == 0) break; while(!isalnum(*ptr)) ++ptr; operand1 = ptr++; while(isalnum(*ptr)) ++ptr; op = '\0'; if(*ptr == '+') op = '+'; else if(*ptr == '-') op = '-'; *ptr++ = '\0'; while(!isalnum(*ptr)) { if(!op) { if(*ptr == '+') op = '+'; else if(*ptr == '-') op = '-'; } ++ptr; } operand2 = ptr++; while(isalnum(*ptr)) ++ptr; *ptr = '\0'; str_to_lower(operand1); str_to_lower(operand2); ++cnt; inst_set[i][cnt].inst = MOV_R1; if(isdigit(operand1[0])) { inst_set[i][cnt].var = IMMEDIATE; inst_set[i][cnt].num = atoi(operand1); } else { str_to_lower(operand1); inst_set[i][cnt].var = handle_variable(operand1); } ++cnt; inst_set[i][cnt].inst = MOV_R2; if(isdigit(operand2[0])) { inst_set[i][cnt].var = IMMEDIATE; inst_set[i][cnt].num = atoi(operand2); } else { str_to_lower(operand2); inst_set[i][cnt].var = handle_variable(operand2); } ++cnt; inst_set[i][cnt].inst = (op == '+' ? ADD : SUB); ++cnt; inst_set[i][cnt].inst = MOV_VAR; inst_set[i][cnt].var = handle_variable(dest); } inst_num[i] = cnt; } std::sort(var_set, var_set+var_num, cmp_var); /*for(i = 1; i <= inst_num[0]; i += 4) { printf("%d R1, %d\n", inst_set[0][i].inst, inst_set[0][i].var); printf("%d R2, %d\n", inst_set[0][i+1].inst, inst_set[0][i+1].var); printf("%d R1, R2\n", inst_set[0][i+2].inst); printf("%d %d, R1\n", inst_set[0][i+3].inst, inst_set[0][i+3].var); } for(i = 1; i <= inst_num[1]; i += 4) { printf("%d R1, %d\n", inst_set[1][i].inst, inst_set[1][i].var); printf("%d R2, %d\n", inst_set[1][i+1].inst, inst_set[1][i+1].var); printf("%d R1, R2\n", inst_set[1][i+2].inst); printf("%d %d, R1\n", inst_set[1][i+3].inst, inst_set[1][i+3].var); }*/ }