static void getlogset(void){ defaultValue(&logsetting); if(strlen(confFile) == 0) { printf("Will load default log set.\n"); return; } FILE *fp = fopen(confFile,"r"); if(fp==NULL) { printf("Can not open file %s\n", confFile); return ; } char line[100]={0}; while(fgets(line, sizeof(line), fp) != NULL){ if( isSkipRow(line) ) continue; char* pos = strchr(line, '='); if( pos == NULL ) continue; char* key = line; char* value = pos+1; *pos = '\0'; value[strlen(value)-1] = '\0'; // remove '\n' setVariable(key, value, &logsetting); memset(line, 0, 100); } }
static uint32_t getRealRowsNum( FILE* fp ){ uint32_t rows = 0; long filePos = ftell(fp); char tmp[200]; while(fgets( tmp, 200, fp )!= NULL ){ if( isSkipRow(tmp)) continue; rows ++; memset( tmp, 0, 200 ); } fseek(fp, filePos, SEEK_SET ); return rows; }
static void readMasterFile(){ //LogWrite(INFO,"Try to read master file %s",filePath[1]); if( !access(filePath[1],F_OK)==0) { //LogWrite(ERROR,"Read file %s %s!",filePath[1], strerror(errno)); return; } FILE* fp =fopen(filePath[1],"r"); if( fp == NULL ){ //LogWrite(ERROR,"Open %s Fail! %s",filePath[1],strerror(errno)); exit (-1) ; } int rows = getRealRowsNum(fp); if( rows == 0 ){ //LogWrite(ERROR,"No master address ! Please check !\n"); exit (-1) ; } //LogWrite(INFO, "Total master's number is: %d", rows); // malloc masterList masterList.masterIP = (struct sockaddr_in *)malloc(rows * sizeof(struct sockaddr_in)); masterList.masterNum = 0; // read data char line[100]={0}; while(fgets(line, sizeof(line), fp)!= NULL){ if( isSkipRow(line) ){ continue; } char* pos = strchr(line, ':'); if( pos == NULL ) continue; int port = atoi( pos + 1); *pos='\0'; char *ip = line; ip = del_both_trim(ip); setAddress( masterList.masterIP + masterList.masterNum, ip, port ); //LogWrite(INFO, "Master number %d : %s:%d",masterList.masterNum, ip, port); masterList.masterNum ++ ; memset(line, 0, 100); } fclose(fp); //LogWrite(INFO,"Read %s file finished!",filePath[1]); }
static BOOLEAN readMasterFile(void){ if( !access(filePath[1],F_OK)==0) { return FALSE; } FILE* fp =fopen(filePath[1],"r"); if( fp == NULL ) { return FALSE; } uint32_t rows = getRealRowsNum(fp); if( rows == 0 ){ return FALSE; } // malloc masterList masterList.masterIP = (struct sockaddr_in *)malloc(rows * sizeof(struct sockaddr_in)); masterList.masterNum = 0; // read data char line[100]={0}; while(fgets(line, sizeof(line), fp)!= NULL){ if( isSkipRow(line) ){ continue; } char* pos = strchr(line, ':'); if( pos == NULL ) continue; int port = atoi( pos + 1); *pos='\0'; char *ip = line; ip = del_both_trim(ip); setAddress( masterList.masterIP + masterList.masterNum, ip, port ); masterList.masterNum ++ ; memset(line, 0, 100); } fclose(fp); return TRUE; }
static BOOLEAN readConfFile(void){ //load default value defaultValue(); // if file does not exist if( !access(filePath[0],F_OK)==0) { return FALSE ; } // file exist FILE* fp =fopen(filePath[0],"r"); if( fp == NULL ){ printf("Can not open file %s", filePath[0]); //LogWrite(ERROR,"Open %s Fail! %s",strerror(errno)); return FALSE ; } // read data char line[100]={0}; while(fgets(line, sizeof(line), fp)!= NULL){ if( isSkipRow(line) ) continue; char* pos = strchr(line, '='); if( pos == NULL ) continue; char* key = line; char* value = pos+1; *pos = '\0'; value[strlen(value)-1] = '\0'; // remove '\n' setVariable(key,value); memset(line, 0, 100); } fclose(fp); return TRUE; //LogWrite(INFO,"Read %s file finished!",filePath[0]); }