contrib/oxl/oxp/SequenceFileName.cxx

Go to the documentation of this file.
00001 // This is oxl/oxp/SequenceFileName.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 
00006 #include "SequenceFileName.h"
00007 
00008 #include <vcl_cstddef.h> // for vcl_size_t()
00009 #include <vcl_iostream.h>
00010 #include <vul/vul_reg_exp.h>
00011 #include <vul/vul_file.h>
00012 #include <vul/vul_sprintf.h>
00013 
00014 #include <oxp/oxp_parse_seqname.h>
00015 
00016 SequenceFileName::SequenceFileName(char const* s, char const* read_or_write)
00017 {
00018   init(s, 0, 1, read_or_write);
00019 }
00020 
00021 SequenceFileName::SequenceFileName(char const* s, int start_frame, int step, char const* read_or_write)
00022 {
00023   init(s, start_frame, step, read_or_write);
00024 }
00025 
00026 void SequenceFileName::init(char const* s, int start_frame, int step, char const* /*read_or_write*/)
00027 {
00028   // First match any trailing ",n[:n]:[n]" (can use ; or ,)
00029   oxp_parse_seqname range(s);
00030   if (range.start_ != -1) start_frame = range.start_;
00031   if (range.step_ != -1) step = range.step_;
00032   end_ = range.end_;
00033 
00034   if (end_ == -1)
00035     end_ = 9999999;
00036 
00037   fmt_ = range.filename_;
00038   ext_ = "";
00039   start_frame_ = start_frame;
00040   step_ = step;
00041   ok_ = false;
00042 
00043   set_end(end_);
00044 
00045   if (fmt_.find('%') == vcl_string::npos) {
00046     // No %, add one
00047     fmt_ += ".%03d";
00048   }
00049 
00050   {
00051     // Make any subdirectory if not an input_file
00052     vcl_size_t i = fmt_.rfind('/');
00053     // If no /, assume that the file is in CWD which exists
00054     if (i != vcl_string::npos) {
00055       vcl_string dir = fmt_.substr(0, i);
00056       if (!vul_file::exists(dir.c_str())) {
00057         vcl_cerr << "SequenceFileName: ** Image directory [" << dir << "] does not exist\n";
00058         return;
00059         // vcl_cerr << "SequenceFileName: ** Making directory " << dir << vcl_endl;
00060         // if (!vul_file::make_directory(dir.c_str())) {
00061         //   vcl_cerr << "SequenceFileName: ** Could not mkdir " << dir << vcl_endl;
00062         //   abort();
00063         // }
00064       } else
00065         if (!vul_file::is_directory(dir.c_str()))
00066           vcl_cerr << "SequenceFileName: WARNING: Inferred subdir [" << dir << ']'
00067                    << " exists and is not already a directory\n";
00068     }
00069   }
00070 
00071   // If no extension, we'll need a default
00072   {
00073     vul_reg_exp re("\\.([a-zA-Z_0-9]+)$");
00074     if (re.find(fmt_.c_str())) {
00075       vcl_cerr << "SequenceFileName: Found extension [" << re.match(1) << "]\n";
00076       int pointpos = re.start(0);
00077       ext_ = fmt_.substr(pointpos);
00078       fmt_.erase(pointpos, vcl_string::npos);
00079     }
00080   }
00081 
00082   ok_ = true;
00083 }
00084 
00085 vcl_string SequenceFileName::name(int frame)
00086 {
00087   return vul_sprintf((fmt_ + ext_).c_str(), frame * step_ + start_frame_);
00088 }
00089 
00090 void SequenceFileName::set_default_extension(char const* extension)
00091 {
00092   if (ext_.length() == 0) {
00093     ext_ = ".";
00094     ext_ += extension;
00095   }
00096 }
00097 
00098 bool SequenceFileName::exists(const vcl_string& fmt, const char* extension, int frame)
00099 {
00100   vul_sprintf buf((fmt + extension).c_str(), frame);
00101   vcl_cerr << "SequenceFileName: Checking [" << buf << "]\n";
00102   return (vul_file::size(buf.c_str()) > 0 || vul_file::size((vcl_string(buf) + ".gz").c_str()) > 0);
00103 }
00104 
00105 vcl_ostream& SequenceFileName::print(vcl_ostream& s) const
00106 {
00107   return s << '[' << fmt_ << ext_ << ' ' << start_frame_ << ':' << step_ << ':' << end_ << ']';
00108 }

Generated on Tue Dec 2 05:14:28 2008 for contrib/oxl/oxp by  doxygen 1.5.1