예제 #1
0
void init_database(){
    FILE * conf = fopen("dados/conf.txt","r");
    if (conf == NULL){
        printf("erro ao abrir arquivo de configuracao\n");
    }
    fscanf(conf,"%d\n",&qtdTabelas);
    int i;
    char linha[500];
    tabelas = (tabela*)malloc(sizeof(tabela)*qtdTabelas);
    for(i=0;i<qtdTabelas;i++){
        fgets(linha,500,conf);
        criarTabela(linha,i);
        strcpy(linha,"");
    }

}
예제 #2
0
파일: gerencia.c 프로젝트: rmeloca/APS1
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);
}