Sathyaish
November 19th, 2004, 03:59 PM
I am writing a program that will take a string that has a C source file and it would format all the multi-line comments like these:
//Jingle bells, jingle bells, jingles all the way
//O what fun it is to ride in a one-horse open sledge
//yeah, jingle bells....
into
/*Jingle bells, jingle bells, jingles all the way
O what fun it is to ride in a one-horse open sledge
yeah, jingle bells....*/
or the lines,
i=0; //Isn't it nice
//today? hehehehe....
into
i=0; /*Isn't it nice
today? hehehehe...*/
The code can be downloaded from the attachment. I've finished coding it but I'm still not through debugging it.
I've taken a linked list to represent a single comment block that needs to be formatted. Each node would represent one line of source code that contains a comment. The list is represented by a struct LOC that looks like this:
/*Represent a line of code that has a // comment in it
The purpose of this structure is to store those lines of
source code that:
(1) Contain a // in them
(2) Appear in a series
(3) Are a part of one multi-line comment that is to be formatted
For more information, please read the "Comments to be targetted" section
in the specification file "Read Me.txt" in this project
*/
struct LOC
{
char *Text; //Text of the line containing the comment
struct LOC *NextLine; //Pointer to the next comment
int Len; //length of the line containing the comment
int Position; //Starting position of the comment in the line of text
int WasFirstNonSpaceChar; /*A boolean value indicating if the comment
was the first non-space character in the line
of text*/
};
I'm facing a problem in my FormatBlock function. Particularly in this while loop within the FormatBlock function.
while(head!=NULL)
{
if(strcat(NewBlock, Replace(head->Text, head->Position, head->Position+1, " "))==NULL)
return NULL;
if(head->NextLine==NULL)
strcat(NewBlock, "*/");
else
strcat(NewBlock, sNewLine);
head=head->NextLine;
}
What happens is that in this while loop, somehow, while i'm only traversing the loop and replacing, say, node 3 out of 5 nodes in my linked list, the text of the 5th node in the list gets changed somehow. Just before this loop, all the elements are intact. I put printf statements at many steps and discovered that the problem is only when it comes to the 2nd element of the linked list within this loop that it changes the 5th element.
For a test, I am currently reading the comments from a file that I created called "Code.c" and put in C:.
You will find the file Code.c also within the attachment. Please download it and put the Code.c file in C: and then walk through the code.
Thoughts as to what might be causing this problem?
//Jingle bells, jingle bells, jingles all the way
//O what fun it is to ride in a one-horse open sledge
//yeah, jingle bells....
into
/*Jingle bells, jingle bells, jingles all the way
O what fun it is to ride in a one-horse open sledge
yeah, jingle bells....*/
or the lines,
i=0; //Isn't it nice
//today? hehehehe....
into
i=0; /*Isn't it nice
today? hehehehe...*/
The code can be downloaded from the attachment. I've finished coding it but I'm still not through debugging it.
I've taken a linked list to represent a single comment block that needs to be formatted. Each node would represent one line of source code that contains a comment. The list is represented by a struct LOC that looks like this:
/*Represent a line of code that has a // comment in it
The purpose of this structure is to store those lines of
source code that:
(1) Contain a // in them
(2) Appear in a series
(3) Are a part of one multi-line comment that is to be formatted
For more information, please read the "Comments to be targetted" section
in the specification file "Read Me.txt" in this project
*/
struct LOC
{
char *Text; //Text of the line containing the comment
struct LOC *NextLine; //Pointer to the next comment
int Len; //length of the line containing the comment
int Position; //Starting position of the comment in the line of text
int WasFirstNonSpaceChar; /*A boolean value indicating if the comment
was the first non-space character in the line
of text*/
};
I'm facing a problem in my FormatBlock function. Particularly in this while loop within the FormatBlock function.
while(head!=NULL)
{
if(strcat(NewBlock, Replace(head->Text, head->Position, head->Position+1, " "))==NULL)
return NULL;
if(head->NextLine==NULL)
strcat(NewBlock, "*/");
else
strcat(NewBlock, sNewLine);
head=head->NextLine;
}
What happens is that in this while loop, somehow, while i'm only traversing the loop and replacing, say, node 3 out of 5 nodes in my linked list, the text of the 5th node in the list gets changed somehow. Just before this loop, all the elements are intact. I put printf statements at many steps and discovered that the problem is only when it comes to the 2nd element of the linked list within this loop that it changes the 5th element.
For a test, I am currently reading the comments from a file that I created called "Code.c" and put in C:.
You will find the file Code.c also within the attachment. Please download it and put the Code.c file in C: and then walk through the code.
Thoughts as to what might be causing this problem?