Field3D
FileSequence.cpp
Go to the documentation of this file.
1 //----------------------------------------------------------------------------//
2 
3 /*
4  * Copyright (c) 2014 Sony Pictures Imageworks Inc
5  *
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the
17  * distribution. Neither the name of Sony Pictures Imageworks nor the
18  * names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior written
20  * permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
29  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
33  * OF THE POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 //----------------------------------------------------------------------------//
37 
42 //----------------------------------------------------------------------------//
43 
44 // Header include
45 #include "FileSequence.h"
46 
47 // System includes
48 #include <cstdlib>
49 #include <iostream>
50 
51 // Library includes
52 #include "Log.h"
53 #include "Field3DFile.h"
54 
55 //----------------------------------------------------------------------------//
56 
58 
59 //----------------------------------------------------------------------------//
60 // FileSequence implementations
61 //----------------------------------------------------------------------------//
62 
63 
64 FileSequence::FileSequence(const std::string &sequence)
65 {
66  // Example sequences: myfile.1-2@.f3d, myfile1-21#.f3d
67  // The number '1' in each of these is the 'sequence start'
68  // The numbers '2' and '21' are the 'sequence end'
69 
70  const std::string k_numbers = "0123456789";
71  const std::string k_seqMarks = "#@";
72  const std::string k_framePlaceholder = "####";
73  const size_t npos = std::string::npos;
74 
75  // Check for file by that name. If it exists, we are just that one file
76  if (fileExists(sequence)) {
77  m_filenames.push_back(sequence);
78  return;
79  }
80 
81  // Find the sequence mark
82  const size_t seqMarkIdx = sequence.find_first_of(k_seqMarks);
83 
84  // If no sequence mark was found, there is no sequence.
85  if (seqMarkIdx == npos) {
86  return;
87  }
88 
89  // Make sure there is not more than one sequence mark
90  if (sequence.find_first_of(k_seqMarks, seqMarkIdx + 1) != npos) {
91  std::stringstream warning;
92  warning << "Multiple sequence marks in filename: " << sequence;
93  Msg::print(Msg::SevWarning, warning.str());
94  return;
95  }
96 
97  // Get the end range index
98  size_t seqEndIdx = sequence.find_last_not_of(k_numbers, seqMarkIdx - 1);
99  if (seqEndIdx == npos) {
100  std::stringstream warning;
101  warning << "Sequence mark but no sequence range in filename: "
102  << sequence;
103  Msg::print(Msg::SevWarning, warning.str());
104  return;
105  } else {
106  seqEndIdx += 1;
107  }
108  if (seqEndIdx == 0) {
109  std::stringstream warning;
110  warning << "Sequence mark preceded by single number: "
111  << sequence;
112  Msg::print(Msg::SevWarning, warning.str());
113  return;
114  }
115 
116  // Make sure the preceding character is '-'
117  if (sequence[seqEndIdx - 1] != '-') {
118  std::stringstream warning;
119  warning << "Sequence mark preceded by single number but no '-': "
120  << sequence;
121  Msg::print(Msg::SevWarning, warning.str());
122  return;
123  }
124 
125  // Get the start range index
126  size_t seqStartIdx = sequence.find_last_not_of(k_numbers, seqEndIdx - 2);
127  if (seqStartIdx == npos) {
128  std::stringstream warning;
129  warning << "No sequence start in filename: "
130  << sequence;
131  Msg::print(Msg::SevWarning, warning.str());
132  return;
133  } else {
134  seqStartIdx += 1;
135  }
136 
137  // String versions of frame numbers
138  const std::string startStr = sequence.substr(seqStartIdx, seqEndIdx - 1);
139  const std::string endStr = sequence.substr(seqEndIdx, seqMarkIdx);
140 
141  // Get the integers
142  const int start = atoi(startStr.c_str());
143  const int end = atoi(endStr.c_str());
144 
145  // Create the file basename for replacement
146  const std::string baseStart = sequence.substr(0, seqStartIdx);
147  const std::string baseEnd = sequence.substr(seqMarkIdx + 1);
148 
149  // Create the filenames
150  for (int i = start; i <= end; ++i) {
151  std::stringstream filename;
152  filename << baseStart << i << baseEnd;
153  m_filenames.push_back(filename.str());
154  }
155 }
156 
157 //----------------------------------------------------------------------------//
158 
160 
161 //----------------------------------------------------------------------------//
const std::string & filename(const size_t idx) const
Returns a single filename.
Definition: FileSequence.h:89
Contains the Field3DFile classesOSS sanitized.
#define FIELD3D_NAMESPACE_SOURCE_CLOSE
Definition: ns.h:60
std::vector< std::string > m_filenames
Stores the resulting filenames.
Definition: FileSequence.h:101
FIELD3D_API void print(Severity severity, const std::string &message)
Sends the string to the assigned output, prefixing the message with the severity. ...
Definition: Log.cpp:66
Contains the Log class which can be used to redirect output to an arbitrary destination.
bool fileExists(const std::string &filename)
checks to see if a file/directory exists or not
FileSequence()
Default constructor. Creates an empty sequence.
Definition: FileSequence.h:75
Contains the FileSequence class which can be used to turn strings such as "file.1-20#.f3d" into a vector of strings.