Advertisement
Guest User

Walking on air

a guest
Mar 29th, 2015
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef enum { false, true } bool;
  5.  
  6. //17:49 - Dank memes
  7.  
  8. //Note to self, have files closed when program exits due to error
  9. //Also, be sure to make errors actually output the specific source of the error
  10.  
  11. void print(char *text)
  12. {
  13.     int len = strlen(text);
  14.     char *str2 = malloc(len+2);
  15.     strcpy(str2,text);
  16.     str2[len] = '\n';
  17.     str2[len+1] = '\0';
  18.     printf(str2);
  19.     free(str2);
  20. }
  21.  
  22. void error(char *text)
  23. {
  24.     int len = strlen(text);
  25.     char *str2 = malloc(len+2);
  26.     strcpy(str2,text);
  27.     str2[len] = '\n';
  28.     str2[len+1] = '\0';
  29.     fprintf(stderr,str2);
  30.     free(str2);
  31. }
  32.  
  33. bool isValidRelationName(char* relName)
  34. {
  35.     if(strlen(relName) < 1)
  36.         return false;
  37.     if(strlen(relName) > 15)
  38.         return false;
  39.     unsigned int i = 0;
  40.     for(i = 0; i<strlen(relName); i++)
  41.         if(!((relName[i] >= 'a' && relName[i] <= 'z') || (relName[i] >= 'A' && relName[i] <= 'Z') || (relName[i] >= '0' && relName[i] <= '9') || relName[i] == '_' ))
  42.             return false;
  43.     if((relName[0] >= 'a' && relName[0] <= 'z') || (relName[0] >= 'A' && relName[0] <= 'Z'))
  44.         return true;
  45.     else
  46.         return false;
  47. }
  48.  
  49. struct tupleStruct
  50. {
  51.     int valueInt;
  52.     char valueStr[255];
  53. };
  54.  
  55. struct attributeStruct
  56. {
  57.     char name[15];
  58.     bool type;
  59.     int length;
  60.     struct tupleStruct tuple[100];
  61. };
  62.  
  63. struct relationStruct
  64. {
  65.     char name[15];
  66.     FILE *schemaFile;
  67.     FILE *dataFile;
  68.     int attributeCount;
  69.     int tupleCount;
  70.     struct attributeStruct attribute[10];
  71. };
  72.  
  73. int getRelationLength(struct relationStruct in)
  74. {
  75.     int ret = 0;
  76.     int i = 0;
  77.     for(i = 0; i<in.attributeCount; i++)
  78.         ret += in.attribute[i].length;
  79.     return ret;
  80. }
  81.  
  82. struct relationStruct relation[10];
  83.  
  84. FILE *configFile;
  85. FILE *queryFile;
  86.  
  87. int main(int argc, char *argv[])
  88. {
  89.     if(argc < 3)
  90.     {
  91.         error("Not enough command line arguments.");
  92.         return 1;
  93.     }
  94.  
  95.     configFile = fopen(argv[1], "r");
  96.     if (!configFile)
  97.     {
  98.         error("Could not open config file.");
  99.         return 1;
  100.     }
  101.  
  102.     queryFile = fopen(argv[2],"r");
  103.     if(!queryFile)
  104.     {
  105.         error("Could not open query file.");
  106.         return 1;
  107.     }
  108.  
  109.     int numRelations = -1;
  110.     int ret = fscanf(configFile,"%d",&numRelations);
  111.  
  112.     if(ret != 1)
  113.     {
  114.         error("Could not get number of relations from config file.");
  115.         return 1;
  116.     }
  117.  
  118.     if(numRelations < 1 || numRelations > 10)
  119.     {
  120.         error("Number of relations in config file exceeded bounds! (1-10)");
  121.         return 1;
  122.     }
  123.  
  124.     int i = 0;
  125.     for(i = 0; i<numRelations; i++)
  126.     {
  127.         char relName[255];
  128.         int ret = fscanf(configFile,"%s",relName);
  129.         if(ret != 1)
  130.         {
  131.             error("Could not get name of relation from config file.");
  132.             return 1;
  133.         }
  134.         if(!isValidRelationName(relName))
  135.         {
  136.             error("Invalid relation name.");
  137.             return 1;
  138.         }
  139.  
  140.         char dataName[255];
  141.         strcpy(dataName,relName);
  142.         int tmpLen = strlen(dataName);
  143.         dataName[tmpLen] = '.';
  144.         dataName[tmpLen+1] = 'd';
  145.         dataName[tmpLen+2] = 'a';
  146.         dataName[tmpLen+3] = 't';
  147.         dataName[tmpLen+4] = '\0';
  148.  
  149.         strcpy(relation[i].name,relName);
  150.  
  151.         tmpLen = strlen(relName);
  152.         relName[tmpLen] = '.';
  153.         relName[tmpLen+1] = 's';
  154.         relName[tmpLen+2] = 'c';
  155.         relName[tmpLen+3] = 'h';
  156.         relName[tmpLen+4] = '\0';
  157.         relation[i].schemaFile = fopen(relName,"r");
  158.  
  159.         if(!relation[i].schemaFile)
  160.         {
  161.             error("Could not open schema file:");
  162.             error(relName);
  163.             return 1;
  164.         }
  165.         else
  166.         {
  167.             int retB = fscanf(relation[i].schemaFile,"%d",&relation[i].attributeCount);
  168.             if(retB != 1)
  169.             {
  170.                 error("Could not get number of attributes from schema file.");
  171.                 return 1;
  172.             }
  173.             if(relation[i].attributeCount < 1 || relation[i].attributeCount > 10)
  174.             {
  175.                 error("Attribute count out of bounds!");
  176.                 return 1;
  177.             }
  178.  
  179.             int q = 0;
  180.             for(q = 0; q<relation[i].attributeCount; q++)
  181.             {
  182.                 char attributeName[255];
  183.                 char attributeType[10];
  184.                 int attributeLength;
  185.                 int retC = fscanf(relation[i].schemaFile,"%s %s %d",attributeName,attributeType,&attributeLength);
  186.                 if(retC != 3)
  187.                 {
  188.                     error("Could not get attribute data from schema file.");
  189.                     return 1;
  190.                 }
  191.                 if(!isValidRelationName(attributeName))
  192.                 {
  193.                     error("Invalid attribute name from schema file.");
  194.                     return 1;
  195.                 }
  196.                 relation[i].attribute[q].length = attributeLength;
  197.                 strcpy(relation[i].attribute[q].name,attributeName);
  198.                 if(attributeType[0] == 'S')
  199.                     relation[i].attribute[q].type = true;
  200.                 else if(attributeType[0] == 'I')
  201.                     relation[i].attribute[q].type = false;
  202.                 else
  203.                 {
  204.                     error("Invalid attribute type not I or S in schema file.");
  205.                     return 1;
  206.                 }
  207.             }
  208.  
  209.             fclose(relation[i].schemaFile);
  210.         }
  211.  
  212.         relation[i].dataFile = fopen(dataName,"rb");
  213.  
  214.         if(!relation[i].dataFile)
  215.         {
  216.             error("Could not open data file for realtion.");
  217.             return 1;
  218.         }
  219.         else
  220.         {
  221.             int relationLength = getRelationLength(relation[i]);
  222.             int sz = 0;
  223.             fseek(relation[i].dataFile, 0L, SEEK_END);
  224.             sz = ftell(relation[i].dataFile);
  225.             fseek(relation[i].dataFile, 0L, SEEK_SET);
  226.             double tupleUnRounded = ((double)sz)/((double)relationLength);
  227.             int tupleCount = (int)tupleUnRounded;
  228.             if(((double)tupleCount) != tupleUnRounded)
  229.             {
  230.                 error("Data file contains incomplete reccord.");
  231.                 return 1;
  232.             }
  233.             relation[i].tupleCount = tupleCount;
  234.  
  235.             char currentTuple[relationLength];
  236.             int x = 0;
  237.             for(x = 0; x<relation[i].tupleCount; x++)
  238.             {
  239.                 int r = 0;
  240.                 for(r = 0; r<relationLength; r++)
  241.                     currentTuple[r] = fgetc(relation[i].dataFile);
  242.  
  243.                 int pointerPos = 0;
  244.  
  245.                 int x2 = 0;
  246.                 for(x2 = 0; x2<relation[i].attributeCount; x2++)
  247.                 {
  248.                     char currentField[relation[i].attribute[x2].length+1];
  249.                     int x3 = 0;
  250.                     for(x3 = 0; x3 < relation[i].attribute[x2].length; x3++)
  251.                         currentField[x3] = currentTuple[x3+pointerPos];
  252.                     currentField[relation[i].attribute[x2].length] = '\0';
  253.                     pointerPos += relation[i].attribute[x2].length;
  254.                     if(relation[i].attribute[x2].type)
  255.                     {
  256.                         strcpy(relation[i].attribute[x2].tuple[x].valueStr,currentField);
  257.                         print(relation[i].attribute[x2].tuple[x].valueStr);
  258.                     }
  259.                     else
  260.                     {
  261.                         unsigned int intBuf = 0;
  262.                         intBuf = currentField[0]*255*255*255;
  263.                         intBuf += currentField[1]*255*255;
  264.                         intBuf += currentField[2]*255;
  265.                         intBuf += currentField[3];
  266.                         relation[i].attribute[x2].tuple[x].valueInt = intBuf;
  267.                     }
  268.                 }
  269.             }
  270.             fclose(relation[i].dataFile);
  271.         }
  272.     }
  273.  
  274.     fclose(configFile);
  275.     fclose(queryFile);
  276.  
  277.     print("Executed successfully.");
  278.  
  279.     return 0;
  280. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement