简体   繁体   English

检测文件序列

[英]Detecting a sequence of files

I need to import file sequences into a Qt program. 我需要将文件序列导入Qt程序。 Import itself is not hard, but I'm pondering what might be the most easiest way to detect if the files form a sequence, eg: 导入本身并不难,但是我正在考虑检测文件是否形成序列的最简单方法,例如:

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. 但是在遍历QUrl / QString时,我一直在努力寻找一种“简单”的解决方案来检测图像编号。 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? 但是在那里吗?

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. 它可能基于sscanf,正则表达式或直接的字符串比较,但是方法必须基本相同。

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. 如果您的应用程序中需要更少的代码行,那么QRegEx是解决方案。

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. 它不是精确的const,因为它与比较字符串一起还提取并保存每个条目的前缀 (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.) (实际上,我正在比较CVS样式的文件版本,在我的情况下,前缀是分支的ID,序列是文件的相邻版本。)

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. 现在,它们都已正确排序,并且已经准备好辅助信息-所有进一步的操作都很简单。 Eg 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. 例如,遍历列表,可以使用简单的规则提取所有序列:与上一个条目相比,前缀是相同/ seq num为+1。

IOW, it might be not much different from what you do. IOW,它可能与您所做的没有太大不同。 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. 我根本不怕在该操作上投入一些内存/ CPU,因为跟在它们后面的文件操作无论如何都是较慢的/更多的内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM