Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef enum { false, true } bool;
- //17:49 - Dank memes
- //Note to self, have files closed when program exits due to error
- //Also, be sure to make errors actually output the specific source of the error
- void print(char *text)
- {
- int len = strlen(text);
- char *str2 = malloc(len+2);
- strcpy(str2,text);
- str2[len] = '\n';
- str2[len+1] = '\0';
- printf(str2);
- free(str2);
- }
- void error(char *text)
- {
- int len = strlen(text);
- char *str2 = malloc(len+2);
- strcpy(str2,text);
- str2[len] = '\n';
- str2[len+1] = '\0';
- fprintf(stderr,str2);
- free(str2);
- }
- bool isValidRelationName(char* relName)
- {
- if(strlen(relName) < 1)
- return false;
- if(strlen(relName) > 15)
- return false;
- unsigned int i = 0;
- for(i = 0; i<strlen(relName); i++)
- if(!((relName[i] >= 'a' && relName[i] <= 'z') || (relName[i] >= 'A' && relName[i] <= 'Z') || (relName[i] >= '0' && relName[i] <= '9') || relName[i] == '_' ))
- return false;
- if((relName[0] >= 'a' && relName[0] <= 'z') || (relName[0] >= 'A' && relName[0] <= 'Z'))
- return true;
- else
- return false;
- }
- struct tupleStruct
- {
- int valueInt;
- char valueStr[255];
- };
- struct attributeStruct
- {
- char name[15];
- bool type;
- int length;
- struct tupleStruct tuple[100];
- };
- struct relationStruct
- {
- char name[15];
- FILE *schemaFile;
- FILE *dataFile;
- int attributeCount;
- int tupleCount;
- struct attributeStruct attribute[10];
- };
- int getRelationLength(struct relationStruct in)
- {
- int ret = 0;
- int i = 0;
- for(i = 0; i<in.attributeCount; i++)
- ret += in.attribute[i].length;
- return ret;
- }
- struct relationStruct relation[10];
- FILE *configFile;
- FILE *queryFile;
- int main(int argc, char *argv[])
- {
- if(argc < 3)
- {
- error("Not enough command line arguments.");
- return 1;
- }
- configFile = fopen(argv[1], "r");
- if (!configFile)
- {
- error("Could not open config file.");
- return 1;
- }
- queryFile = fopen(argv[2],"r");
- if(!queryFile)
- {
- error("Could not open query file.");
- return 1;
- }
- int numRelations = -1;
- int ret = fscanf(configFile,"%d",&numRelations);
- if(ret != 1)
- {
- error("Could not get number of relations from config file.");
- return 1;
- }
- if(numRelations < 1 || numRelations > 10)
- {
- error("Number of relations in config file exceeded bounds! (1-10)");
- return 1;
- }
- int i = 0;
- for(i = 0; i<numRelations; i++)
- {
- char relName[255];
- int ret = fscanf(configFile,"%s",relName);
- if(ret != 1)
- {
- error("Could not get name of relation from config file.");
- return 1;
- }
- if(!isValidRelationName(relName))
- {
- error("Invalid relation name.");
- return 1;
- }
- char dataName[255];
- strcpy(dataName,relName);
- int tmpLen = strlen(dataName);
- dataName[tmpLen] = '.';
- dataName[tmpLen+1] = 'd';
- dataName[tmpLen+2] = 'a';
- dataName[tmpLen+3] = 't';
- dataName[tmpLen+4] = '\0';
- strcpy(relation[i].name,relName);
- tmpLen = strlen(relName);
- relName[tmpLen] = '.';
- relName[tmpLen+1] = 's';
- relName[tmpLen+2] = 'c';
- relName[tmpLen+3] = 'h';
- relName[tmpLen+4] = '\0';
- relation[i].schemaFile = fopen(relName,"r");
- if(!relation[i].schemaFile)
- {
- error("Could not open schema file:");
- error(relName);
- return 1;
- }
- else
- {
- int retB = fscanf(relation[i].schemaFile,"%d",&relation[i].attributeCount);
- if(retB != 1)
- {
- error("Could not get number of attributes from schema file.");
- return 1;
- }
- if(relation[i].attributeCount < 1 || relation[i].attributeCount > 10)
- {
- error("Attribute count out of bounds!");
- return 1;
- }
- int q = 0;
- for(q = 0; q<relation[i].attributeCount; q++)
- {
- char attributeName[255];
- char attributeType[10];
- int attributeLength;
- int retC = fscanf(relation[i].schemaFile,"%s %s %d",attributeName,attributeType,&attributeLength);
- if(retC != 3)
- {
- error("Could not get attribute data from schema file.");
- return 1;
- }
- if(!isValidRelationName(attributeName))
- {
- error("Invalid attribute name from schema file.");
- return 1;
- }
- relation[i].attribute[q].length = attributeLength;
- strcpy(relation[i].attribute[q].name,attributeName);
- if(attributeType[0] == 'S')
- relation[i].attribute[q].type = true;
- else if(attributeType[0] == 'I')
- relation[i].attribute[q].type = false;
- else
- {
- error("Invalid attribute type not I or S in schema file.");
- return 1;
- }
- }
- fclose(relation[i].schemaFile);
- }
- relation[i].dataFile = fopen(dataName,"rb");
- if(!relation[i].dataFile)
- {
- error("Could not open data file for realtion.");
- return 1;
- }
- else
- {
- int relationLength = getRelationLength(relation[i]);
- int sz = 0;
- fseek(relation[i].dataFile, 0L, SEEK_END);
- sz = ftell(relation[i].dataFile);
- fseek(relation[i].dataFile, 0L, SEEK_SET);
- double tupleUnRounded = ((double)sz)/((double)relationLength);
- int tupleCount = (int)tupleUnRounded;
- if(((double)tupleCount) != tupleUnRounded)
- {
- error("Data file contains incomplete reccord.");
- return 1;
- }
- relation[i].tupleCount = tupleCount;
- char currentTuple[relationLength];
- int x = 0;
- for(x = 0; x<relation[i].tupleCount; x++)
- {
- int r = 0;
- for(r = 0; r<relationLength; r++)
- currentTuple[r] = fgetc(relation[i].dataFile);
- int pointerPos = 0;
- int x2 = 0;
- for(x2 = 0; x2<relation[i].attributeCount; x2++)
- {
- char currentField[relation[i].attribute[x2].length+1];
- int x3 = 0;
- for(x3 = 0; x3 < relation[i].attribute[x2].length; x3++)
- currentField[x3] = currentTuple[x3+pointerPos];
- currentField[relation[i].attribute[x2].length] = '\0';
- pointerPos += relation[i].attribute[x2].length;
- if(relation[i].attribute[x2].type)
- {
- strcpy(relation[i].attribute[x2].tuple[x].valueStr,currentField);
- print(relation[i].attribute[x2].tuple[x].valueStr);
- }
- else
- {
- unsigned int intBuf = 0;
- intBuf = currentField[0]*255*255*255;
- intBuf += currentField[1]*255*255;
- intBuf += currentField[2]*255;
- intBuf += currentField[3];
- relation[i].attribute[x2].tuple[x].valueInt = intBuf;
- }
- }
- }
- fclose(relation[i].dataFile);
- }
- }
- fclose(configFile);
- fclose(queryFile);
- print("Executed successfully.");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement