Finished 1-23
This commit is contained in:
parent
03c0df163f
commit
6edf3378f9
|
@ -1,55 +1,38 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int com = 0;
|
||||
|
||||
/*
|
||||
* TODO:
|
||||
* Keep backbuffer of 1 character (current & i-1) to check for comment ends
|
||||
* Handle double slashes nicely
|
||||
* Find a cleaner way to achieve this
|
||||
*/
|
||||
int incomment = 0;
|
||||
|
||||
int uncomment(char s[],int lim) {
|
||||
int c, i, quo = 0, wnl = 0;
|
||||
/* c is character integer
|
||||
* i is column
|
||||
* com is current comment state
|
||||
* quo is current quote state */
|
||||
for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n'; ++i) {
|
||||
/* Check if we are in a character constant or between quotes, if yes ignore comments */
|
||||
if (c == '"' || (c == '\'' && s[i-1] != '"')) {
|
||||
if (quo == 1) {
|
||||
quo = 0; //test
|
||||
} else {
|
||||
quo = 1;
|
||||
}
|
||||
} else if (quo == 0 && (c == '/' || c == '*')) {
|
||||
if (com == 0) {
|
||||
if (c == '*' && s[i-1] == '/') {
|
||||
com = 1;
|
||||
} else if (c == '/' && s[i-1] == '/') {
|
||||
wnl = 1;
|
||||
s[i-1] = ' ';
|
||||
}
|
||||
} else if (com == 1 && c == '/' && s[i-1] == '*') {
|
||||
com = 0;
|
||||
int c, i, nw = 0, lastchar;
|
||||
for (i=0; i < lim-1 && (c=getchar())!=EOF && c!='\n';) {
|
||||
// Check the state of the comment, 0 being "not in a comment", 1 being "in a comment" and 2 being "exiting from a comment", reset 2 afterwards
|
||||
if (incomment == 2) {
|
||||
incomment = 0;
|
||||
};
|
||||
if (c == '/') {
|
||||
if (lastchar == '/') {
|
||||
nw = 1;
|
||||
s[i--] = ' ';
|
||||
} else if(lastchar == '*') {
|
||||
incomment = 2;
|
||||
}
|
||||
} else if (c == '*' && lastchar == '/') {
|
||||
incomment = 1;
|
||||
s[i--] = ' ';
|
||||
}
|
||||
if (com == 0 && wnl == 0) {
|
||||
if (incomment == 0 && nw == 0) {
|
||||
s[i] = c;
|
||||
} else {
|
||||
s[i] = ' ';
|
||||
++i;
|
||||
}
|
||||
lastchar = c;
|
||||
}
|
||||
/* If the line is completly empty, do not print a new line, jump to the next line */
|
||||
for (;i > 0 && (s[i-1] == ' ' || s[i-1] == '\t'); i--);
|
||||
if (i == 0) {
|
||||
if (incomment == 1 || nw == 1) {
|
||||
s[i] = '\0';
|
||||
} else {
|
||||
s[i] = '\n';
|
||||
s[i+1] = '\0';
|
||||
}
|
||||
/* If we have hit the end of the line, tell the previous function, else return the lenght */
|
||||
/* If we have hit the end of the line, tell the previous function, else return the lenght */
|
||||
if (c == EOF) {
|
||||
return EOF;
|
||||
} else {
|
||||
|
|
Loading…
Reference in New Issue