Пример #1
0
int obterBloco(Tabela* tabela, int tamanhoRegistro) {
    short int qtdRegistros, iniEspacoLivre, fimEspacoLivre;
    FILE *file;

    file = fopen(tabela->nomesArquivosBlocos[tabela->numeroBlocos - 1], "r+b");
    fread(&qtdRegistros, sizeof (short int), 1, file);

    fseek(file, 4, SEEK_SET);
    fread(&iniEspacoLivre, sizeof (short int), 1, file);

    fimEspacoLivre = (qtdRegistros * 2) + 6;

    if ((iniEspacoLivre - fimEspacoLivre) >= tamanhoRegistro) {
        return tabela->numeroBlocos - 1;
    }

    printf("Criar novo bloco\n");

    char* temp = (char*) calloc(100, sizeof (char));
    char* nomeArquivo = (char*) calloc(100, sizeof (char));

    //criar bloco para cada tabela
    sprintf(temp, "_%d", tabela->numeroBlocos);
    strcpy(nomeArquivo, "Arquivos/");
    strcat(nomeArquivo, tabela->nome);
    strcat(nomeArquivo, temp);
    strcat(nomeArquivo, ".dat");

    gerarBloco(nomeArquivo);
    adicionarBloco(tabela, nomeArquivo);

    return tabela->numeroBlocos - 1;
}
Пример #2
0
void interpretarCreateTable(Banco* banco, char* caminhoArquivo) {
    FILE* file;
    TokenReader* tokenReader;
    Tabela* tabela;
    Campo* campo;
    char* linha;
    char* token;
    char* nomeBloco;
    char* nomeCampo;
    Tipo tipoCampo;
    int bytesCampo;

    file = fopen(caminhoArquivo, "r");
    linha = (char*) calloc(1000, sizeof (char));
    nomeBloco = (char*) calloc(100, sizeof (char));

    //Cada linha corresponde à um CREATE TABLE
    tokenReader = newTokenReader(linha);
    while (fgets(linha, 1000, file) != NULL) {
        setTokenString(tokenReader, linha);

        token = nextToken(tokenReader); //CREATE
        if (!strcasecmp(token, "CREATE")) {
            token = nextToken(tokenReader); //TABLE
            token = nextToken(tokenReader);
            tabela = criarTabela(token);
            adicionarTabela(banco, tabela);
            token = nextToken(tokenReader); //abre parêntesis
        }

        while (hasMoreTokens(tokenReader)) {
            nomeCampo = nextToken(tokenReader); //nome
            if (!strcasecmp(nomeCampo, ";")) {
                break;
            }

            token = nextToken(tokenReader); //tipo

            if (!strcasecmp(token, "INTEGER")) {
                tipoCampo = INTEGER;
                bytesCampo = 4;
            } else if (!strcasecmp(token, "BOOLEAN")) {
                tipoCampo = BOOLEAN;
                bytesCampo = 1;
            } else {
                if (!strcasecmp(token, "CHAR")) {
                    tipoCampo = CHAR;
                } else if (!strcasecmp(token, "VARCHAR")) {
                    tipoCampo = VARCHAR;
                }
                token = nextToken(tokenReader); //abre parêntesis
                token = nextToken(tokenReader); //bytes
                bytesCampo = atoi(token);
                token = nextToken(tokenReader); //fecha parêntesis
            }
            token = nextToken(tokenReader); //vírgula
            campo = criarCampo(nomeCampo, tipoCampo, bytesCampo);
            adicionarCampo(tabela, campo);
        }

        //criar bloco para cada tabela
        sprintf(nomeCampo, "_%d", tabela->numeroBlocos);
        strcpy(nomeBloco, "Arquivos/");
        strcat(nomeBloco, tabela->nome);
        strcat(nomeBloco, nomeCampo);
        strcat(nomeBloco, ".dat");
        gerarBloco(nomeBloco);
        adicionarBloco(tabela, nomeBloco);
    }
    fclose(file);
}