|
Katana Plug-in APIs 0.1
|
00001 #pragma once 00002 00003 #include <cstddef> 00004 #include <iostream> 00005 #include <sstream> 00006 #include <string> 00007 00008 #include <boost/regex.hpp> 00009 00010 struct FilenameMatch 00011 { 00012 explicit FilenameMatch(bool isValid) : isValid(isValid) {} 00013 FilenameMatch(bool isValid, 00014 const std::string prefix, 00015 const std::string number, 00016 const std::string suffix) 00017 : isValid(isValid), 00018 prefix(std::move(prefix)), 00019 number(std::move(number)), 00020 suffix(std::move(suffix)) 00021 { 00022 } 00023 bool operator==(const FilenameMatch& rhs) const 00024 { 00025 return isValid == rhs.isValid && prefix == rhs.prefix && 00026 number == rhs.number && suffix == rhs.suffix; 00027 } 00028 bool operator!=(const FilenameMatch& rhs) const { return !(*this == rhs); } 00029 std::string str() const 00030 { 00031 if (!isValid) 00032 { 00033 return "Not a match"; 00034 } 00035 std::stringstream ss; 00036 ss << prefix << '\n' << number << '\n' << suffix; 00037 return ss.str(); 00038 } 00039 00040 bool isValid; 00041 std::string prefix; 00042 std::string number; 00043 std::string suffix; 00044 }; 00045 00046 struct SequenceMatch 00047 { 00048 explicit SequenceMatch(bool isValid) : isValid(isValid) {} 00049 SequenceMatch(bool isValid, 00050 std::string prefix, 00051 std::string range, 00052 std::string firstFrame, 00053 std::string lastFrame, 00054 std::string printfSyntaxPadding, 00055 std::string format, 00056 std::string suffix) 00057 : isValid(std::move(isValid)), 00058 prefix(std::move(prefix)), 00059 range(std::move(range)), 00060 firstFrame(std::move(firstFrame)), 00061 lastFrame(std::move(lastFrame)), 00062 printfSyntaxPadding(std::move(printfSyntaxPadding)), 00063 format(std::move(format)), 00064 suffix(std::move(suffix)) 00065 { 00066 } 00067 00068 size_t getPadding() const 00069 { 00070 size_t result(format.length()); 00071 if (format.empty()) 00072 { 00073 result = std::stoi(printfSyntaxPadding); 00074 } 00075 return result; 00076 } 00077 bool operator==(const SequenceMatch& rhs) const 00078 { 00079 return isValid == rhs.isValid && prefix == rhs.prefix && 00080 range == rhs.range && firstFrame == rhs.firstFrame && 00081 lastFrame == rhs.lastFrame && 00082 printfSyntaxPadding == rhs.printfSyntaxPadding && 00083 format == rhs.format && suffix == rhs.suffix; 00084 } 00085 friend std::ostream& operator<<(std::ostream& os, 00086 const SequenceMatch& match); 00087 00088 bool isValid; 00089 std::string prefix; 00090 std::string range; 00091 std::string firstFrame; 00092 std::string lastFrame; 00093 std::string printfSyntaxPadding; 00094 std::string format; 00095 std::string suffix; 00096 }; 00097 00098 inline std::ostream& operator<<(std::ostream& os, const SequenceMatch& match) 00099 { 00100 if (!match.isValid) 00101 { 00102 os << "Not a sequence"; 00103 return os; 00104 } 00105 os << " prefix: " << match.prefix << " range: " << match.range 00106 << " firstFrame: " << match.firstFrame 00107 << " lastFrame: " << match.lastFrame 00108 << " printf Style padding:" << match.printfSyntaxPadding 00109 << " hash style paddding: " << match.format 00110 << " suffix: " << match.suffix; 00111 return os; 00112 } 00113 00114 inline FilenameMatch makeFilenameMatch(const std::string& s) 00115 { 00116 boost::regex expr{R"((.*[-\._]+)(\d+)(\..*))"}; 00117 boost::smatch matches; 00118 if (!boost::regex_match(s, matches, expr)) 00119 { 00120 return FilenameMatch(false); 00121 } 00122 return FilenameMatch(true, matches[1], matches[2], matches[3]); 00123 } 00124 00125 inline SequenceMatch makeFileSequenceMatch(const std::string& s) 00126 { 00127 boost::regex expr{ 00128 R"((.*[-\._]+)(\((\d+)-(\d+)\))?((%(\d+)d)|(#{1,10}))(\..*))"}; 00129 boost::smatch matches; 00130 if (!boost::regex_match(s, matches, expr)) 00131 { 00132 return SequenceMatch(false); 00133 } 00134 return SequenceMatch(true, matches[1], matches[2], matches[3], matches[4], 00135 matches[7], matches[8], matches[9]); 00136 }
1.7.3