I'm writing a code formatter to use on the board (similar to Lucky's but not C++). All is going well but I have a few problems which you will probably notice straight away.
1. Only one keyword is being formatted per line.
this is because I have a series of if-else-if statements so they all get skipped.
2. Keywords are being found inside other words.
3. Currently the single line comments only work when on a line by itself :rolleyes: But I haven't really looked into that properly yet. Here is a sample of some formatted code
Anyway, the most important one for me to fix right now is 1. Here is a sample of my code:Code:#include <iostream>
#include <cstdio>
// This is a comment
int main(int argc, char **argv){ // <- problem 1
int numbers[20], total;
for(int i = 0; i <= 19; i++){
numbers[i] = i;
total += i;
}
printf("This is a string\n"); // <- problem 2
return 0;
}
Code:void parse_string(char *str, FILE **out){
/* constants used for strings, comments and preprocessor parsing */
const int ON = 1;
const int OFF = 0;
const int WAIT = 11;
/* ptr to keyword in the string */
char *ptr = NULL;
int key_size = 0;
int is_prepro = 0;
int is_string = 0;
int is_sline_com = 0;
int i,j;
for(i=0;i<strlen(str);i++){
/* Check keywords */
if(ptr = strstr(str,"char")){
key_size = 4;
}
else if(ptr = strstr(str,"int")){
key_size = 3;
}
else if(ptr = strstr(str,"return")){
key_size = 6;
}
/* continues on... */
/* skipping preprocessor, comment and string parsing code */
/* If the address of str[i] is at the keyword detected... */
if(&str[i] == ptr){
/* If single line comment is OFF */
if(is_sline_com == OFF){
/* Enter blue label */
fprintf(*out,"<blue label here>");
/* print the keyword */
for(j=0;j<key_size;j++,i++){
fputc(str[i],*out);
}
/* print end label */
fprintf(*out,"<end label>");
}
}
/* If no key, print the next character */
fputc(str[i],*out);