开发者

Detecting a sequence of files

开发者 https://www.devze.com 2023-01-08 12:45 出处:网络
I need to import file sequences into a Qt program. Import itself is not hard, but I\'m pondering what might be the most easiest way to detect if the files form a sequence, e.g.:

I need to import file sequences into a Qt program. Import itself is not hard, but I'm pondering what might be the most easiest way to detect if the files form a sequence, e.g.:

filename_00001.png
filename_00002.png
filename_00003.png
etc.

Now, I have a drag'n'drop import working, and I'm sorting the list of imported files alphabetically. But while iterating through QUrl/QString I'm struggling to find an 'easy' solution to detect image numbering. I could go through iterating the file names, find the first number in a string, form a base string, counter + possible padding and end strings and try to match these. This, however, sounds a little too messy and makes me think there must be a better w开发者_开发知识库ay. But is there?


My intuition would tell me that any solution is essentially going to involve something like what you've just described. It might be based on sscanf, regexes, or direct string comparisons, but the approach must fundamentally be the same.

May I ask what the goal is?


What you describe most likely is the easiest solution.

If you want less lines of code in your app, then QRegEx is the solution.


I could go through iterating the file names, find the first number in a string, form a base string, counter + possible padding and end strings and try to match these. This, however, sounds a little too messy and makes me think there must be a better way. But is there?

I wanted to have simple solution, thus I have sacrificed memory and use several lists/maps to sort/group names. I packed strings into structs with extra fields for auxiliary information.

I'm using a special compare function. It isn't precisely const, since along with the comparing strings it also extracts and saves a prefix for every entry. (Actually I am comparing CVS-style file version and the prefix in my case is an id of a branch, sequences are adjacent versions of a file.)

It works like that. The input file list:

"filename_00001.png"
"filename_00002.png"
"filename_00003.png"

First strip extensions:

"filename_00001", "png"
"filename_00002", "png"
"filename_00003", "png"

Then during sorting extract from file name prefix: the name - all digits on the end. If prefixes are equal, then compare the numbers. (In my case input isn't padded with zeros):

"filename_00001", "png", "filename_"
"filename_00002", "png", "filename_"
"filename_00003", "png", "filename_"

After the sorting/prefix extraction, all sequences have the same prefix. If file doesn't have a number on the end, the whole file name is a prefix and it is by definition unique. You can also additionally save the extracted number:

"filename_00001", "png", "filename_", 1
"filename_00002", "png", "filename_", 2
"filename_00003", "png", "filename_", 3

Now they are all sorted and sorted properly, auxiliary information is out there prepared - all further operations are rather trivial. E.g. iterating over the list, one can extract all sequences with simple rule: prefix is the same/seq num is +1 compared to the prev entry.

IOW, it might be not much different from what you do. I am simply not afraid to throw a bit of memory/CPU on the operation since following them file operations are slower/more memory hungry by magnitude anyway.

0

精彩评论

暂无评论...
验证码 换一张
取 消