int normalizarArquivo(char* caminhoArquivo) { FILE* file; FILE* temp; TokenReader* tokenReader; char* linha; char* token; file = fopen(caminhoArquivo, "r"); if (!file) { return 0; } temp = fopen("Arquivos/temp", "w"); linha = (char*) calloc(1000, sizeof (char)); tokenReader = newTokenReader(linha); while (fgets(linha, 1000, file) != NULL) { setTokenString(tokenReader, linha); while (hasMoreTokens(tokenReader)) { token = nextToken(tokenReader); if (strcasecmp(token, "\n")) { fputs(token, temp); fputs(" ", temp); if (!strcasecmp(token, ";")) { fputs("\n", temp); } } } } fclose(file); fclose(temp); return 1; }
int ReverseTokenizer::countTokens() { StreamGuard guard(stream, offset); // store current seek pointer position std::streamoff curroff = offset; // position get pointer at end of stream offset = offend; int count = 0; while (hasMoreTokens()) { nextToken(); count++; } // reposition seek get pointer to original position offset = curroff; return count; }
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); }