trouble passing parameters to console application - qt

I have trouble with correctly launching cloc 1.62 from windows command line using qprocess. Here is what i have:
A QStringList with all the languages cloc recognizes;
QStringList languages;
languages<<"\"ABAP\""<<"\"ActionScript\""<<"\"Ada\""<<"\"ADSO/IDSM\""<<"\"AMPLE\""<<"\"Ant\""<<"\"Apex Trigger\"";
Then i create a qstring consisting of all of the list's elements separated by comma, exept for one, which is stored in a lang variable;
QString langsinuse;
for (int i=0;i<languages.length();i++)
{
if (languages.at(i) != lang)
{
if (langsinuse.isEmpty())
{
langsinuse=langsinuse+languages.at(i);
}
else
{
langsinuse=langsinuse+','+languages.at(i);
}
}
}
then i build an arguments stringlist and launch the process:
QStringList arguments;
QProcess cloc;
QString params="cloc-1.62.exe --xml --by-file --report-file="+
list1.at(1).trimmed()+'/'+name+'/'+"report.xml"+" --exclude-lang="+langsinuse+" "+distr;
arguments<<"/k"<<params;
cloc.startDetached("cmd.exe",arguments,"cloc/");
But somehow the spaces in the languages names are not considered escaped and every word separated by space considered a different parameter for cloc, even though both words are in double quotes (for example "\"Apex Trigger\"") and clock produces a bunch of errors.
(50 errors:
Unable to read: Trigger","Arduino
Unable to read: Sketch","ASP","ASP.Net","Assembly","AutoHotkey","awk","Bourne)
It's the error that happens when you don't put a language's name that contains spaces in double quotes, int the --exclude-lang= option (for example --exclude-lang=Apex Trigger will cause an error, --exclude-lang="Apex Trigger" won't)
However if i just save the whole command i build in qt and save it in some batch file it runs just fine.
Am i missing something in escaping the double quotes correctly?

Ok i just had to pass arguments separately and not as a single string.
arguments<<"/k"<<"cloc-1.62.exe" <<"--xml"<<"--by-file"<<"--report-file="+
list1.at(1).trimmed()+'/'+name+'/'+"report.xml"<<"--exclude-lang="+langsinuse<<distr;

Related

How to remove double qoutes in Objective-C

Let me introduce myself.
My name is Vladimir, C++ programmer, I am from Serbia. two weeks ago I have started to learn objective-C and it was fine until tonight.
Problem:
I cant remove double quotes from my NSLog output.
NSLog(#"The best singers:%#", list.best);
Strings are joined with componentsJoinedByString:#" and "
I would like to get something like this:
The best singers: Mickey and John.
But I get this:
The best singers: ("Mickey", and "John").
I cant remove comma (,) and parentheses either.
I have tried with "replaceOccurencesOfString" but with no success. It can remove any character except qoute and comma.
Also I have used -(NSString *)description method to return string.
You are getting the raw output from your list (which I assume is an array). You will have to do your own formatting to get this to display in the format that you want. You can achieve this by building your string by iterating through your array. Note that this probably isn't the most efficient nor the most robust way to achieve this.
NSMutableString *finalString = [NSMutableString string];
BOOL first = YES;
for (NSString *nameString in list) {
if (first) {
[finalString appendString:nameString];
first = NO;
} else {
[finalString appendString:[NSString stringWithFormat:#" and %#", nameString]];
}
}

Escape Spaces in QT

I want to use the QString.split(' ') method to split a input command into the command
QStringList commandList = command.split(' ');
however command has a UNIX path at the end of it. i.e. it looks something like
QString command = new QString("caommand -a -b /path/to\ specific/file");
the path command is specified by the user at runtime(the user escapes any spaces in the path). For some reason command.split(' '); does not escape the spaces.
I am new to QT, how does it escape spaces?
Thanks for any help
You can use QDir::toNativeSeparators() to convert it to unix style. And split received result by spaces, though you have got to figure out where are the spaces between commands and where are the possible spaces in filename
For example:
QString myUnixPath = QDir::toNativeSeparators("/home/path with spaces/");
will return unix style path, while
QString qtPath = QDir::fromNativeSeparators("/path/with\ spaces/");
will return /path with spaces/

QRegExp: individual quantifiers can't be non-greedy, but what good alternatives then?

I'm trying to write code that appends ending _my_ending to the filename, and does not change file extension.
Examples of what I need to get:
"test.bmp" -> "test_my_ending.bmp"
"test.foo.bar.bmp" -> "test.foo.bar_my_ending.bmp"
"test" -> "test_my_ending"
I have some experience in PCRE, and that's trivial task using it. Because of the lack of experience in Qt, initially I wrote the following code:
QString new_string = old_string.replace(
QRegExp("^(.+?)(\\.[^.]+)?$"),
"\\1_my_ending\\2"
);
This code does not work (no match at all), and then I found in the docs that
Non-greedy matching cannot be applied to individual quantifiers, but can be applied to all the quantifiers in the pattern
As you see, in my regexp I tried to reduce greediness of the first quantifier + by adding ? after it. This isn't supported in QRegExp.
This is really disappointing for me, and so, I have to write the following ugly but working code:
//-- write regexp that matches only filenames with extension
QRegExp r = QRegExp("^(.+)(\\.[^.]+)$");
r.setMinimal(true);
QString new_string;
if (old_string.contains(r)){
//-- filename contains extension, so, insert ending just before it
new_string = old_string.replace(r, "\\1_my_ending\\2");
} else {
//-- filename does not contain extension, so, just append ending
new_string = old_string + time_add;
}
But is there some better solution? I like Qt, but some things that I see in it seem to be discouraging.
How about using QFileInfo? This is shorter than your 'ugly' code:
QFileInfo fi(old_string);
QString new_string = fi.completeBaseName() + "_my_ending"
+ (fi.suffix().isEmpty() ? "" : ".") + fi.suffix();

removing whitespaces from a QRegExpValidator

I have a code someone wrote and there
this->llBankCode = new widgetLineEditWithLabel(tr("Bankleitzahl"), "", Qt::AlignTop, this);
QRegExpValidator *validatorBLZ = new QRegExpValidator(this);
validatorBLZ->setRegExp(QRegExp( "[0-9]*", Qt::CaseSensitive));
this->llBankCode->lineEdit->setValidator(validatorBLZ);
as it can be seen from this code, is that validatorBLZ can accept only numbers between 0 and 9. I would like to change it, that validatorBLZ would be able to get as an input whitespace as well (but not to start with a whitespace), but it wont be shown.
Example:
if i try to copy & paste a string of the format '22 34 44', the result would be an empty field. What i would like to happen is that the string '22 34 44' will be shown in the field as '223444'.
How could i do it?
You could try using:
QString string = "22 34 44";
string.replace(QString(" "), QString(""));
That will replace any spaces with a non-space.
Write your own QValidator subclass and reimplement validate and fixup. Fixup does what you ask for: changes the input in a way that makes it intermediate/acceptable.
In your case, consider the following code-snippet for fixup:
fixup (QString &input) const
{
QString fixed;
fixed.reserve(input.size());
for (int i=0; i<input.size(); ++i)
if (input.at(i).isDigit()) fixed.append(input.at(i));
input = fixed;
}
(this is not tested)
The validate function will obviously look similar, returning QValidator::Invalid when it encounters a non-digit character and returning the according position in pos.
If your BLZ is limited to Germany, you could easily add the validation feature that it only returns QValidator::Acceptable when there are eight digits, and QValidator::Intermediate else.
Anyhow, writing an own QValidator, which often is very easy and straight forward, is the best (and most future-proof) solution most of the time. RegExes are great, but C++ clearly is the more powerful language here, which in addition results in a much more readable validator ;).

How do I find out which suffix user has chosen when using a QFileDialog?

Well I'm using the following code to get the filename for a file that needs to be stored ..
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),"/home/user/MyDocs/",tr("JPG files (*.jpg);;BMP files (*.bmp);;PNG files (*.png)"));
I'm providing the user with a number of options regarding the file format in which the file is to be saved. However, the returned QString only gives me the prefix filename the user have chosen, not the suffix and thus I don't know which file format the user chose. How can I detect such a file format?
The code in the question works in Windows (Qt 4.6.2 and Win XP). fileName contains the selected extension. But you are obviously using something else Windows, so you could try this workaround:
QFileDialog dialog(this, tr("Save as ..."), "/home/user/MyDocs/");
dialog.setAcceptMode(QFileDialog::AcceptSave);
QStringList filters;
filters << "JPG files (*.jpg)" << "BMP files (*.bmp)" << "PNG files (*.png)";
dialog.setNameFilters(filters);
if (dialog.exec() == QDialog::Accepted)
{
QString selectedFilter = dialog.selectedNameFilter();
QString fileName = dialog.selectedFiles()[0];
}
That is a slighty modified code from here.
You need to use the 5th optional string
I usually do it like this:
#define JPEG_FILES "JPG files (*.jpg)"
#define BMP_FILES "BMP files (*.bmp)"
#define PNG_FILES "PNG files (*.png)"
QString selectedFilter;
QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
"/home/user/MyDocs/",
JPEG_FILES ";;" BMP_FILES ";;" PNG_FILES, &selectedFilter);
if (fileName.isNull())
return;
if (selectedFilter == JPEG_FILES) {
...
} else if (selectedFilter == BMP_FILES) {
...
} else if (selectedFilter == PNG_FILES) {
...
} else {
// something strange happened
}
The compiler takes care to concatenate the literal strings in the argument.
I'm not sure how the returned string interacts with tr(). You'll have to test and find out. probably need to un-translate it.
It could have been nicer if the function would return the index of the selected filter but alas, it does not.
A nicer solution would be to put the filters in a list, create a string from it and then compare to the returned selected filter string to the ones in the list. This would also solve the tr() problem.
Have a look to this discussion. It uses QFileInfo on the string that was entered in a QFileDialog.

Resources