Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QString and file paths
QtWS25 Last Chance

[SOLVED] QString and file paths

Scheduled Pinned Locked Moved General and Desktop
10 Posts 5 Posters 10.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    d2uriel
    wrote on last edited by
    #1

    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.

    1. 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.
    2. 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.

    "Do not judge, and you will never be mistaken." - Jean-Jacques Rousseau

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dangelog
      wrote on last edited by
      #2

      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|
      @

      Software Engineer
      KDAB (UK) Ltd., a KDAB Group company

      1 Reply Last reply
      0
      • P Offline
        P Offline
        p-himik
        wrote on last edited by
        #3

        I think that d2uriel means correct path for platform an app was launched on.

        d2uriel, you can try QDir::makeAbsolute(). It returns false if path can't be converted to absolute.

        1 Reply Last reply
        0
        • F Offline
          F Offline
          fluca1978
          wrote on last edited by
          #4

          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?

          1 Reply Last reply
          0
          • D Offline
            D Offline
            d2uriel
            wrote on last edited by
            #5

            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

            "Do not judge, and you will never be mistaken." - Jean-Jacques Rousseau

            1 Reply Last reply
            0
            • F Offline
              F Offline
              fluca1978
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • D Offline
                D Offline
                d2uriel
                wrote on last edited by
                #7

                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.

                "Do not judge, and you will never be mistaken." - Jean-Jacques Rousseau

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  fluca1978
                  wrote on last edited by
                  #8

                  So you don't have any possible check against the path, why bother? Just try to create it (or as suggested to make it absolute) and see if it works. What is your aim?

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    d2uriel
                    wrote on last edited by
                    #9

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

                    "Do not judge, and you will never be mistaken." - Jean-Jacques Rousseau

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      goetz
                      wrote on last edited by
                      #10

                      You can make everything to Platin ASCII by encoding it with, for example, base64. Just make sure to split at the path separators before encoding.

                      http://www.catb.org/~esr/faqs/smart-questions.html

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved