// 将消息信息放进列表中, 需要在操作前获得互斥对象 int pandora_frame_push(struct list_head **l, msg_t id, size_t len, const void *msg) { struct pandoras_msg *thiz; size_t s; if ( l == NULL ) return 1; s = len + sizeof(struct pandoras_msg); thiz = (struct pandoras_msg *)malloc(s); memset(thiz, 0, s); thiz->tsp = time(NULL); thiz->id = id; thiz->msg_size = len; thiz->msg_body = (void *)(((char *)thiz)+sizeof(struct pandoras_msg)); list_ini(thiz->nd); memcpy(thiz->msg_body, msg, len); if ( *l == NULL ) { *l = & thiz->nd; } else { list_inserttail( *l, &thiz->nd ); } return 0; }
int pandora_register_client(struct pandoras_box *pb, struct pandora_client *pc, void *_private) { struct pandora_client *new_pc; if ( pb == NULL || pc == NULL ) return 1; new_pc = (struct pandora_client *)malloc(sizeof(struct pandora_client)); if ( new_pc == NULL ) return 1; memset(new_pc, 0, sizeof(struct pandora_client)); memcpy(new_pc, pc, sizeof(struct pandora_client)); list_ini(new_pc->nd); new_pc->_private = _private; pthread_mutex_lock(&pb->clients_lck); if ( pb->clients ) { list_inserttail(pb->clients, &new_pc->nd); } else { pb->clients = & new_pc->nd; } pthread_mutex_unlock(&pb->clients_lck); return 0; }
void lex_ini(char *regex_file, char *code_file) { regex_num = regex_loader(token, regex_file); if ((code_file_p = fopen(code_file, "r")) == NULL) { printf("Open failed.\n"); return; } char **post = (char**)malloc(regex_num * sizeof(char*)); for (int i = 0; i < regex_num; i++) { *(post + i) = re2post(token[i].regex); } start = (state**)malloc(regex_num*sizeof(state*)); for (int i = 0; i < regex_num; i++) { *(start + i) = post2nfa(*(post + i), i); } current_list.s = (state**)malloc(g_state_num * sizeof(state*)); next_list.s = (state**)malloc(g_state_num * sizeof(state*)); list_ini(start, ¤t_list, regex_num); current_buff = (char*)calloc(1024, sizeof(char)); next_buff = (char*)calloc(1024, sizeof(char)); current_num = load(current_buff); current_state = 1; if (current_num >= 1024) { next_num = load(next_buff); next_state = 1; } else fclose(code_file_p); }
int main(int argc, char *argv[]){ int it; int *copia; int numElementos, i; FILE *f = NULL; List *list = NULL; List *laux = NULL; EleList *pele = NULL; if(argc != 2){ printf("Faltan argumentos\n"); return -1; } f = fopen(argv[1], "r"); if (!f){ return -1; } list = list_ini(); if(!list){ fclose(f); return -1; } while(feof(f) == 0){ /*Bucle que inserta dependiendo de si es par o impar*/ copia = (int *)malloc(sizeof(int)); if(!copia){ list_free(list); fclose(f); return -1; } fscanf(f, "%d", &it); pele = elelist_ini(); if(!pele){ free(copia); list_free(list); fclose(f); return -1; } *copia = it; if(!elelist_setInfo(pele, copia)){ list_free(list); fclose(f); free(copia); elelist_free(pele); return -1; } if((it)%2){ if(!list_insertLast(list, pele)){ list_free(list); fclose(f); elelist_free(pele); return -1; } if(list_print(stdout, list) < 0){ list_free(list); fclose(f); elelist_free(pele); return -1; } fprintf(stdout, "\n"); }else{ if(!list_insertFirst(list, pele)){ list_free(list); fclose(f); elelist_free(pele); return -1; } if(list_print(stdout, list)<0){ list_free(list); fclose(f); elelist_free(pele); return -1; } fprintf(stdout, "\n"); } elelist_free(pele); } fclose(f); numElementos = list_size(list); printf("Lista con %d elementos: \n", numElementos); if(list_print(stdout, list)<0){ list_free(list); return -1; } fprintf(stdout, "\n"); laux = list_ini(); if(!laux){ list_free(list); return -1; } for(i=0; i< (numElementos/2); i++){ pele = list_extractFirst(list); printf("Elemento extraido: "); elelist_print(stdout, pele); fprintf(stdout, "\n"); list_print(stdout, list); fprintf(stdout, "\n"); if(!pele){ list_free(list); list_free(laux); return -1; } if(!list_insertInOrder(laux, pele)){ list_free(list); list_free(laux); elelist_free(pele); return -1; } list_print(stdout, laux); fprintf(stdout, "\n"); elelist_free(pele); } while(list_isEmpty(list) == FALSE){ pele = list_extractLast(list); if(!pele){ list_free(list); list_free(laux); return -1; } printf("Elemento extraido: "); elelist_print(stdout, pele); fprintf(stdout, "\n"); list_print(stdout, list); fprintf(stdout, "\n"); if(!list_insertInOrder(laux, pele)){ list_free(list); list_free(laux); elelist_free(pele); return -1; } list_print(stdout, laux); fprintf(stdout, "\n"); elelist_free(pele); } printf("Lista con %d elementos: \n", list_size(laux)); if(list_print(stdout, laux)<0){ list_free(laux); return -1; } fprintf(stdout, "\n"); list_free(laux); list_free(list); return 0; }
int pandora_init(void *p) { int ret; pack_t pid; pthread_t sniffer_thread, protocol_thread; struct charge_task *tsk = (struct charge_task *)p; pb.zeus_port = 8082; pb.tsk = (struct charge_task *)p; log_printf(INF, "BOX.INIT: 初始化..."); pthread_mutex_init(&pb.msg_tx_lck, NULL); pthread_mutex_init(&pb.msg_rx_lck, NULL); pthread_mutex_init(&pb.clients_lck, NULL); pthread_mutex_init(&pb.sh_blocks_lck, NULL); log_printf(INF, "BOX.INIT: 初始化共享字段空间..."); ret = shared_block_touch(&pb.sh_blocks, 19); if ( ret ) { goto faile; } ret = shared_block_fill(pb.sh_blocks[0], (pack_t)PID_WORKING_JOB_01, "进程1", 1251 + 5); if ( ret ) { goto faile; } ret = shared_block_fill(pb.sh_blocks[1], (pack_t)PID_WORKING_JOB_02, "进程2", 1251 + 5); if ( ret ) { goto faile; } for ( pid = PID_PEDDING_JOB_01; pid <= (pack_t)PID_PEDDING_JOB_16; pid ++ ) { char buff[32]; sprintf(buff, "程序%d", pid & 0xFF); ret = shared_block_fill(pb.sh_blocks[(pid & 0xFF)-1], pid, buff, 61 + 3); if ( ret ) goto faile; } ret = shared_block_fill(pb.sh_blocks[18], (pack_t)PID_CHARGER_INFO, "充电桩", 612 + 0); if ( ret ) goto faile; ret = pthread_create( & protocol_thread, &tsk->attr, protocol_client_proc, &pb); if ( 0 != ret ) { ret = 0x1006; log_printf(ERR, "创建协议处理现成失败."); goto faile; } #ifdef _USE_P_BJNR_PROTOCOL do { struct pandora_client pc; struct socket_session *ss; int ret; memset(&pc, 0, sizeof(pc)); strncpy(pc.protocol_name, "北京南瑞协议", 31); list_ini(pc.nd); pc.bpc.via_way = PI_VIA_SOCKETS; pthread_mutex_init(&pc.rx_msg_lck, NULL); pthread_mutex_init(&pc.tx_msg_lck, NULL); pthread_mutex_init(&pc.pedding_msg_lck, NULL); ss = (struct socket_session*)malloc(sizeof(struct socket_session)); if ( ! ss ) { log_printf(WRN, "BOX.RESOURCE.ALLOC: 资源分配失败!"); goto faile; } memset(ss, 0, sizeof(struct socket_session)); ss->established_tsp = 0; ss->last_conn_tsp = 0; ss->s_srv = -1; strncpy(ss->s_srv_ip, tsk->server_addr, 32); ss->s_srv_port = tsk->server_port; pc.op.do_initialize = BJNR_initialize; pc.op.do_working = BJNR_working; pc.op.do_pause = BJNR_pause; pc.op.do_exit = BJNR_exit; pc.op.do_destroy = BJNR_destroy; log_printf(INF, "BOX.CLIENT: 注册客户端..."); ret = pandora_register_client(&pb, &pc, ss); if ( ret ) { log_printf(WRN, "BOX.PROTOCOL.REG: 注册客户端失败!"); } else { log_printf(INF, "BOX.PROTOCOL.REG: 客户端注册成功!"); } } while (0); #endif return 0; faile: log_printf(ERR, "BOX: 异常退出!"); return 1; }
int get_next_token(lexeme *lex) { int lex_length = 0; char lex_s[256]; int mark; blank_skip(); for (; ; buff_p++, lex_length++) { /*if (current_buff[buff_p] == 9 || current_buff[buff_p] == 10) { lex_length--; continue; }*/ lex_s[lex_length] = current_buff[buff_p]; mark = match(current_buff[buff_p]); if (mark == -1) { if (current_buff[buff_p] == '\0') { lex_s[0] = 0; strcpy(lex->lexeme_name, lex_s); strcpy(lex->token_name, "STRING_END"); //ending = 1; return 0; } printf("No match error.\n"); return 0; } else if (mark != -2) { blank_skip(); lex_s[lex_length] = 0; strcpy(lex->lexeme_name, lex_s); strcpy(lex->token_name, token[mark].token_name); list_ini(start, ¤t_list, regex_num); return 1; } if (buff_p == 1023) { if (next_state == 1) { temp = current_buff; current_buff = next_buff; next_buff = temp; next_state = 0; buff_p = 0; if (load(next_buff)) { next_state = 1; } } else { printf("End of the file.\n"); fclose(code_file_p); return -1; } } } }