Skip to content

Instantly share code, notes, and snippets.

Created October 19, 2012 19:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/3920066 to your computer and use it in GitHub Desktop.
Save anonymous/3920066 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void format(const char *scheme, const char *code)
{
char token[0xff], next_token[0xff];
int scheme_pos = 0;
while (sscanf(code, "%s", token) == 1)
{
code = strstr(code, token) + strlen(token); // seek code
sscanf(code, "%s", next_token);
// insert space between neighbor alpha tokens
if (isalpha(token[strlen(token) - 1]) && isalpha(next_token[0]))
strcat(token, " ");
int asterisks_to_read = strlen(token);
char ch;
do
{
ch = scheme[scheme_pos++];
if (ch == ' ')
printf(" ");
else if (ch == '*')
--asterisks_to_read;
else if (ch == '\n')
break;
} while (asterisks_to_read > 0 && ch != '\0');
printf("%s", token);
if (ch == '\n')
printf("\n");
}
}
void read_into_string(const char *fname, char *string)
{
FILE *file = fopen(fname, "r");
char ch;
int n = 0;
do
{
ch = fgetc(file);
if (ch == '(' || ch == ')' || ch == ';' || ch == ',' || ch == '{' || ch == '}')
{
string[n++] = ' ';
string[n++] = ch;
string[n++] = ' ';
}
else
string[n++] = ch;
} while (ch != EOF);
fclose(file);
}
int main(int argc, char **argv)
{
if (argc != 3)
return 1;
char scheme[0xffff];
char code[0xffff];
memset(code, 0, 0xffff * sizeof(char));
memset(scheme, 0, 0xffff * sizeof(char));
read_into_string(argv[1], scheme);
read_into_string(argv[2], code);
format(scheme, code);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment