C++: Stacks and Printing something out when you reach the end of a file or when reading the last character in a file - ifstream

I need to read from a file, character by character.
My specific problem:
If it finds a /*, it puts them into the stack, and then goes into "comment mode" where it ignores everything until it finds a */. If it never finds the matching pair */* in the entire file, it should print out "unbalanced symbol /" but it never prints that*

The program now needs to handle two states/modes instead of one:
It starts in "matching mode", putting ( { [ on the stack and popping ) } ] if they match.
As soon as you read a /*, you enter "comment mode" and ignore everything until you read */, at which point you return to "matching mode" with the previous stack. You don't need a stack while in this mode. The same goes for " and '.
If you reach end of input while in "comment mode" you print out: "unbalanced symbol" with the symbol that made you enter that mode.

Related

How to replace a string pattern with different strings quickly?

For example, I have many HTML tabs to style, they use different classes, and will have different backgrounds. Background images files have names corresponding to class names.
The way I found to do it is yank:
.tab.home {
background: ...home.jpg...
}
then paste, then :s/home/about.
This is to be repeated for a few times. I found that & can be used to repeat last substitute, but only for the same target string. What is the quickest way to repeat a substitute with different target string?
Alternatively, probably there are more efficient ways to do such a thing?
I had a quick play with some vim macro magic and came up with the following idea... I apologise for the length. I thought it best to explain the steps..
First, place the text block you want to repeat into a register (I picked register z), so with the cursor at the beginning of the .tab line I pressed "z3Y (select reg z and yank 3 lines).
Then I entered the series of VIM commands I wanted into the buffer as )"zp:.,%s/home/. (Just press i and type the commands)
This translate to;
) go the end of the current '{}' block,
"zp paste a copy of the text in register z,
.,%s/home/ which has two tricks.
The .,% ensures the substitution applies to everything from the start of the .tab to the end of the closing }, and,
The command is incomplete (ie, does not have a at the end), so vim will prompt me to complete the command.
Note that while %s/// will perform a substitution across every line of the file, it is important to realise that % is an alias for range 1,$. Using 1,% as a range, causes the % to be used as the 'jump to matching parenthesis' operator, resulting in a range from the current line to the end of the % match. (which in this example, is the closing brace in the block)
Then, after placing the cursor on the ) at the beginning of the line, I typed "qy$ which means yank all characters to the end of the line into register q.
This is important, because simply yanking the line with Y will include a carriage return in the register, and will cause the macro to fail.
I then executed the content of register q with #q and I was prompted to complete the s/home/ on the command line.
After typing the replacement text and pressing enter, the pasted block (from register z) appeared in the buffer with the substitutions already applied.
At this point you can repeat the last #qby simple typing ##. You don't even need to move the cursor down to the end of the block because the ) at the start of the macro does that for you.
This effectively reduces the process of yanking the original text, inserting it, and executing two manual replace commands into a simple ##.
You can safely delete the macro string from your edit buffer when done.
This is incredibly vim-ish, and might waste a bit of time getting it right, but it could save you even more when you do.
Vim macro's might be the trick you are looking for.
From the manual, I found :s//new-replacement. Seemed to be too much typing.
Looking for a better answer.

Using com.opencsv.CSVReader on windows stops reading lines prematurely

I have two files that are identical except for the line ending codes. The one that uses the newline (linux/Unix)character works (reads all 550 rows of data) and the one that uses carriage return and line feed (Windows) stops returning lines after reading 269 lines. In both cases the data is read correctly up to the point where they stop.
If I run dos2unix on the file that fails, the resulting file works.
I would like to be able read CSV files regardless of their origin. If I could at least detect that the file is in the wrong format before reading part of the data that would be helpful
Even if I could tell at any time in the middle of reading the file that it was not going to work, I could output an error.
My current state of reading half the file and terminating with no error is dangerous.
The problem is that under the covers openCSV uses a BufferedReader which reads a line from the stream until it gets to the Systems line.seperator.
If you know beforehand what the line separator of the file is then in your application just do a System.setProperty("line.separator", newLine) where newLine is either "\n" or "\r\n" based on the file you are about to parse. Or you can pass that in as a parameter.
If you want to automatically detect the file character. Create a method that will take the file you want, create a BufferedReader and read a single line. If the last character is a '\r' then your system system uses "\n" but you want to set it to "\r\n". Else if line.contains("\n") returns true then you are on a system that uses "\r\n" and you want to set it to "\n". Otherwise the system and the file you are reading have compatible line feed characters.
Just note if you do change the system line feed character be sure to set it back after processing the file in case your program is processing multiple files.

"Element locator with prefix '| id' is not supported" error in Robot framework

I am using robot framework to test a GUI application ,
when I try to run the test case , got an error like
"Element locator with prefix '| id' is not supported " .
But I am using the latest version of selenium2library i.e.2.39.0 .
I will be thankful ,If somebody helps me out regarding the same .
and I have one more query ,i.e. how to click on the contents on GUI when working with robot framework
Thanks in advance
I think the only way you can get such an error message is if you mix two styles of cell separators in your test. For example, you may be mixing tabs and pipes, or multiple spaces and pipes.
Robot determines which format to use on a line-by line basis. First, it looks for a tab anywhere in the line being parsed, and if it finds it, it uses tabs to split the line. If it doesn't find a tab, it checks to see if the line begins with a pipe and space. If so, it uses the pipe for a separator. Failing that, it uses multiple spaces as the separator.
I can reproduce the exact error you are getting by mixing pipes with either a tab or multiple spaces. For example, the following will generate the exact same error you report:
# the next line begins with two spaces
click element | id=treeview_tv_active
Robot will detect the two leading spaces and decide to use spaces to split the line into cells. Thus, the first cell will be "click element" and the second cell will be "| id=treeview_tv_active". Selenium looks for everything before the "=" as the locator type, thus it's using "| id" as the locator, which is invalid and results in the error that you see.
Since you haven't shown us your code it's impossible to say for sure, but my guess is that the line causing the problem begins with a space or tab, or has a tab embedded somewhere else in the line, but later in the same line attempts to use pipes as cell separators.

File contains two EOF characters; what happens?

Will this screw up file size estimation on the file system? Will the filesystem overwrite everything past the first EOF character? How is this handled?
In Unix there is no EOF character. It's simply a concept, a value returned by getc to signal "this is the end (beautiful friend)". EOF is chosen so that getc (and friends) can't return it in any other case.
And about writing past the end of file, different filesystems do things differently.
Some will leave holes that don't actually occupy any space on the disk
Some will fill in the blanks with blanks (0)

How to check for and remove a newline (\n) in the first line of a text field in actionscript?

In the script, sometimes a newline is added in the beginning of the text field (am using a textArea in adobe flex 3), and later on that newline might need to be removed (after other text has been added). I was wondering how to check if there is a newline at the beginning of the text field and then how to remove it. Thanks in advance.
How about
private function lTrimTextArea(ta:TextArea) {
ta.text = ta.text.replace(/^\n*/,'');
}
To remove all line breaks from the start of a string, regardless of whether they are Windows (CRLF) or UNIX (LF only) line breaks, use:
ta.text = ta.text.replace(/^[\r\n]+/,'');
You should use + in the regex instead of * so that the regex only makes a replacement if there is actually a line break at the start of the string. If you use ^\n* as Robusto suggested the regex will find a zero-length match at the start of the string if the string does not start with a line break, and replace that with nothing. Replacing nothing with nothing is a waste of CPU cycles. It may not matter in this situation, but avoiding unintended zero-length matches is a very good habit when working with regular expressions. In other situations they will bite you.
If you particularly want to check first character here is solution:
if(ta.text.charAt(0) == "\n" || ta.text.charAt(0) == "\r")
{
ta.text.slice(1,ta.text.length-1);
}
slice method will slice that first character and gives your text from second character.
If you want to simply disable the ability to create a carriage return / new line, then all you need to do is disable multiline for that TextField...
(exampleTextField as TextField).multiline = false;
This will still trigger KEY_DOWN and KEY_UP events, however will not append the text with the carriage return.

Resources