int load_data(char *filename, char *entidad){ FILE * entity_file; FILE * block_file; struct record_t *record; struct file_header * file_header; struct block_t * current_block; char * buffer; int res; /* Entity file */ entity_file = open_file_to_parse(filename); if (!entity_file){ return BAD_NAME_FILE; } /* Block and header file */ block_file = open_block_file(entidad, 1); if(!block_file){ return BAD_NAME_FILE; }else{ file_header = (struct file_header*) malloc(sizeof(struct file_header)); if(!file_header) return ALLOCATE_FAIL; else{ initialize_file_header(file_header); write_header(block_file, file_header); } } /* Allocate buffer and record */ buffer = (char*) malloc (RECORD_MAX_SIZE); record = (struct record_t*) malloc(sizeof(struct record_t)); current_block = (struct block_t *) malloc(sizeof(struct block_t)); if(!buffer || !record || !current_block) return ALLOCATE_FAIL; initialize_buffer(buffer); initialize_block(current_block, 1); initialize_record(record); /* Read text file */ while(fgets(buffer, RECORD_MAX_SIZE, entity_file) != NULL){ res = parse_line(buffer, record, RECORD_MAX_SIZE); if(res == RES_OK){ res = find_and_write_block(block_file, file_header, record, current_block); initialize_record(record); free(record->content); }; }; write_to_file(block_file, file_header, current_block); print_header(file_header); free(record); free(buffer); free(file_header); free(current_block); close_entity_file(entity_file); close_block_file(block_file); return res; }
int search_by_code(char* entidad, int code){ FILE * block_file; struct record_t *record; struct block_t * current_block; struct file_header * file_header; int res, i = 1, j = 1, recs = 0; int current_pos = 0, encontrado = 1; block_file = open_block_file(entidad, 2); if(!block_file){ return BAD_NAME_FILE; } /* Allocate buffer and record */ record = (struct record_t*) malloc(sizeof(struct record_t)); current_block = (struct block_t *) malloc(sizeof(struct block_t)); file_header = (struct file_header*) malloc(sizeof(struct file_header)); if(!record || !current_block || !file_header) return ALLOCATE_FAIL; initialize_record(record); initialize_block(current_block,1); read_header(block_file, file_header); while((i < file_header->total_blocks+1) && (encontrado == 1)){ current_pos = 0; j = 1; res = read_from_file(block_file, current_block, file_header, i); if(res == RES_OK){ recs = current_block->total_reg; while((j < recs+1) && (encontrado == 1)){ initialize_record(record); current_pos += read_block(current_block, record, current_pos); if(code == record->code){ print_success_message(record); encontrado = 0; } free(record->content); j++; } } i++; } if(encontrado == 1){ print_error_message(code); } free(record); free(file_header); free(current_block); fclose(block_file); return res; }
int list_data(char *filename){ FILE * block_file; struct record_t *record; struct block_t * current_block; struct file_header * file_header; int res, i, j, recs, k; int current_pos = 0; block_file = open_block_file(filename, 2); if(!block_file) return BAD_NAME_FILE; /* Allocate buffer and record */ record = (struct record_t*) malloc(sizeof(struct record_t)); current_block = (struct block_t *) malloc(sizeof(struct block_t)); file_header = (struct file_header*) malloc(sizeof(struct file_header)); if(!record || !current_block || !file_header) return ALLOCATE_FAIL; initialize_record(record); initialize_block(current_block,1); read_header(block_file, file_header); printf("Listado de %s\n\n", filename); k=0; for(i=1;i<file_header->total_blocks+1; i++){ current_pos = 0; res = read_from_file(block_file, current_block, file_header, i); if(res == RES_OK){ recs = current_block->total_reg; for(j=0; j < recs; j++){ initialize_record(record); current_pos += read_block(current_block, record, current_pos); printf("%03d) %d, %s\n",k+1, record->code, record->content); k++; free(record->content); } } } printf("\n"); free(record); free(file_header); free(current_block); fclose(block_file); return res; }
void test_initialize_record(void) { record = NULL; initialize_record(&record, SIZE); int i; for (i = 0; i < SIZE; i++) { CU_ASSERT_EQUAL_FATAL(record->members[i], i + 1); CU_ASSERT_EQUAL_FATAL(record->index[i], i); CU_ASSERT_FALSE_FATAL(record->direction[i]); } }
int order(char * entidad, int (*fp)(struct record_t*, struct record_t*)){ FILE * block_file; FILE * tmp_file; struct record_t *record; struct block_t * current_block; struct file_header * file_header; int res, j = 0; int current_pos = 0; int current_pos_freeze = 0; int registros_leidos = 0, bloques_leidos = 0; int todo_leido = 1, totales_leidos = 0; int registro_en_memoria = 1, registro_procesado = 0; int tmp_files = 1; char path[30]; struct record_t buffer[TMP_BUFFER]; struct record_t freeze_buffer[TMP_BUFFER]; block_file = open_block_file(entidad, 2); if(!block_file){ return BAD_NAME_FILE; } /* Allocate buffer and record */ current_block = (struct block_t *) malloc(sizeof(struct block_t)); file_header = (struct file_header*) malloc(sizeof(struct file_header)); record = (struct record_t*) malloc(sizeof(struct record_t)); if(!current_block || !file_header || !record) return ALLOCATE_FAIL; initialize_block(current_block,1); read_header(block_file, file_header); initialize_record(record); res = read_from_file(block_file, current_block, file_header, 1); bloques_leidos++; if(res == RES_OK){ /* Inicializo el buffer con registros */ while(j<TMP_BUFFER && (todo_leido == 1)){ if(registros_leidos < current_block->total_reg){ current_pos += read_block(current_block, record, current_pos); memcpy(&buffer[j],record,sizeof(struct record_t)); registros_leidos++; totales_leidos++; j++; }else{ if((bloques_leidos < file_header->total_blocks+1) && (file_header->total_records > totales_leidos)){ res = read_from_file(block_file, current_block, file_header, current_block->number+1); bloques_leidos++; registros_leidos = 0; current_pos = 0; }else{ todo_leido = 0; } } } /* Creo el primer sort file */ sprintf(path,"tmp/sort%d.txt",tmp_files); tmp_file = fopen(path, "w"); if(file_header->total_records == totales_leidos) heapify(buffer,totales_leidos, fp); else heapify(buffer,TMP_BUFFER, fp); while((todo_leido == 1) && tmp_file){ if(registros_leidos < current_block->total_reg){ current_pos += read_block(current_block, record, current_pos); registros_leidos++; registro_en_memoria = 0; registro_procesado = 1; }else{ if(bloques_leidos < file_header->total_blocks){ res = read_from_file(block_file, current_block, file_header, current_block->number+1); bloques_leidos++; current_pos = 0; registros_leidos = 0; registro_procesado = 1; registro_en_memoria = 1; }else{ todo_leido = 0; } } while((registro_procesado == 1) && (registro_en_memoria == 0)){ if(fp(record,&buffer[0]) < 0){ if(current_pos_freeze<TMP_BUFFER){ memcpy(&freeze_buffer[current_pos_freeze],record,sizeof(struct record_t)); current_pos_freeze++; registro_procesado = 0; registro_en_memoria = 1; } else { /* vacio el buffer en el archivo */ print_buffer_to_file(buffer, TMP_BUFFER, tmp_file, fp); /* cierro archivo y abro el siguiente */ tmp_files++; fclose(tmp_file); sprintf(path,"tmp/sort%d.txt",tmp_files); tmp_file = fopen(path, "w"); /* copio el freeze_buffer al buffer y creo el heap*/ heapify(freeze_buffer,TMP_BUFFER, fp); memcpy(buffer,freeze_buffer,sizeof(struct record_t) * TMP_BUFFER); current_pos_freeze = 0; registro_procesado = 1; } } else{ /* Guardo en el archivo, libero memoria */ print_record_to_file(buffer, tmp_file); /* Guardo en el buffer el record leido */ memcpy(&buffer[0],&buffer[TMP_BUFFER-1],sizeof(struct record_t)); memcpy(&buffer[TMP_BUFFER-1],record,sizeof(struct record_t)); registro_procesado = 0; registro_en_memoria = 1; update(buffer, TMP_BUFFER, fp); } } } /* Guardo en el archivo*/ if((file_header->total_records == totales_leidos) && (current_block->number == 1)) print_buffer_to_file(buffer, totales_leidos, tmp_file, fp); else print_buffer_to_file(buffer, TMP_BUFFER, tmp_file, fp); fclose(tmp_file); /* Guardo lo que tenia en el freeze_buffer en otro archivo */ if(current_pos_freeze > 1){ tmp_files++; sprintf(path,"tmp/sort%d.txt",tmp_files); tmp_file = fopen(path, "w"); print_buffer_to_file(freeze_buffer, current_pos_freeze, tmp_file, fp); fclose(tmp_file); } } free(record); free(current_block); free(file_header); fclose(block_file); /* Merge de los archivos generados */ res = merge_files(tmp_files, fp); printf("\n"); return res; }
int main(int argc, char **argv){ int i=0, res, free_space; char content[17] = "Esto es un test "; FILE * block_file; struct file_header * fh; struct block_t * block; struct record_t * record; printf("Welcome to Carga Masiva Test\n\n"); block_file = fopen("block_records_test.dat", "w"); fh = (struct file_header*) malloc(sizeof(struct file_header)); record = (struct record_t*) malloc(sizeof(struct record_t)); block = (struct block_t *) malloc(sizeof(struct block_t)); if(!record || !block || !fh) return -1; initialize_block(block, 1); initialize_record(record); initialize_file_header(fh); res = write_header(block_file, fh); if(res == 0){ for(i=0;i<50;i++){ record->content = (char*) malloc(strlen(content)+1); memcpy(record->content, content, strlen(content)+1); record->content[17] = '\0'; record->code = i+1; record->size = (int)sizeof(record->size) + (int)sizeof(record->code) + (int) strlen(record->content) + 1; free_space = MAX_SIZE - block->space_occupied; if(free_space < record->size){ write_to_file(block_file, fh, block); printf("(Bloque %d) \tOcupado: %d, Records: %d\n", block->number, block->space_occupied, block->total_reg); initialize_block(block, block->number+1); } write_block(block, fh, record); initialize_record(record); free(record->content); } } write_to_file(block_file, fh, block); printf("(Bloque %d) \tOcupado: %d, Records: %d\n", block->number, block->space_occupied, block->total_reg); printf("\nListo la carga de bloques\n"); printf("---------------------------------------\n"); print_header(fh); free(block); free(record); free(fh); fclose(block_file); return 0; }