Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Public Domain as per author Hextator's request
- #include <stdio.h>
- #include <string.h>
- typedef unsigned char u8;
- #define PATH_LEN 4096
- #define EXT_LEN 16
- char fileName[PATH_LEN];
- char inputChar[2];
- #define COMMENT_CHAR inputChar[0]
- #define FILENAME_CAT_STR " Reformatted"
- #define SHOWLINE //printf("%s:%u\n", __FILE__, __LINE__)
- FILE * ReadCursor;
- FILE * WriteCursor;
- int gotoNextLine()
- {
- while (fgetc(ReadCursor) != '\n')
- if (feof(ReadCursor))
- {
- SHOWLINE;
- return -1;
- }
- return ftell(ReadCursor);
- }
- int gotoPrevLine()
- {
- fseek(ReadCursor, -1, SEEK_CUR);
- while (fgetc(ReadCursor) != '\n')
- {
- fseek(ReadCursor, -2, SEEK_CUR); SHOWLINE;
- if (ferror(ReadCursor))
- {
- SHOWLINE;
- return -1;
- }
- }
- return ftell(ReadCursor);
- }
- int main(int argc, char * argv[])
- {
- printf("Assembly Beautifier app by Hextator.\n");
- printf("This application is Public Domain.\n");
- fgets(inputChar, 2, stdin);
- fflush(stdin);
- sprintf(fileName, "%s", argv[1]);
- if
- (
- fileName[0] == '\0'
- || fileName[0] == '\n'
- || !strcmp(fileName, "(null)")
- )
- {
- printf("Enter full path to target file:\n\n");
- fgets(fileName, PATH_LEN, stdin);
- fflush(stdin);
- fileName[strlen(fileName) - 1] = '\0';
- printf("\n");
- }
- ReadCursor = fopen(fileName, "r");
- if (!ReadCursor)
- {
- printf("Read fail.\n");
- fgets(inputChar, 2, stdin);
- return 1;
- }
- char targetName[PATH_LEN];
- for (int i = 0; i < PATH_LEN; i++)
- targetName[i] = fileName[i];
- int cursor;
- cursor = strlen(targetName);
- for (; targetName[cursor] != '.'; cursor--);
- char extension[EXT_LEN];
- int extensionLen = 0;
- for (int i = cursor; i < strlen(targetName); i++)
- if (i - cursor < 16)
- {
- extension[i - cursor] = targetName[i];
- extensionLen++;
- }
- extension[extensionLen] = '\0';
- targetName[cursor] = '\0';
- strcat(targetName, FILENAME_CAT_STR);
- strcat(targetName, extension);
- WriteCursor = fopen(targetName, "w");
- if (!WriteCursor)
- {
- printf("Write fail.\n");
- fclose(ReadCursor);
- fgets(inputChar, 2, stdin);
- return 1;
- }
- printf("Enter the character to be used as a comment indicator:\n\n");
- Force_Get_Comment_Char:
- fgets(inputChar, 2, stdin);
- fflush(stdin);
- printf("\n");
- if (inputChar[0] == '\n')
- goto Force_Get_Comment_Char;
- bool lastLineWasCode = false;
- bool newArg = false;
- bool atBeginningOfLine = true;
- bool inComment = false;
- bool isBranch = false;
- bool isLabel = false;
- bool lastCharWasLineEnd;
- bool lastLineEndWasAlone;
- bool isInBrace;
- bool inDirective;
- u8 commentChar = inputChar[0];
- u8 labelString[PATH_LEN];
- int mainCursorPosition = 0;
- int alternateCursorPosition = 0;
- int maxArgCount = 0;
- int currArgCount = 0;
- int charsInArg = 0;
- //Available functions:
- //int gotoNextLine() - returns cursor position of next line in ReadCursor
- //int gotoPrevLine() - returns cursor position of previous line in ReadCursor
- while (!feof(ReadCursor))
- {
- inputChar[0] = fgetc(ReadCursor); SHOWLINE;
- if (feof(ReadCursor))
- {
- break; SHOWLINE;
- }
- SHOWLINE;
- if (inputChar[0] == '\n')
- {
- if (currArgCount > maxArgCount)
- maxArgCount = currArgCount; SHOWLINE;
- currArgCount = 0; SHOWLINE;
- charsInArg = 0; SHOWLINE;
- }
- SHOWLINE;
- if
- (
- (
- inputChar[0] == ' '
- || inputChar[0] == '\t'
- )
- && newArg
- )
- {
- currArgCount++; SHOWLINE;
- charsInArg = 0; SHOWLINE;
- newArg = false; SHOWLINE;
- }
- else if
- (
- inputChar[0] == commentChar
- || inputChar[0] == '.'
- )
- {
- if (gotoNextLine() != -1)
- {
- fseek(ReadCursor, -1, SEEK_CUR); SHOWLINE;
- }
- }
- else
- {
- newArg = true; SHOWLINE;
- charsInArg++;
- if
- (
- !(charsInArg & 0x7)
- && (charsInArg != 0)
- )
- {
- currArgCount++; SHOWLINE;
- }
- }
- }
- fseek(ReadCursor, 0, SEEK_SET); SHOWLINE;
- maxArgCount++; SHOWLINE;
- currArgCount = 0; SHOWLINE;
- charsInArg = 0; SHOWLINE;
- //Available variables:
- //bool lastLineWasCode
- //bool newArg
- //bool atBeginningOfLine
- //bool inComment
- //bool isBranch
- //bool isLabel
- //bool lastCharWasLineEnd
- //bool lastLineEndWasAlone
- //bool isInBrace
- //bool inDirective
- //u8 commentChar
- //u8 labelString[PATH_LEN]
- //int mainCursorPosition
- //int alternateCursorPosition
- //int maxArgCount
- //int currArgCount
- //int charsInArg
- //Available functions:
- //int gotoNextLine() - returns cursor position of next line in ReadCursor
- //int gotoPrevLine() - returns cursor position of previous line in ReadCursor
- while (1)
- {
- inputChar[0] = fgetc(ReadCursor); SHOWLINE;
- if (feof(ReadCursor))
- {
- if
- (
- !inComment
- && !atBeginningOfLine
- && !inDirective
- && !isLabel
- )
- {
- for (int i = 0; i < maxArgCount - currArgCount; i++)
- {
- fprintf(WriteCursor, "\t"); SHOWLINE;
- }
- fprintf(WriteCursor, "%c", commentChar); SHOWLINE;
- }
- if (!lastCharWasLineEnd)
- {
- fprintf(WriteCursor, "\n"); SHOWLINE;
- }
- break;
- }
- if (inputChar[0] == '\n')
- {
- if (lastCharWasLineEnd)
- {
- lastLineEndWasAlone = true; SHOWLINE;
- }
- else
- {
- lastLineEndWasAlone = false; SHOWLINE;
- }
- lastCharWasLineEnd = true; SHOWLINE;
- if
- (
- !inComment
- && !atBeginningOfLine
- && !isBranch
- && !inDirective
- && !isLabel
- && (currArgCount > 0)
- )
- {
- for (int i = 0; i < maxArgCount - currArgCount; i++)
- {
- fprintf(WriteCursor, "\t"); SHOWLINE;
- }
- fprintf(WriteCursor, "%c", commentChar); SHOWLINE;
- }
- currArgCount = 0; SHOWLINE;
- inComment = false; SHOWLINE;
- newArg = true; SHOWLINE;
- isBranch = false; SHOWLINE;
- isInBrace = false; SHOWLINE;
- inDirective = false; SHOWLINE;
- charsInArg = 0; SHOWLINE;
- atBeginningOfLine = true; SHOWLINE;
- fprintf(WriteCursor, "\n"); SHOWLINE;
- if (fgetc(ReadCursor) == commentChar)
- {
- fseek(ReadCursor, -1, SEEK_CUR); SHOWLINE;
- continue;
- }
- fseek(ReadCursor, -1, SEEK_CUR); SHOWLINE;
- mainCursorPosition = ftell(ReadCursor); SHOWLINE;
- isLabel = false; SHOWLINE;
- labelString[0] = '\0'; SHOWLINE;
- alternateCursorPosition = 0; SHOWLINE;
- while((inputChar[0] = fgetc(ReadCursor)) != '\n')
- {
- if (inputChar[0] == ':')
- {
- isLabel = true; SHOWLINE;
- break;
- }
- else if (feof(ReadCursor))
- {
- SHOWLINE;
- break;
- }
- else
- {
- labelString[alternateCursorPosition] = inputChar[0]; SHOWLINE;
- }
- alternateCursorPosition++; SHOWLINE;
- }
- labelString[alternateCursorPosition] = '\0'; SHOWLINE;
- fseek(ReadCursor, mainCursorPosition, SEEK_SET); SHOWLINE;
- if (isLabel)
- {
- fprintf(WriteCursor, "%c-------", commentChar); SHOWLINE;
- for (int i = 1; i < maxArgCount; i++)
- {
- fprintf(WriteCursor, "--------"); SHOWLINE;
- }
- fprintf(WriteCursor, "%c%s", commentChar, labelString); SHOWLINE;
- fprintf(WriteCursor, "\n"); SHOWLINE;
- }
- }
- else if
- (
- (inputChar[0] == ' '
- || inputChar[0] == '\t'
- )
- && newArg
- )
- {
- lastCharWasLineEnd = false; SHOWLINE;
- if (inComment)
- {
- fprintf(WriteCursor, "%c", inputChar[0]); SHOWLINE;
- continue;
- }
- else if (isInBrace)
- {
- fprintf(WriteCursor, "%c", inputChar[0]); SHOWLINE;
- charsInArg++; SHOWLINE;
- if
- (
- !(charsInArg & 0x7)
- && (charsInArg != 0)
- )
- {
- currArgCount++; SHOWLINE;
- }
- continue;
- }
- currArgCount++; SHOWLINE;
- if (newArg)
- {
- fprintf(WriteCursor, "\t"); SHOWLINE;
- }
- newArg = false; SHOWLINE;
- charsInArg = 0; SHOWLINE;
- }
- else if (inputChar[0] == commentChar)
- {
- lastCharWasLineEnd = false; SHOWLINE;
- if (atBeginningOfLine)
- {
- lastLineWasCode = false; SHOWLINE;
- }
- else
- for (int i = 0; i < maxArgCount - currArgCount; i++)
- {
- fprintf(WriteCursor, "\t"); SHOWLINE;
- }
- atBeginningOfLine = false; SHOWLINE;
- fprintf(WriteCursor, "%c", commentChar); SHOWLINE;
- inComment = true; SHOWLINE;
- }
- else if (inputChar[0] == '.')
- {
- inDirective = true; SHOWLINE;
- atBeginningOfLine = false; SHOWLINE;
- fprintf(WriteCursor, "."); SHOWLINE;
- }
- else if (inputChar[0] == '{')
- {
- isInBrace = true; SHOWLINE;
- charsInArg = 1; SHOWLINE;
- fprintf(WriteCursor, "{"); SHOWLINE;
- }
- else if (inputChar[0] == '}')
- {
- isInBrace = false; SHOWLINE;
- charsInArg++; SHOWLINE;
- if
- (
- !(charsInArg & 0x7)
- && (charsInArg != 0)
- )
- {
- currArgCount++; SHOWLINE;
- }
- fprintf(WriteCursor, "}"); SHOWLINE;
- }
- else
- {
- lastCharWasLineEnd = false; SHOWLINE;
- if
- (
- (
- (inputChar[0] | 0x20) == 'b'
- || (inputChar[0] | 0x20) == 'j'
- )
- && atBeginningOfLine
- )
- {
- if ((fgetc(ReadCursor) | 0x20) != 'i')
- {
- isBranch = true; SHOWLINE;
- }
- fseek(ReadCursor, -1, SEEK_CUR); SHOWLINE;
- if
- (
- isBranch
- && !lastLineEndWasAlone
- )
- {
- fprintf(WriteCursor, "\n"); SHOWLINE;
- }
- }
- atBeginningOfLine = false; SHOWLINE;
- newArg = true; SHOWLINE;
- fprintf(WriteCursor, "%c", inputChar[0]); SHOWLINE;
- charsInArg++; SHOWLINE;
- if
- (
- !(charsInArg & 0x7)
- && (charsInArg != 0)
- )
- {
- currArgCount++; SHOWLINE;
- }
- }
- }
- fclose(ReadCursor); SHOWLINE;
- fclose(WriteCursor); SHOWLINE;
- //printf("\n");
- printf("Success.\n"); SHOWLINE;
- fgets(inputChar, 2, stdin); SHOWLINE;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment