Skip to content

OpenToonz - An open-source full-featured 2D animation creation software

C

#assistants: TFrameId from string

Ivan Mahonin114f8511017b
diff --git a/toonz/sources/common/timage_io/tlevel_io.cpp b/toonz/sources/common/timage_io/tlevel_io.cpp
index 3a6e379f9..0eab2540d 100644
--- a/toonz/sources/common/timage_io/tlevel_io.cpp
+++ b/toonz/sources/common/timage_io/tlevel_io.cpp
@@ -116,17 +116,8 @@ TLevelP TLevelReader::loadInfo() {
TFilePath ln(it->getLevelName());
// cout << "try " << *it << " " << it->getLevelName() << endl;
if (levelName == TFilePath(it->getLevelName())) {
- try {
- level->setFrame(it->getFrame(), TImageP());
- data.push_back(*it);
- } catch (TMalformedFrameException tmfe) {
- // skip frame named incorrectly warning to the user in the message
- // center.
- DVGui::warning(QString::fromStdWString(
- tmfe.getMessage() + L": " +
- QObject::tr("Skipping frame.").toStdWString()));
- continue;
- }
+ level->setFrame(it->getFrame(), TImageP());
+ data.push_back(*it);
}
}
if (!data.empty()) {
diff --git a/toonz/sources/common/tsystem/tfilepath.cpp b/toonz/sources/common/tsystem/tfilepath.cpp
index 4d99fed3b..f4a5beb4b 100644
--- a/toonz/sources/common/tsystem/tfilepath.cpp
+++ b/toonz/sources/common/tsystem/tfilepath.cpp
@@ -51,8 +51,8 @@ bool isNumbers(std::wstring str, int fromSeg, int toSeg) {
// Let's check if it follows the format ####A (i.e 00001 or 00001a)
int numDigits = 0, numLetters = 0;
for (int pos = fromSeg + 1; pos < toSeg; pos++) {
- if ((str[pos] >= 'A' && str[pos] <= 'Z') ||
- (str[pos] >= 'a' && str[pos] <= 'z')) {
+ if ((str[pos] >= L'A' && str[pos] <= L'Z') ||
+ (str[pos] >= L'a' && str[pos] <= L'z')) {
// Not the right format if we ran into a letter without first finding a
// number
if (!numDigits) return false;
@@ -60,7 +60,7 @@ bool isNumbers(std::wstring str, int fromSeg, int toSeg) {
// We'll keep track of the number of letters we find.
// NOTE: From here on out we should only see letters
numLetters++;
- } else if (str[pos] >= '0' && str[pos] <= '9') {
+ } else if (str[pos] >= L'0' && str[pos] <= L'9') {
// Not the right format if we ran into a number that followed a letter.
// This format is not something we expect currently
if (numLetters) return false; // not right format
@@ -86,8 +86,46 @@ bool checkForSeqNum(QString type) {
else
return false;
}
+
+bool parseFrame(const std::wstring &str, int &frame, QString &letter, int &padding) {
+ if (str.empty())
+ return false;
+
+ int i = 0, number = 0;
+ while(i < (int)str.size() && str[i] >= L'0' && str[i] <= L'9')
+ number = number * 10 + str[i++] - L'0';
+ int digits = i;
+ wchar_t l = str[i] >= L'a' && str[i] <= L'z' ? str[i++]
+ : str[i] >= L'A' && str[i] <= L'Z' ? str[i++]
+ : L'\0';
+ if (number == 0 || i < (int)str.size())
+ return false;
+
+ frame = number;
+ letter = l ? QString(1, QChar(l)) : QString();
+ padding = str[0] == L'0' ? digits : 0;
+ return true;
+}
+
}; // namespace
+
+TFrameId::TFrameId(const std::string &str, char s)
+ : m_frame(EMPTY_FRAME), m_letter(), m_zeroPadding(4), m_startSeqInd(s)
+{
+ if (str.empty()) return;
+ if (!parseFrame(to_wstring(str), m_frame, m_letter, m_zeroPadding))
+ m_frame = NO_FRAME;
+}
+
+TFrameId::TFrameId(const std::wstring &str, char s)
+ : m_frame(EMPTY_FRAME), m_letter(), m_zeroPadding(4), m_startSeqInd(s)
+{
+ if (str.empty()) return;
+ if (!parseFrame(str, m_frame, m_letter, m_zeroPadding))
+ m_frame = NO_FRAME;
+}
+
// TFrameId::operator string() const
std::string TFrameId::expand(FrameFormat format) const {
if (m_frame == EMPTY_FRAME)
@@ -713,8 +751,8 @@ TFilePath TFilePath::getParentDir() const // noSlash!
int i = getLastSlash(m_path); // cerco l'ultimo slash
if (i < 0) {
if (m_path.length() >= 2 &&
- (('a' <= m_path[0] && m_path[0] <= 'z') ||
- ('A' <= m_path[0] && m_path[0] <= 'Z')) &&
+ ((L'a' <= m_path[0] && m_path[0] <= L'z') ||
+ (L'A' <= m_path[0] && m_path[0] <= L'Z')) &&
m_path[1] == ':')
return TFilePath(m_path.substr(0, 2));
else
@@ -770,23 +808,7 @@ TFrameId TFilePath::getFrame() const {
if (!checkForSeqNum(type) || !isNumbers(str, j, i))
return TFrameId(TFrameId::NO_FRAME);
- int k, number = 0, digits = 0;
- for (k = j + 1; k < i && iswdigit(str[k]); k++) {
- digits++;
- number = number * 10 + str[k] - L'0';
- }
- char letter = '\0';
- if (iswalpha(str[k])) letter = str[k++] + ('a' - L'a');
-
- // if (number == 0 || k < i) // || letter!='\0')
- // throw TMalformedFrameException(
- // *this,
- // str + L": " + QObject::tr("Malformed frame name").toStdWString());
- int padding = 0;
-
- if (str[j + 1] == '0') padding = digits;
-
- return TFrameId(number, letter, padding, str[j]);
+ return TFrameId(str.substr(j+1, i-j-1), str[j]);
}
//-----------------------------------------------------------------------------
@@ -1166,4 +1188,4 @@ TFilePath::TFilePathInfo TFilePath::analyzePath() const {
info.fId = TFrameId(TFrameId::NO_FRAME, 0, 0); // initialize with NO_PAD
info.extension = QString();
return info;
-}
\ No newline at end of file
+}
diff --git a/toonz/sources/include/tfilepath.h b/toonz/sources/include/tfilepath.h
index 458186322..b31e36b31 100644
--- a/toonz/sources/include/tfilepath.h
+++ b/toonz/sources/include/tfilepath.h
@@ -57,6 +57,8 @@ public:
}
TFrameId(int f, QString str, int p = 4, char s = '.')
: m_frame(f), m_letter(str), m_zeroPadding(p), m_startSeqInd(s) {}
+ explicit TFrameId(const std::string &str, char s = '.');
+ explicit TFrameId(const std::wstring &wstr, char s = '.');
inline bool operator==(const TFrameId &f) const {
return f.m_frame == m_frame && f.m_letter == m_letter;
@@ -318,18 +320,6 @@ type is a string that indicate the filename extension(ex:. bmp or .bmp)*/
//-----------------------------------------------------------------------------
-class TMalformedFrameException final : public TException {
-public:
- TMalformedFrameException(const TFilePath &fp,
- const std::wstring &msg = std::wstring())
- : TException(fp.getWideName() + L":" + msg) {}
-
-private:
- TMalformedFrameException();
-};
-
-//-----------------------------------------------------------------------------
-
DVAPI std::ostream &operator<<(std::ostream &out, const TFilePath &path);
typedef std::list<TFilePath> TFilePathSet;
diff --git a/toonz/sources/toonz/filebrowser.cpp b/toonz/sources/toonz/filebrowser.cpp
index f8074e162..62491cc98 100644
--- a/toonz/sources/toonz/filebrowser.cpp
+++ b/toonz/sources/toonz/filebrowser.cpp
@@ -601,17 +601,7 @@ void FileBrowser::refreshCurrentFolderItems() {
for (it = all_files.begin(); it != all_files.end(); it++) {
TFrameId tFrameId;
- try {
- tFrameId = it->getFrame();
- } catch (TMalformedFrameException tmfe) {
- // Incorrect frame name sequence. Warning to the user in the message
- // center.
- DVGui::warning(QString::fromStdWString(
- tmfe.getMessage() + L": " +
- QObject::tr("Skipping frame.").toStdWString()));
- continue;
- }
-
+ tFrameId = it->getFrame();
TFilePath levelName(it->getLevelName());
if (levelName.isLevelName()) {
diff --git a/toonz/sources/toonzfarm/tnzcore_stuff/tfilepath.cpp b/toonz/sources/toonzfarm/tnzcore_stuff/tfilepath.cpp
index 237c89926..1b36f99be 100644
--- a/toonz/sources/toonzfarm/tnzcore_stuff/tfilepath.cpp
+++ b/toonz/sources/toonzfarm/tnzcore_stuff/tfilepath.cpp
@@ -14,6 +14,40 @@ const char auxslash = '\\';
//=============================================================================
+TFrameId::TFrameId(const std::string &str, char s)
+ : m_frame(EMPTY_FRAME), m_letter(), m_zeroPadding(4), m_startSeqInd(s)
+{
+ if (!str.empty()) {
+ int i;
+ m_frame = 0;
+ for(i = 0; i < (int)str.size() && isdigit(str[i]); ++i)
+ m_frame = m_frame*10 + str[i] - '0';
+ if (i < (int)str.size() && isalpha(str[i]))
+ m_letter = str[i++];
+ if (m_frame == 0 || i < (int)str.size())
+ m_frame = NO_FRAME;
+ }
+}
+
+//-------------------------------------------------------------------
+
+TFrameId::TFrameId(const std::wstring &str, char s)
+ : m_frame(EMPTY_FRAME), m_letter(), m_zeroPadding(4), m_startSeqInd(s)
+{
+ if (!str.empty()) {
+ int i;
+ m_frame = 0;
+ for(i = 0; i < (int)str.size() && iswdigit(str[i]); ++i)
+ m_frame = m_frame*10 + str[i] - L'0';
+ if (i < (int)str.size() && iswalpha(str[i]))
+ m_letter = str[i++] + ('a' - L'a');
+ if (m_frame == 0 || i < (int)str.size())
+ m_frame = NO_FRAME;
+ }
+}
+
+//-------------------------------------------------------------------
+
// TFrameId::operator string() const
std::string TFrameId::expand(FrameFormat format) const {
if (m_frame == EMPTY_FRAME)
@@ -348,7 +382,7 @@ TFrameId TFilePath::getFrame() const {
number = number * 10 + str[k] - '0';
char letter = '\0';
if (isalpha(str[k])) letter = str[k++];
- if (number == 0 || k < i) throw TMalformedFrameException();
+ if (number == 0 || k < i) return TFrameId(TFrameId::NO_FRAME);
return TFrameId(number, letter);
}