Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include <string>
  5. #include <map>
  6. #include <set>
  7. #include <vector>
  8. #include "math.h"
  9.  
  10. #include "../flex/flex.h"
  11.  
  12. FILE* outFile;
  13. std::string lineStr;
  14.  
  15. void WriteLine(bool addNewline, bool finalize=false)
  16. {
  17.     if(addNewline) lineStr += "\\n";
  18.     if(lineStr.length() > 64 || finalize)
  19.     {
  20.         fprintf(outFile, "\"%s\"\n", lineStr.c_str());
  21.         lineStr.clear();
  22.     }
  23. }
  24.  
  25. bool IsChainable(LexToken token)
  26. {
  27.     switch(token)
  28.     {
  29.     case LEX_NEWLINE:
  30.     case LEX_WHITESPACE:
  31.     case LEX_OPERATORS:
  32.     case LEX_MEMBER:
  33.         return true;
  34.     };
  35.     return false;
  36. }
  37.  
  38. std::string GetNextIDString()
  39. {
  40.     static int idBuffer[512] = {0};
  41.     static char strBuffer[512] = {0};
  42.     const char* chrsS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
  43.     const char* chrsI = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789";
  44.     static int len = 1;
  45.  
  46.     // Build string
  47.     memset(strBuffer, 0, 512);
  48.     for(int i=0; i<len; ++i)
  49.     {
  50.         const char* chrs = (i == 0 ? chrsS : chrsI);
  51.         strBuffer[i] = chrs[idBuffer[i]];
  52.     }
  53.    
  54.     // Increment
  55.     int dec = 0;
  56.     bool carry = true;
  57.     while(carry)
  58.     {
  59.         const int max = (dec == 0 ? strlen(chrsS) : strlen(chrsI)) - 1;
  60.         ++idBuffer[dec];
  61.         carry = (idBuffer[dec] == max);
  62.         if(carry)
  63.         {
  64.             idBuffer[dec] = 0;
  65.             carry = true;
  66.             ++len, ++dec;
  67.         }
  68.     }
  69.     std::string result(strBuffer);
  70.     return result;
  71. }
  72.  
  73. struct IDstring
  74. {
  75.     IDstring() : str(GetNextIDString()){}
  76.     std::string str;
  77. };
  78. std::map<std::string, IDstring> IDmap;
  79. std::set<std::string> externIDs;
  80.  
  81. void ParseToken(const std::string& string,
  82.                 LexToken llastToken, LexToken lastToken, LexToken token, LexToken nextToken)
  83. {
  84.     static bool externType = false;
  85.     static bool preprocLine = false;
  86.     switch(token)
  87.     {
  88.     case LEX_WHITESPACE:
  89.         if(!IsChainable(lastToken) && !IsChainable(nextToken)) lineStr += ' ';
  90.         break;
  91.     case LEX_IDENTIFIER:
  92.     {
  93.         if(externType) externIDs.insert(string);
  94.         std::string name = string;
  95.         if(externIDs.find(string) == externIDs.end() && lastToken!=LEX_MEMBER)
  96.         {
  97.             name = IDmap[string].str;
  98.         }
  99.         lineStr += name;
  100.         externType = false;
  101.         break;
  102.     }
  103.     case LEX_VALUE:
  104.         lineStr += string; break;
  105.     case LEX_PREPROCESSOR:
  106.         preprocLine = true;
  107.         lineStr += string; break;
  108.     case LEX_RESERVED:
  109.         lineStr += string; break;
  110.     case LEX_MODIFIER_EXTERN:
  111.         lineStr += string;
  112.         break;
  113.     case LEX_MODIFIER_INTERN:
  114.         lineStr += string; break;
  115.     case LEX_TYPE:
  116.         externType = (llastToken == LEX_MODIFIER_EXTERN);
  117.         lineStr += string; break;
  118.     case LEX_OPERATORS:
  119.         lineStr += string; break;
  120.     case LEX_MEMBER:
  121.         lineStr += string; break;
  122.     case LEX_UNKNOWN:
  123.         printf("Unknown: \"%s\"\n", string.c_str());
  124.         lineStr += string;
  125.         break;
  126.     case LEX_NEWLINE:
  127.         WriteLine(preprocLine);
  128.         preprocLine = false;
  129.         break;
  130.     };
  131. }
  132. void _FlexHandleToken(const char* string, LexToken token)
  133. {
  134.     static std::string thisString="";
  135.     static LexToken llastToken=LEX_NULL, lastToken=LEX_NULL, thisToken=LEX_NULL;
  136.     if(thisToken) ParseToken(thisString, llastToken, lastToken, thisToken, token);
  137.     thisString = string;
  138.     llastToken = lastToken;
  139.     lastToken = thisToken;
  140.     thisToken = token;
  141. }
  142.  
  143. int main()
  144. {
  145.     const int argc = __argc;
  146.     char** argv = __argv;
  147.  
  148.     system("cls");
  149.     char* fileName = "test.frag";
  150.     if(argc > 1) fileName = argv[1];
  151.  
  152.     // Try to open file
  153.     printf("Compressing \"%s\"...\n", fileName);
  154.     FILE* fp = 0;
  155.     fopen_s(&fp, fileName, "r");
  156.     if(!fp)
  157.     {
  158.         printf("Error: unable to open file!");
  159.     }
  160.     else
  161.     {
  162.         fclose(fp);
  163.         const std::string outFileName = std::string(fileName) + ".out";
  164.         fopen_s(&outFile, outFileName.c_str(), "w");
  165.         _FlexHandleFile(fileName);
  166.         _FlexHandleToken("", LEX_NULL);
  167.         WriteLine(false, true);
  168.         fclose(outFile);
  169.         printf("Done.\n");
  170.     }
  171.     return 0;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement