diff --git a/trabalho3/Makefile b/trabalho3/Makefile index 97c2353..d07fd34 100644 --- a/trabalho3/Makefile +++ b/trabalho3/Makefile @@ -5,9 +5,9 @@ NAME=programaTrab2 # Directories INCDIR=include LIBDIR=lib -BLDDIR=build +BLDDIR=bin SRCDIR=src -OBJDIR=$(SRCDIR)/obj +OBJDIR=$(SRCDIR) # Can use any debbuger (like gdb) or overlay (like valgrind) (though gdb is an overlay too) DEBUGGER= diff --git a/trabalho3/build/programaTrab1 b/trabalho3/bin/programaTrab1 similarity index 100% rename from trabalho3/build/programaTrab1 rename to trabalho3/bin/programaTrab1 diff --git a/trabalho3/bin/programaTrab2 b/trabalho3/bin/programaTrab2 new file mode 100755 index 0000000..53063ab Binary files /dev/null and b/trabalho3/bin/programaTrab2 differ diff --git a/trabalho3/build/programaTrab2 b/trabalho3/build/programaTrab2 deleted file mode 100755 index ca1ee68..0000000 Binary files a/trabalho3/build/programaTrab2 and /dev/null differ diff --git a/trabalho3/src/main.c b/trabalho3/src/main.c index 0838588..2dddac6 100644 --- a/trabalho3/src/main.c +++ b/trabalho3/src/main.c @@ -2,7 +2,7 @@ TRABALHO 3 - ORGANIZAÇÃO DE ARQUIVOS Nome: Michelle Wingter da Silva nUSP: 10783243 - Juliano Fantozzi xxxxxxxx + Juliano Fantozzi 9791218 */ @@ -121,31 +121,17 @@ void remove_registro(){ char nomeCampo[MAX]; char valorCampo[MAX]; - //int n_rem = 0; - //REG_REM* listaRem = NULL; - scanf(" %s %d", nomeBin, &n); - //criando lista de removidos - //NO_REG_REM* lista_rem; - - for (int i = 0; i < n; ++i) - { - //scanf("%s %s", nomeCampo, valorCampo); + for (int i = 0; i < n; ++i){ scanf("%s", nomeCampo); scan_quote_string(valorCampo); //printf("nome e valor|%s|%s|\n", nomeCampo, valorCampo); - //lista_rem = calloc(1, sizeof(NO_REG_REM)); busca_RemoveReg(nomeBin, nomeCampo, valorCampo); } - //fclose(bin); binarioNaTela2(nomeBin); - //printf("\nListar o arquivo binário %s.", nomeBin); - - - //free(lista_rem); - + //printf("\nListar o arquivo binário %s.", nomeBin); } /* @@ -158,13 +144,11 @@ void insere_registro(){ scanf(" %s %d", nomeBin, &n); char idStr[MAX], salStr[MAX], tel[MAX], nome[MAX], cargo[MAX]; - //int id; double sal; REGDADOS *rd; long int ultimo_reg = -1; - for (int i = 0; i < n; ++i) - { + for (int i = 0; i < n; ++i){ scanf("%s ", idStr); scanf("%s ", salStr); scan_quote_string(tel); @@ -173,7 +157,6 @@ void insere_registro(){ //printf("id|%s|, sal|%s|, tel|%s|, nome|%s|, cargo|%s|\n", idStr, salStr, tel, nome, cargo); rd = calloc(1, sizeof(REGDADOS)); - //criaNovoRegDados(rd, id, sal, tel, nome, cargo); criaNovoRegDados2(rd, idStr, salStr, tel, nome, cargo); // printf("\n==========INSERINDO: \n\tid|%d|, sal|%lf|, tel|%s|, nome|%s|, cargo|%s|\n", rd->idServidor, rd->salarioServidor, rd->telefoneServidor, rd->nomeServidor, rd->cargoServidor); // printf("TAMANHOS: \n\ttel = %ld\n\tnome = %d\n\tcargo = %d\n\t", strlen(tel), rd->tamNomeServidor, rd->tamCargoServidor); diff --git a/trabalho3/src/manipulaReg.c b/trabalho3/src/manipulaReg.c index ca59471..a8bd7e2 100644 --- a/trabalho3/src/manipulaReg.c +++ b/trabalho3/src/manipulaReg.c @@ -2,7 +2,7 @@ TRABALHO 3 - ORGANIZAÇÃO DE ARQUIVOS Nome: Michelle Wingter da Silva nUSP: 10783243 - Juliano Fantozzi xxxxxxxx + Juliano Fantozzi 9791218 */ diff --git a/trabalho3/src/organizaArq.c b/trabalho3/src/organizaArq.c index b9b37c8..9fd7b7c 100644 --- a/trabalho3/src/organizaArq.c +++ b/trabalho3/src/organizaArq.c @@ -2,7 +2,7 @@ TRABALHO 3 - ORGANIZAÇÃO DE ARQUIVOS Nome: Michelle Wingter da Silva nUSP: 10783243 - Juliano Fantozzi xxxxxxxx + Juliano Fantozzi 9791218 */ @@ -71,9 +71,59 @@ void ordena_por_id(char *nomeBin_in, char *nomeBin_out){ fclose(bin_in); fclose(bin_out); +} + +//========================== MERGING DE DOIS ARQUIVOS ====================== +void read_andMerge(char *nomeBin_in1, char *nomeBin_in2, char *nomeBin_out){ + //========= checando arquivo 1 ============ + FILE* bin_in1 = fopen(nomeBin_in1, "rb"); + fileCheck(bin_in1); + + FILE* bin_in2 = fopen(nomeBin_in2, "rb"); + fileCheck(bin_in2); + + FILE* bin_out = fopen(nomeBin_out, "wb"); + fileCheck(bin_out); + + REGCAB* rc = calloc(1, sizeof(REGCAB)); + REGDADOS *rd1 = NULL; + REGDADOS *rd2 = NULL; + + + leCabecalho(nomeBin_in1, rc); + regCabToArqBin(rc, bin_out); + do{ + rd1 = pegarProximoRegistro(bin_in1); + rd2 = pegarProximoRegistro(bin_in2); + pegaRegistroNoOffset + + if(rd1.idServidor > rd2.idServidor && rd1 && rd2){ + REGDADOS *aux = rd2; + rd2 = rd1; + rd1 = aux; + } + + escreveRegDados(bin_out, rd1); + if(rd1.idServidor != rd2.idServidor) escreveRegDados(bin_out, rd2); + + if (rd1) free(rd1); + if (rd2) free(rd2); + } + while(rd1 || rd2); + + free(rc); + fclose(bin_in1); + fclose(bin_in2); + fclose(bin_out); } +//========================== MATCHING DE DOIS ARQUIVOS ====================== +void read_andMatch(char *nomeBin_in1, char *nomeBin_in2, char *nomeBin_out){ +} + +//========================== FUNCOES AUXILIARES ============================= + int int_compare_id(const void *A, const void *B) { // para ordenar de forma crescente REGDADOS *pA, *pB; pA = (REGDADOS *) A; @@ -81,51 +131,79 @@ int int_compare_id(const void *A, const void *B) { // para ordenar de forma cres return pB->idServidor - pA->idServidor; } -//========================== MERGING DE DOIS ARQUIVOS ====================== -void read_andMerge(char *nomeBin_in1, char *nomeBin_in2, char *nomeBin_out){ - //========= checando arquivo 1 ============ - FILE* bin_in1 = fopen(nomeBin_in1, "rb"); - if(bin_in1 == NULL){ +void fileCheck(FILE *fp){ + if(fp == NULL){ printf("Falha no processamento do arquivo.\n"); exit(0); } - char status1; - fread(&status1, STATUS_TAM, 1, bin_in1); + char status; + fread(&status, STATUS_TAM, 1, fp); //printf("status = %c\n", status); - if (status1 == '0') { - printf("Falha no processamento do arquivo.\n"); - fclose(bin_in1); - exit(0); - } - //========= checando arquivo 2 ============ - FILE* bin_in2 = fopen(nomeBin_in2, "rb"); - if(bin_in2 == NULL){ + if (status == '0') { printf("Falha no processamento do arquivo.\n"); + fclose(fp); exit(0); } +} - char status2; - fread(&status2, STATUS_TAM, 1, bin_in2); - //printf("status = %c\n", status); +REGDADOS *pegarProximoRegistro(FILE *fp){ + if(feof(fp)) return NULL; - if (status2 == '0') { - printf("Falha no processamento do arquivo.\n"); - fclose(bin_in2); - exit(0); +} + +void escreveRegDados(int* tamPagina, int *tamRegAnterior, REGDADOS* r, REGCAB* c, FILE* bin){ + if (r->idServidor == -1){ + return; } - //======================================== - //merge dos dois arquivos de entrada para o arquivo de saida + int aux = r->tamanhoRegistro + 5; + if((*tamPagina - aux) < 0){ + char arroba = '@'; + + for(int i = 0; i < *tamPagina; i++){ + fwrite(&arroba, sizeof(char), 1, bin); + (*tamRegAnterior) = (*tamRegAnterior) + 1; + } -} + //adicionar o tamanho desses arrobas no ultimo registro da pagina + fseek(bin, -(*tamRegAnterior + 4), SEEK_CUR); + fwrite(&(*tamRegAnterior), sizeof(int), 1, bin); + fseek(bin, 0, SEEK_END); + *tamPagina = 32000; + } -//========================== MATCHING DE DOIS ARQUIVOS ====================== -void read_andMatch(char *nomeBin_in1, char *nomeBin_in2, char *nomeBin_out){ + *tamPagina -= aux; + + fwrite(&r->removido, REM_TAM, 1, bin); + fwrite(&r->tamanhoRegistro, TAM_TAM, 1, bin); + fwrite(&r->encadeamentoLista, ENC_TAM, 1, bin); + fwrite(&r->idServidor, ID_TAM, 1, bin); + fwrite(&r->salarioServidor, SAL_TAM, 1, bin); + fwrite(&r->telefoneServidor, TEL_TAM * sizeof(char), 1, bin); + + if(r->nomeServidor[0] != '\0'){ + int tamanho2 = strlen(r->nomeServidor) + 2; + fwrite(&tamanho2, TAM_TAM, 1, bin); + fwrite(&c->tags[3], TAG_TAM, 1, bin); + fwrite(&r->nomeServidor, strlen(r->nomeServidor) * sizeof(char) + 1, 1, bin); + } + if(r->cargoServidor[0] != '\0'){ + int tamanho = strlen(r->cargoServidor) + 2; + fwrite(&tamanho, TAM_TAM, 1, bin); + fwrite(&c->tags[4], TAG_TAM, 1, bin); + fwrite(&r->cargoServidor, strlen(r->cargoServidor) * sizeof(char) + 1, 1, bin); + } + if(*tamPagina == 0){ + *tamPagina = 32000; + } + + (*tamRegAnterior) = r->tamanhoRegistro; } +} \ No newline at end of file diff --git a/trabalho3/src/rcab.c b/trabalho3/src/rcab.c index 11ded62..a2a9f08 100644 --- a/trabalho3/src/rcab.c +++ b/trabalho3/src/rcab.c @@ -2,7 +2,7 @@ TRABALHO 3 - ORGANIZAÇÃO DE ARQUIVOS Nome: Michelle Wingter da Silva nUSP: 10783243 - Juliano Fantozzi xxxxxxxx + Juliano Fantozzi 9791218 */ diff --git a/trabalho3/src/rdados.c b/trabalho3/src/rdados.c index 749c956..2c6d73a 100644 --- a/trabalho3/src/rdados.c +++ b/trabalho3/src/rdados.c @@ -2,7 +2,7 @@ TRABALHO 3 - ORGANIZAÇÃO DE ARQUIVOS Nome: Michelle Wingter da Silva nUSP: 10783243 - Juliano Fantozzi xxxxxxxx + Juliano Fantozzi 9791218 */