Guest User

Hextator

a guest
Oct 14th, 2008
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.39 KB | None | 0 0
  1. //Public Domain as per author Hextator's request
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. typedef unsigned char u8;
  7.  
  8. #define PATH_LEN        4096
  9. #define EXT_LEN         16
  10.  
  11. char fileName[PATH_LEN];
  12. char inputChar[2];
  13.  
  14. #define COMMENT_CHAR        inputChar[0]
  15. #define FILENAME_CAT_STR    " Reformatted"
  16.  
  17. #define SHOWLINE        //printf("%s:%u\n", __FILE__, __LINE__)
  18.  
  19. FILE * ReadCursor;
  20. FILE * WriteCursor;
  21.  
  22. int gotoNextLine()
  23. {
  24.     while (fgetc(ReadCursor) != '\n')
  25.         if (feof(ReadCursor))
  26.         {
  27.             SHOWLINE;
  28.             return -1;
  29.         }
  30.     return ftell(ReadCursor);
  31. }
  32.  
  33. int gotoPrevLine()
  34. {
  35.     fseek(ReadCursor, -1, SEEK_CUR);
  36.     while (fgetc(ReadCursor) != '\n')
  37.     {
  38.         fseek(ReadCursor, -2, SEEK_CUR); SHOWLINE;
  39.         if (ferror(ReadCursor))
  40.         {
  41.             SHOWLINE;
  42.             return -1;
  43.         }
  44.     }
  45.     return ftell(ReadCursor);
  46. }
  47.  
  48. int main(int argc, char * argv[])
  49. {
  50.     printf("Assembly Beautifier app by Hextator.\n");
  51.     printf("This application is Public Domain.\n");
  52.     fgets(inputChar, 2, stdin);
  53.     fflush(stdin);
  54.  
  55.     sprintf(fileName, "%s", argv[1]);
  56.  
  57.     if
  58.     (
  59.     fileName[0] == '\0'
  60.     || fileName[0] == '\n'
  61.     || !strcmp(fileName, "(null)")
  62.     )
  63.     {
  64.         printf("Enter full path to target file:\n\n");
  65.         fgets(fileName, PATH_LEN, stdin);
  66.         fflush(stdin);
  67.         fileName[strlen(fileName) - 1] = '\0';
  68.         printf("\n");
  69.     }
  70.  
  71.     ReadCursor = fopen(fileName, "r");
  72.  
  73.     if (!ReadCursor)
  74.     {
  75.         printf("Read fail.\n");
  76.         fgets(inputChar, 2, stdin);
  77.         return 1;
  78.     }
  79.  
  80.     char targetName[PATH_LEN];
  81.     for (int i = 0; i < PATH_LEN; i++)
  82.         targetName[i] = fileName[i];
  83.     int cursor;
  84.     cursor = strlen(targetName);
  85.     for (; targetName[cursor] != '.'; cursor--);
  86.     char extension[EXT_LEN];
  87.     int extensionLen = 0;
  88.     for (int i = cursor; i < strlen(targetName); i++)
  89.         if (i - cursor < 16)
  90.         {
  91.             extension[i - cursor] = targetName[i];
  92.             extensionLen++;
  93.         }
  94.     extension[extensionLen] = '\0';
  95.     targetName[cursor] = '\0';
  96.     strcat(targetName, FILENAME_CAT_STR);
  97.     strcat(targetName, extension);
  98.  
  99.     WriteCursor = fopen(targetName, "w");
  100.  
  101.     if (!WriteCursor)
  102.     {
  103.         printf("Write fail.\n");
  104.         fclose(ReadCursor);
  105.         fgets(inputChar, 2, stdin);
  106.         return 1;
  107.     }
  108.  
  109.     printf("Enter the character to be used as a comment indicator:\n\n");
  110. Force_Get_Comment_Char:
  111.     fgets(inputChar, 2, stdin);
  112.     fflush(stdin);
  113.     printf("\n");
  114.     if (inputChar[0] == '\n')
  115.         goto Force_Get_Comment_Char;
  116.  
  117.     bool lastLineWasCode = false;
  118.     bool newArg = false;
  119.     bool atBeginningOfLine = true;
  120.     bool inComment = false;
  121.     bool isBranch = false;
  122.     bool isLabel = false;
  123.     bool lastCharWasLineEnd;
  124.     bool lastLineEndWasAlone;
  125.     bool isInBrace;
  126.     bool inDirective;
  127.     u8 commentChar = inputChar[0];
  128.     u8 labelString[PATH_LEN];
  129.     int mainCursorPosition = 0;
  130.     int alternateCursorPosition = 0;
  131.     int maxArgCount = 0;
  132.     int currArgCount = 0;
  133.     int charsInArg = 0;
  134.  
  135.     //Available functions:
  136.     //int gotoNextLine() - returns cursor position of next line in ReadCursor
  137.     //int gotoPrevLine() - returns cursor position of previous line in ReadCursor
  138.  
  139.     while (!feof(ReadCursor))
  140.     {
  141.         inputChar[0] = fgetc(ReadCursor); SHOWLINE;
  142.         if (feof(ReadCursor))
  143.         {
  144.             break; SHOWLINE;
  145.         }
  146.         SHOWLINE;
  147.         if (inputChar[0] == '\n')
  148.         {
  149.             if (currArgCount > maxArgCount)
  150.                 maxArgCount = currArgCount; SHOWLINE;
  151.             currArgCount = 0; SHOWLINE;
  152.             charsInArg = 0; SHOWLINE;
  153.         }
  154.         SHOWLINE;
  155.         if
  156.         (
  157.         (
  158.         inputChar[0] == ' '
  159.         || inputChar[0] == '\t'
  160.         )
  161.         && newArg
  162.         )
  163.         {
  164.             currArgCount++; SHOWLINE;
  165.             charsInArg = 0; SHOWLINE;
  166.             newArg = false; SHOWLINE;
  167.         }
  168.         else if
  169.         (
  170.         inputChar[0] == commentChar
  171.         || inputChar[0] == '.'
  172.         )
  173.         {
  174.             if (gotoNextLine() != -1)
  175.             {
  176.                 fseek(ReadCursor, -1, SEEK_CUR); SHOWLINE;
  177.             }
  178.         }
  179.         else
  180.         {
  181.             newArg = true; SHOWLINE;
  182.             charsInArg++;
  183.             if
  184.             (
  185.             !(charsInArg & 0x7)
  186.             && (charsInArg != 0)
  187.             )
  188.             {
  189.                 currArgCount++; SHOWLINE;
  190.             }
  191.         }
  192.     }
  193.  
  194.     fseek(ReadCursor, 0, SEEK_SET); SHOWLINE;
  195.     maxArgCount++; SHOWLINE;
  196.     currArgCount = 0; SHOWLINE;
  197.     charsInArg = 0; SHOWLINE;
  198.  
  199.     //Available variables:
  200.     //bool lastLineWasCode
  201.     //bool newArg
  202.     //bool atBeginningOfLine
  203.     //bool inComment
  204.     //bool isBranch
  205.     //bool isLabel
  206.     //bool lastCharWasLineEnd
  207.     //bool lastLineEndWasAlone
  208.     //bool isInBrace
  209.     //bool inDirective
  210.     //u8 commentChar
  211.     //u8 labelString[PATH_LEN]
  212.     //int mainCursorPosition
  213.     //int alternateCursorPosition
  214.     //int maxArgCount
  215.     //int currArgCount
  216.     //int charsInArg
  217.  
  218.     //Available functions:
  219.     //int gotoNextLine() - returns cursor position of next line in ReadCursor
  220.     //int gotoPrevLine() - returns cursor position of previous line in ReadCursor
  221.  
  222.     while (1)
  223.     {
  224.         inputChar[0] = fgetc(ReadCursor); SHOWLINE;
  225.         if (feof(ReadCursor))
  226.         {
  227.             if
  228.             (
  229.             !inComment
  230.             && !atBeginningOfLine
  231.             && !inDirective
  232.             && !isLabel
  233.             )
  234.             {
  235.                 for (int i = 0; i < maxArgCount - currArgCount; i++)
  236.                 {
  237.                     fprintf(WriteCursor, "\t"); SHOWLINE;
  238.                 }
  239.                 fprintf(WriteCursor, "%c", commentChar); SHOWLINE;
  240.             }
  241.             if (!lastCharWasLineEnd)
  242.             {
  243.                 fprintf(WriteCursor, "\n"); SHOWLINE;
  244.             }
  245.             break;
  246.         }
  247.         if (inputChar[0] == '\n')
  248.         {
  249.             if (lastCharWasLineEnd)
  250.             {
  251.                 lastLineEndWasAlone = true; SHOWLINE;
  252.             }
  253.             else
  254.             {
  255.                 lastLineEndWasAlone = false; SHOWLINE;
  256.             }
  257.             lastCharWasLineEnd = true; SHOWLINE;
  258.             if
  259.             (
  260.             !inComment
  261.             && !atBeginningOfLine
  262.             && !isBranch
  263.             && !inDirective
  264.             && !isLabel
  265.             && (currArgCount > 0)
  266.             )
  267.             {
  268.                 for (int i = 0; i < maxArgCount - currArgCount; i++)
  269.                 {
  270.                     fprintf(WriteCursor, "\t"); SHOWLINE;
  271.                 }
  272.                 fprintf(WriteCursor, "%c", commentChar); SHOWLINE;
  273.             }
  274.             currArgCount = 0; SHOWLINE;
  275.             inComment = false; SHOWLINE;
  276.             newArg = true; SHOWLINE;
  277.             isBranch = false; SHOWLINE;
  278.             isInBrace = false; SHOWLINE;
  279.             inDirective = false; SHOWLINE;
  280.             charsInArg = 0; SHOWLINE;
  281.             atBeginningOfLine = true; SHOWLINE;
  282.             fprintf(WriteCursor, "\n"); SHOWLINE;
  283.             if (fgetc(ReadCursor) == commentChar)
  284.             {
  285.                 fseek(ReadCursor, -1, SEEK_CUR); SHOWLINE;
  286.                 continue;
  287.             }
  288.             fseek(ReadCursor, -1, SEEK_CUR); SHOWLINE;
  289.             mainCursorPosition = ftell(ReadCursor); SHOWLINE;
  290.             isLabel = false; SHOWLINE;
  291.             labelString[0] = '\0'; SHOWLINE;
  292.             alternateCursorPosition = 0; SHOWLINE;
  293.             while((inputChar[0] = fgetc(ReadCursor)) != '\n')
  294.             {
  295.                 if (inputChar[0] == ':')
  296.                 {
  297.                     isLabel = true; SHOWLINE;
  298.                     break;
  299.                 }
  300.                 else if (feof(ReadCursor))
  301.                 {
  302.                     SHOWLINE;
  303.                     break;
  304.                 }
  305.                 else
  306.                 {
  307.                     labelString[alternateCursorPosition] = inputChar[0]; SHOWLINE;
  308.                 }
  309.                 alternateCursorPosition++; SHOWLINE;
  310.             }
  311.             labelString[alternateCursorPosition] = '\0'; SHOWLINE;
  312.             fseek(ReadCursor, mainCursorPosition, SEEK_SET); SHOWLINE;
  313.             if (isLabel)
  314.             {
  315.                 fprintf(WriteCursor, "%c-------", commentChar); SHOWLINE;
  316.                 for (int i = 1; i < maxArgCount; i++)
  317.                 {
  318.                     fprintf(WriteCursor, "--------"); SHOWLINE;
  319.                 }
  320.                 fprintf(WriteCursor, "%c%s", commentChar, labelString); SHOWLINE;
  321.                 fprintf(WriteCursor, "\n"); SHOWLINE;
  322.             }
  323.         }
  324.         else if
  325.         (
  326.         (inputChar[0] == ' '
  327.         || inputChar[0] == '\t'
  328.         )
  329.         && newArg
  330.         )
  331.         {
  332.             lastCharWasLineEnd = false; SHOWLINE;
  333.             if (inComment)
  334.             {
  335.                 fprintf(WriteCursor, "%c", inputChar[0]); SHOWLINE;
  336.                 continue;
  337.             }
  338.             else if (isInBrace)
  339.             {
  340.                 fprintf(WriteCursor, "%c", inputChar[0]); SHOWLINE;
  341.                 charsInArg++; SHOWLINE;
  342.                 if
  343.                 (
  344.                 !(charsInArg & 0x7)
  345.                 && (charsInArg != 0)
  346.                 )
  347.                 {
  348.                     currArgCount++; SHOWLINE;
  349.                 }
  350.                 continue;
  351.             }
  352.             currArgCount++; SHOWLINE;
  353.             if (newArg)
  354.             {
  355.                 fprintf(WriteCursor, "\t"); SHOWLINE;
  356.             }
  357.             newArg = false; SHOWLINE;
  358.             charsInArg = 0; SHOWLINE;
  359.         }
  360.         else if (inputChar[0] == commentChar)
  361.         {
  362.             lastCharWasLineEnd = false; SHOWLINE;
  363.             if (atBeginningOfLine)
  364.             {
  365.                 lastLineWasCode = false; SHOWLINE;
  366.             }
  367.             else
  368.                 for (int i = 0; i < maxArgCount - currArgCount; i++)
  369.                 {
  370.                     fprintf(WriteCursor, "\t"); SHOWLINE;
  371.                 }
  372.             atBeginningOfLine = false; SHOWLINE;
  373.             fprintf(WriteCursor, "%c", commentChar); SHOWLINE;
  374.             inComment = true; SHOWLINE;
  375.         }
  376.         else if (inputChar[0] == '.')
  377.         {
  378.             inDirective = true; SHOWLINE;
  379.             atBeginningOfLine = false; SHOWLINE;
  380.             fprintf(WriteCursor, "."); SHOWLINE;
  381.         }
  382.         else if (inputChar[0] == '{')
  383.         {
  384.             isInBrace = true; SHOWLINE;
  385.             charsInArg = 1; SHOWLINE;
  386.             fprintf(WriteCursor, "{"); SHOWLINE;
  387.         }
  388.         else if (inputChar[0] == '}')
  389.         {
  390.             isInBrace = false; SHOWLINE;
  391.             charsInArg++; SHOWLINE;
  392.             if
  393.             (
  394.             !(charsInArg & 0x7)
  395.             && (charsInArg != 0)
  396.             )
  397.             {
  398.                 currArgCount++; SHOWLINE;
  399.             }
  400.             fprintf(WriteCursor, "}"); SHOWLINE;       
  401.         }
  402.         else
  403.         {
  404.             lastCharWasLineEnd = false; SHOWLINE;
  405.             if
  406.             (
  407.             (
  408.             (inputChar[0] | 0x20) == 'b'
  409.             || (inputChar[0] | 0x20) == 'j'
  410.             )
  411.             && atBeginningOfLine
  412.             )
  413.             {
  414.                 if ((fgetc(ReadCursor) | 0x20) != 'i')
  415.                 {
  416.                     isBranch = true; SHOWLINE;
  417.                 }
  418.                 fseek(ReadCursor, -1, SEEK_CUR); SHOWLINE;
  419.                 if
  420.                 (
  421.                 isBranch
  422.                 && !lastLineEndWasAlone
  423.                 )
  424.                 {
  425.                     fprintf(WriteCursor, "\n"); SHOWLINE;
  426.                 }
  427.             }
  428.             atBeginningOfLine = false; SHOWLINE;
  429.             newArg = true; SHOWLINE;
  430.             fprintf(WriteCursor, "%c", inputChar[0]); SHOWLINE;
  431.             charsInArg++; SHOWLINE;
  432.             if
  433.             (
  434.             !(charsInArg & 0x7)
  435.             && (charsInArg != 0)
  436.             )
  437.             {
  438.                 currArgCount++; SHOWLINE;
  439.             }
  440.         }
  441.     }
  442.  
  443.     fclose(ReadCursor); SHOWLINE;
  444.     fclose(WriteCursor); SHOWLINE;
  445.  
  446.     //printf("\n");
  447.     printf("Success.\n"); SHOWLINE;
  448.     fgets(inputChar, 2, stdin); SHOWLINE;
  449.     return 0;
  450. }
Advertisement
Add Comment
Please, Sign In to add comment