[SOLVED] QString and file paths
-
Hello everyone,
I'm trying to figure out a way of checking if a specified QString is a valid path. That means a path that can be created and not necessarily existing. I already checked the QString class itself, QDir, QFile and found nothing relevant. Only those come to my mind.
I came up with an idea of how to achieve that but maybe you can point me to a better direction.
- Try to create the path using QDir::mkpath() and check if it succeeded. If it didn't go to step #2, otherwise we're finished.
- Go through all characters in the QString object and check if they are valid for a path. That's a bit problematic. How should I check that? Something like below (brain2terminal)?
@QList<int> charsToBeRemoved;
QString path = " some / path *=? /should:/be/"""here";
// First simplify the path.
path = path.simplified();
// Check each character in the path.
for(int i = 0; i < path.length(); i++) {
QChar c = path[i];
int cat = c.category();
// Any chance to make below 'if' statement shorter? And... it's not really tested... any ideas?
if(cat == QChar::Other_Control || cat == QChar::Other_Format || cat == QChar::Other_Surrogate)
// Add this character to charsToBeRemoved list because it cannot be included in a path.
charsToBeRemoved.append(i);
}
// Remove the chars.
while(!charsToBeRemoved.isEmpty())
// Remove them from the end - otherwise indexes will change.
path.remove(charsToBeRemoved.takeLast(), 1);
// Remove any spaces that may be left.
path = path.trimmed();
path = path.simplified(); // Am I insane?
// Check if string is empty (only bad characters in the string).
if(path.isEmpty())
path = "TMP";@Do you have any suggestions how to achieve what I need?
Thank you in advance.
PS: The path will not be input by user - it's created from data read by a barcode scanner and this thingy can throw weird things if it doesn't read the barcode correctly.
-
You should elaborate more about what is a "valid path" for you. The path in your example
@
" some / path *=? /should:/be/"""here"
@is a perfectly legit path under Unix.
@
$ mkdir -p ' some / path =? /should:/be/"""here'
$ echo $?
0
$ find | perl -pe 's/(.)/|$1|/'
|.|
|./ some |
|./ some / path *=? |
|./ some / path *=? /should:|
|./ some / path *=? /should:/be|
|./ some / path *=? /should:/be/"""here|
@ -
I guess you are trying to verify if a path is valid and in the case it is not, which char has caused the path to be wrong. I don't see any simple solution here, since you could have to check each character also against each other (e.g., quotes), not only as it is. Why don't you restrict the set of legal paths your application is going to accept and then check them with a regular expression?
-
peppe: You are correct, my mistake. I'm developing an application on Windows (and it's going to be used only there).
p-himik: fluca1978 explained exactly what I am trying to achieve.
fluca1978: The problem is that I cannot create a set of legal paths. As I stated one part of the path is read by a barcode reader. It is being sent to a database to which I am connecting and retrieving data.
Unless, of course, you meant legal characters and the use of regular expressions to check if they exist in my path? Oh dear, I hate regexps... :P
-
You must have some restrictions, otherwise it does not make sense at all to check for paths! So for instance you will now that each directory must start with a character, followed by three digits, then there could be a space or other three digits....just to make an example. So if you can write down all possible cases, then you cna summarize them up in one (or a few) regexp.
-
But there's no rule for that. The barcode may contain whatever set of characters you can imagine in whatever order possible. Length is also undetermined. If a read error occurs and weird data is being sent to the DB then it's even worse.
Either I'm missing something you are thinking of or I cannot fully explain what's happening.
-
The aim is to classify files. Each set of files belongs to a specific location which is determined by the barcode. If the barcode contains invalid characters I cannot make a directory form them. I then need to strip the barcode out of unwanted characters.
Okay, I think I know what I'll do: if it won't be possible to create the path specified by the barcode I will just move the files to a directory named "_INVALID" or something. For some reason I was just stuck to creating a valid path out of invalid data. That's not really reasonable, is it?
Thank you for your help. Case closed ;-).