Why a directory string loss '/' will return the parent qdir in Qt - qt

It maybe a bug. Please read webclectic answer.
If QFileInfo(filename) loss '/', QDir::absolutePath will return the parent string. Like below code.
QFileInfo file("e:/QtExample/mytest/out/res/res1/");
QFileInfo file_no("e:/QtExample/mytest/out/res/res1");
QDir dirFile = file.absoluteDir();
QDir dirFile_no = file_no.absoluteDir();
QString strDirFile = dirFile.absolutePath(); //"E:/QtExample/mytest/out/res/res1
QString strDirFile_no = dirFile_no.absolutePath(); //"E:/QtExample/mytest/out/res
I found it with my QTreeView. My code will call the slot from QTreeView::clicked signals
connect(ui.m_pView,SIGNAL(clicked(QModelIndex)),this,SLOT(myClicked(QModelIndex)));
the slot will get the QModelIndex, then I use QFileSystemMode::fileInfo get the QFileInfo.
QFileInfo rFileInfo = m_model.fileInfo(index);
QDir absDir = rFileInfoDir.absoluteDir();
But the return of QFileInfo always return "e:/QtExample/mytest/out/res/res", so If I call QFileInfo::absoluteDir get the directory, the directory is the parent of "res1".So I will get wrong entrylst from my hope directory.
Should I add the '/' after the absoluteFilePath() to get the right QDir?
And why strDirPath equal "E:/QtExample/mytest/out/res/res1", but rDir will list the "res" directory entrylist?
//rFileInfoDir == E:/QtExample/mytest/out/res/res1
QString strDirPath = rFileInfoDir.absoluteFilePath();
QDir rDir = rFileInfoDir.absoluteDir();

If you go deep into the absoluteDir() function call you will see why this happens. In Windows the fileName function in qfsfileengine_win.cpp is called. In this function there is this code part:
if (file == AbsolutePathName) {
int slash = ret.lastIndexOf(QLatin1Char('/'));
if (slash < 0)
return ret;
else if (ret.at(0) != QLatin1Char('/') && slash == 2)
return ret.left(3); // include the slash
else
return ret.left(slash > 0 ? slash : 1);
}
You can see that it returns the part of the string left of the last separator. I don't know if this is the desired behavior or a bug. Maybe you could issue a Qt bug for this. The documentation is not clear on this matter.

From the QFileInfo::absoluteDir() docs:
Returns the file's absolute path as a QDir object.
In the case of "/foo/bar", the file is "bar" and its directory is /foo. It doesn't matter if bar is a directory or a plain file, as directories are also files. If you want something like "for files, return the parent directory, for directories, return the directory itself", you'll have to write your own little function testing via QFileInfo::isDir for what to return.

e:/QtExample/mytest/out/res/res1/res is the path to a directory called "res", e:/QtExample/mytest/out/res/res1/ is the path to an empty directory (called ""). So whether you should add a slash or not depends on what you are trying to do.
In my own personal opinion it's better not to add a final slash because it's easy to add it if you need to, while removing it is not as straightforward.

Related

Workaround for case-sensitive input to dir

I am using Octave 5.1.0 on Windows 10 (x64). I am parsing a series of directories looking for an Excel spreadsheet in each directory with "logbook" in its filename. The problem is these files are created by hand and the filenaming isn't consistent: sometimes it's "LogBook", other times it's "logbook", etc...
It looks like the string passed as input to the dir function is case-sensitive so if I don't have the correct case, dir returns an empty struct. Currently, I am using the following workaround, but I wondered if there was a better way of doing this (for a start I haven't captured all possible upper/lower case combinations):
logbook = dir('*LogBook.xls*');
if isempty(logbook)
logbook = dir('*logbook.xls*');
if isempty(logbook)
logbook = dir('*Logbook.xls*');
if isempty(logbook)
logbook = dir('*logBook.xls*');
if isempty(logbook)
error(['Could not find logbook spreadsheet in ' dir_name '.'])
end
end
end
end
You need to get the list of filenames (either via readdir, dir, ls), and then search for the string in that list. If you use readdir, it can be done like this:
[files, err, msg] = readdir ('.'); # read current directory
if (err != 0)
error ("failed to readdir (error code %d): %s", msg);
endif
logbook_indices = find (cellfun (#any, regexpi (files, 'logbook'));
logbook_filenames = files(logbook_indices);
A much less standard approach could be:
glob ('*[lL][oO][gG][bB][oO][kK]*')

SQLite: isolating the file extension from a path

I need to isolate the file extension from a path in SQLite. I've read the post here (SQLite: How to select part of string?), which gets 99% there.
However, the solution:
select distinct replace(column_name, rtrim(column_name, replace(column_name, '.', '' ) ), '') from table_name;
fails if a file has no extension (i.e. no '.' in the filename), for which it should return an empty string. Is there any way to trap this please?
Note the filename in this context is the bit after the final '\'- it shouldn't be searching for'.'s in the full path, as it does at moment too.
I think it should be possible to do it using further nested rtrims and replaces.
Thanks. Yes, you can do it like this:
1) create a scalar function called "extension" in QtScript in SQLiteStudio
2) The code is as follows:
if ( arguments[0].substring(arguments[0].lastIndexOf('\u005C')).lastIndexOf('.') == -1 )
{
return ("");
}
else
{
return arguments[0].substring(arguments[0].lastIndexOf('.'));
}
3) Then, in the SQL query editor you can use
select distinct extension(PATH) from DATA
... to itemise the distinct file extensions from the column called PATH in the table called DATA.
Note that the PATH field must contain a backslash ('\') in this implementation - i.e. it must be a full path.

trouble passing parameters to console application

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;

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();

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