Last active
January 4, 2016 18:49
-
-
Save qianjigui/8663359 to your computer and use it in GitHub Desktop.
CBestPractice_Steps 用于阶段性处理各流程的状态维护与统一管理
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
#include "step.h" | |
#include "step_imp.h" | |
void init_context(char* CONTEXT_FILE_PATH, struct desc_set* set) | |
{ | |
FILE *fp; | |
char buff[1024]; | |
char* line; | |
struct status_step_context * context = set->context; | |
const struct status_step_desc * descs = set->descs; | |
const int STEP_SIZE = set->size; | |
fp = fopen(CONTEXT_FILE_PATH, "r"); | |
if(fp){ | |
while((line=fgets(buff,sizeof(buff),fp))){ | |
char name[1024]; | |
int status; | |
if(EOF!=sscanf(line,"%s %d",name,&status)){ | |
printf("Read: %s %d\n", name, status); | |
int i; | |
for(i=0;i<STEP_SIZE;i++){ | |
if(0==strcmp(name, descs[i].name)){ | |
(descs+i)->load(buff, status, context, descs+i); | |
break; | |
} | |
} | |
} | |
} | |
fclose(fp); | |
}else{ | |
//CAN NOT find | |
printf("Can NOT find %s\n",CONTEXT_FILE_PATH); | |
} | |
} | |
void process_steps(char* CONTEXT_FILE_PATH, struct desc_set* set) | |
{ | |
FILE *fp; | |
struct status_step_context * context = set->context; | |
const struct status_step_desc * descs = set->descs; | |
const int STEP_SIZE = set->size; | |
printf("Open %s with append mode\n", CONTEXT_FILE_PATH); | |
fp = fopen(CONTEXT_FILE_PATH, "a"); | |
if(fp){ | |
int i; | |
for(i=0;i<STEP_SIZE;i++){ | |
(descs+i)->process(fp, context, descs+i); | |
} | |
printf("Close %s\n", CONTEXT_FILE_PATH); | |
fclose(fp); | |
}else{ | |
//CAN NOT find | |
printf("Can NOT append %s\n",CONTEXT_FILE_PATH); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef STEP | |
#define STEP 1 | |
#include <stdio.h> | |
enum STATUS_STEP_RETURN_VALUE {ERROR, SUCCESS, FINISH}; | |
enum STEP_VALUE {STEP_BASE, STEP_DOWNLOAD, STEP_RUN, STEP_SUBMIT}; | |
struct status_step_context{ | |
enum STEP_VALUE step; | |
}; | |
struct status_step_desc { | |
const char* name; | |
enum STATUS_STEP_RETURN_VALUE (*load)(char* buff,int status, struct status_step_context * context, const struct status_step_desc * desc); | |
enum STATUS_STEP_RETURN_VALUE (*process)(FILE* output, struct status_step_context * context, const struct status_step_desc * desc); | |
}; | |
struct desc_set { | |
int size; | |
const struct status_step_desc* descs; | |
struct status_step_context* context; | |
}; | |
extern void init_context(char* filename, struct desc_set* set); | |
extern void process_steps(char* filename, struct desc_set* set); | |
#endif //STEP |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment