Advertisement
BlueBear

XMLGenerator.c

Nov 20th, 2013
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define DEBUG
  6.  
  7. typedef enum resourceType
  8. {
  9.     FISH,
  10.     TRASH,
  11.     GOLD,
  12.     DIAMOND
  13. }resource_t;
  14.  
  15. typedef struct buildings
  16. {
  17.     char *name;
  18.     int level;
  19.     int hitPoints;
  20.     resource_t type;
  21.     int resourceCost;
  22.     int buildTime;
  23.     int XPGain;
  24.     struct buildings *next;
  25. }buildingTagContent_t;
  26.  
  27. typedef struct players
  28. {
  29.     // TODO complete this
  30.     struct players *next;
  31. }playerTagContent_t;
  32.  
  33. typedef struct tag
  34. {
  35.     char *tagName;
  36.     buildingTagContent_t *buildingProperties;
  37.     playerTagContent_t *playerProperties;
  38.     struct tag *next;
  39. }tag_t;
  40.  
  41.  
  42. int findString(const char *string, FILE *fp);
  43. //find string in given string
  44.  
  45. FILE *openXML(const char *name);
  46. //Opens an XML file, if exists, if not, creates
  47.  
  48. int createTag(const char *tagName, tag_t *p_tag);
  49. //
  50.  
  51. int greets();
  52. //utter bullshit
  53.  
  54.  
  55. int main(void)
  56. {
  57.     const char header[] = "<?xml version=\"1.0\"?>\n";
  58.     char filename[20];
  59.     FILE *xml;
  60.     greets();
  61.  
  62.     printf("Please specify file that you want to edit: <example.xml>\n");
  63.     gets(filename);
  64.     xml = openXML(filename);
  65.    
  66.     //plain file, init header
  67.     if(findString(header, xml) == 0)
  68.     {
  69.         rewind(xml);
  70.         fprintf(xml, "%s", header);
  71.     #ifdef DEBUG
  72.         fprintf(stdout, "Header initialised in XML\n");
  73.     #endif
  74.     }
  75.     else    //go ahead
  76.     {
  77.        
  78.     }
  79.    
  80.     system("pause");
  81.     return 0;
  82. }
  83.  
  84. int greets()
  85. {
  86.     printf("\tXML Generator/Parser/Editor/Whatever\n");
  87.     printf("\tBy Citrus Lee, Red Datura/Dreamventory\n");
  88.     printf("\t2013-XXXX\n");
  89.     printf("\tLicensed under UFWTFYW license\n");
  90.     printf("\t(Use For What The Fuck You Want license)\n\n\n");
  91.  
  92. }
  93.  
  94. int createTag(const char *tagName, tag_t *p_tag)
  95. {
  96.     if(p_tag == NULL)
  97.     {
  98.         p_tag = (tag_t *) malloc(sizeof(tag_t));
  99.         strcpy(p_tag->tagName, tagName);
  100.     }
  101. }
  102.  
  103. int findString(const char *string, FILE *fp)
  104. {
  105.     int lineNum = 0;
  106.     char temp[514];
  107.  
  108.     while(fgets(temp, 512, fp) != NULL)
  109.     {
  110.         if((strstr(temp, string)) != NULL)
  111.         {
  112.             printf("A match found on line: %d\n", lineNum);
  113.         #ifdef DEBUG
  114.             printf("\n%s\n", temp);
  115.         #endif
  116.             return 1;
  117.         }
  118.         lineNum++;
  119.     }
  120.     return 0;
  121. }
  122.  
  123. FILE *openXML(const char *name)
  124. {
  125.     FILE *xml = fopen(name, "r+");
  126.     if(xml == NULL)
  127.     {
  128.         xml = fopen(name, "w");
  129.         fprintf(stdout, "File %s does not exist, creating\n", name);
  130.         if(xml == NULL)
  131.         {
  132.             fprintf(stdout, "Could not create/open file %s at line %d\n", name, __LINE__);
  133.             exit(10);
  134.         }
  135.     }
  136.     else
  137.     {
  138.         fprintf(stdout, "File %s opened successfully...\n", name);
  139.     }
  140.     return xml;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement