Can you give a typical line from the file?
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
You will need to either tokenize the lines and get rid of anything after // or between /* */
(You may be able to base what you do off of something like this: http://www.daniweb.com/code/snippet217441.html )
-or-
You can write a finite state machine similar to the ones that a compiler would use to detect and strip comments from a file (or even run your source file you are testing through the C++ preprocessor -- command varies from compiler to compiler -- to strip the comments before you run this program. I am not sure if that will preserve line numbers).
-or-
You can use a regex library (e.g. Boost Regex )and do something like this . Perhaps a bit of overkill but it's the same issue.
I wish I could have been of more help to you with an algorithm. I tried writing a bit of a finite state machine with just if statements but it got a bit cumbersome and I'm sure there are more efficient/faster ways of doing it.
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
Oh, c'mon, jonsca
You will need to either tokenize the lines and get rid of anything after // or between /* */
-or-
You can write a finite state machine similar to the ones that a compiler would use...
-or-
You can use a regex library...
He saidTotally new to C++, so please forgive me...
He can't even find a double quote in the line he's read, how is he going to add aregex library, or even know what a state machine is? I myself never heard of a state machine until my girlfriend was getting her masters.
Know your audience...
Sheesh! :icon_wink:
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
Oh, c'mon, jonsca
Know your audience..
Noted. I didn't come up with anything earth shatteringly simple, so sometimes you need an elephant gun as a fly swatter. ;)
Personally, I think the preprocessor idea is the least difficult of all but I didn't try it to see if the line numbers are preserved. That way you don't have to worry about the comments because they are gone.
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
All you have to do is find the ", and if found, find the next ". Copy the stuff in between. It's simple.
Tokenize... sheesh! :icon_wink:
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
All you have to do is find the ", and if found, find the next ". Copy the stuff in between. It's simple.
Tokenize... sheesh! :icon_wink:
It's the fact that there may be string literals within the comments that he wants to ignore. /* "So if I"m parsing for quotes I'm gonna get this one" */ So you could just look for // and /* and lop off the rest of the line, but you'd miss something like "correctone" in function(/* "nevermind" */"correctone"), hence having to have some idea where your comments begin and end (enter the tokenizer).
If you were guaranteed there to be no string literals within the comments (my preprocessor idea) then it would be as simple as you propose!
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
1) check the file for all string literals
Can string literals include embedded quotations? If so, then the project becomes harder.
2) Also, I have to ensure it ignores comments that include quotations.
Again, you can use a brute force method or use find() on an STL string to find either the \\ or the \* and then see if there are quotation marks between there and the end of the line or the */, depending on which type of comment is found.
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
Of course it's harder. If you can find all the strings irregardless of the comments, it's a simple matter to modify that code to find the comments and remove them first. This ain't rocket science. If you can do one, you do the other with a minor code change. Then put them together. Again -- sheesh! I don't mind telling noobs they have to think to program, but having to tell seasoned programmers the same thing is just annoying...
And what the heck does tetron's first post have to do with anything in this thread?
WaltP
Posting Sage w/ dash of thyme
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
And what the heck does tetron's first post have to do with anything in this thread?
I think is was one of those tests where you are watching the team is passing around the basketball so intently that when a guy in a costume walks through the court you don't notice it at all? Yeah, I guess maybe he copied and pasted his post into the wrong one...
@WaltP -- you're right. I made some assumptions and I should have kept it simple.
jonsca
Quantitative Phrenologist
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581