UltraScan III
us_datafiles.cpp
Go to the documentation of this file.
1 
3 #include "us_datafiles.h"
4 
5 // Get a data file name; either by matching the GUID,
6 // finding a numeric gap in existing file names,
7 // or bumping to the next number past the last used.
8 // path = path to data files directory (e.g., "dataDir()/models")
9 // guid = GUID value to match
10 // lfchar = leading file character ("M", "N", ...)
11 // lkupTag = look-up tag ("model", "noise", ...)
12 // lkupAtt = look-up attribute ("guid", "modelGUID", ...)
13 // newFile = returned new-file flag
14 // (return:) full path name for new or old file to write
15 QString US_DataFiles::get_filename( const QString& path, const QString& guid,
16  const QString& lfchar, const QString& lkupTag, const QString& lkupAtt,
17  bool& newFile )
18 {
19  QDir f( path );
20  QStringList filter( lfchar + "???????.xml" );
21  QStringList f_names = f.entryList( filter, QDir::Files, QDir::Name );
22  f_names.sort();
23  newFile = true; // Assume file will be new
24  QString ofname = ""; // Start with empty output file name
25  int numFile = 1; // Default number if no files exist
26 
27  for ( int ii = 0; ii < f_names.size(); ii++ )
28  { // Browse all the existing files in the directory
29  QString fname = f_names[ ii ];
30 
31  if ( !guid.isEmpty() )
32  { // If guid is not empty, search for a match in existing files
33  QFile m_file( path + "/" + fname );
34 
35  if ( ! m_file.open( QIODevice::ReadOnly | QIODevice::Text) ) continue;
36 
37  QXmlStreamReader xml( &m_file );
38 
39  while ( ! xml.atEnd() )
40  { // Search for a matching tag and attribute
41  xml.readNext();
42 
43  if ( xml.isStartElement() )
44  {
45  if ( xml.name() == lkupTag )
46  { // Found the look-up tag
47  QXmlStreamAttributes a = xml.attributes();
48 
49  if ( a.value( lkupAtt ).toString() == guid )
50  { // There is a match of an attribute value to the GUID
51  ofname = fname; // File name will be this one
52  newFile = false; // Not a new file
53  break; // We're done looking
54  }
55  }
56  }
57  }
58 
59  m_file.close();
60  }
61 
62  if ( newFile )
63  { // No match yet found: look for a gap in numbering
64  if ( ofname.isEmpty() )
65  { // No gap was previously found
66  int numCurr = fname.mid( 1, 7 ).toInt(); // Current file numeric
67  numFile = ii + 1; // Expected file numeric
68 
69  if ( numCurr > numFile ) // There is a gap: use missing number name
70  ofname = lfchar + QString().sprintf( "%07i", numFile ) + ".xml";
71  }
72  }
73 
74  else // A match was found in XML, so break from the file loop
75  break;
76 
77  }
78 
79  if ( ofname.isEmpty() )
80  { // No match and no numeric gap, so bump to number past last existing file
81  if ( f_names.size() > 0 )
82  numFile = f_names.last().mid( 1, 7 ).toInt() + 1;
83  ofname = lfchar + QString().sprintf( "%07i", numFile ) + ".xml";
84  }
85 
86  return path + "/" + ofname;
87 }
88 
89 // Get a data file name. This version has no newFile return argument.
90 // path = path to data files directory (e.g., "dataDir()/models")
91 // guid = GUID value to match
92 // lfchar = leading file character ("M", "N", ...)
93 // lkupTag = look-up tag ("model", "noise", ...)
94 // lkupAtt = look-up attribute ("guid", "modelGUID", ...)
95 // (return:) full path name for new or old file to write
96 QString US_DataFiles::get_filename( const QString& path, const QString& guid,
97  const QString& lfchar, const QString& lkupTag, const QString& lkupAtt )
98 {
99  bool newf = false;
100  return get_filename( path, guid, lfchar, lkupTag, lkupAtt, newf );
101 }
102