PLearn 0.1
|
00001 // -*- C++ -*- 00002 00003 // ReadAndWriteCommand.cc 00004 // 00005 // Copyright (C) 2002 Pascal Vincent 00006 // 00007 // Redistribution and use in source and binary forms, with or without 00008 // modification, are permitted provided that the following conditions are met: 00009 // 00010 // 1. Redistributions of source code must retain the above copyright 00011 // notice, this list of conditions and the following disclaimer. 00012 // 00013 // 2. Redistributions in binary form must reproduce the above copyright 00014 // notice, this list of conditions and the following disclaimer in the 00015 // documentation and/or other materials provided with the distribution. 00016 // 00017 // 3. The name of the authors may not be used to endorse or promote 00018 // products derived from this software without specific prior written 00019 // permission. 00020 // 00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00024 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00025 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00026 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00027 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00028 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00029 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00030 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00031 // 00032 // This file is part of the PLearn library. For more information on the PLearn 00033 // library, go to the PLearn Web site at www.plearn.org 00034 00035 /* ******************************************************* 00036 * $Id: ReadAndWriteCommand.cc 10189 2009-05-07 16:05:13Z nouiz $ 00037 ******************************************************* */ 00038 00040 #include "ReadAndWriteCommand.h" 00041 #include <plearn/base/Object.h> 00042 #include <plearn/base/stringutils.h> 00043 #include <plearn/io/fileutils.h> 00044 #include <plearn/io/PyPLearnScript.h> 00045 #include <plearn/io/openFile.h> 00046 00047 namespace PLearn { 00048 using namespace std; 00049 00051 PLearnCommandRegistry ReadAndWriteCommand::reg_(new ReadAndWriteCommand); 00052 00053 ReadAndWriteCommand::ReadAndWriteCommand(): 00054 PLearnCommand("read_and_write", 00055 00056 "Used to check (debug) the serialization system", 00057 00058 "read_and_write <sourcefile> <destfile> [--update] [--mode={plearn_ascii,plearn_binary}] [modification string] ...\n" 00059 "Reads an Object (in PLearn serialization format) from the <sourcefile> and writes it to the <destfile>\n" 00060 "If the sourcefile ends with a .psave file, then it will not be subjected to macro preprosessing \n" 00061 "Otherwise (ex: .plearn .vmat) it will. \n" 00062 "If their is modification string in format option=value, the modification will be made to the object before saving\n" 00063 "The --update option makes that we generate the file only if we can calculate the modification time of the sourcefile and it is more recent than the destfile." 00064 ) 00065 {} 00066 00068 void ReadAndWriteCommand::run(const vector<string>& args) 00069 { 00070 if(args.size()<2) 00071 PLERROR("read_and_write takes 2 or more arguments: <sourcefile> <destfile> [--update] [--mode={plearn_ascii,plearn_binary}][modification string] ..."); 00072 string source = args[0]; 00073 string dest = args[1]; 00074 00075 string ext = extract_extension(source); 00076 PP<Object> o; 00077 time_t date_src=0; 00078 00079 //read the file 00080 o=smartLoadObject(source, date_src); 00081 00082 uint idx_start=2; 00083 PStream::mode_t mode = PStream::plearn_ascii; 00084 for(;idx_start<args.size();){ 00085 if(args[idx_start]=="--update"){ 00086 PLCHECK(date_src>0); 00087 time_t date_dst=mtime(dest); 00088 if((date_dst>date_src) && (date_src>0)){ 00089 pout << "The file is up to date. We don't regenerate it."<<endl; 00090 return; 00091 } 00092 } else if(args[idx_start]=="--mode=plearn_ascii"){ 00093 mode=PStream::plearn_ascii; 00094 } else if(args[idx_start]=="--mode=plearn_binary"){ 00095 mode=PStream::plearn_binary; 00096 } else 00097 break;//the rest are modification string 00098 idx_start++; 00099 } 00100 00101 //modif the object 00102 string left; 00103 string right; 00104 for(uint i=idx_start; i<args.size();i++){ 00105 split_on_first(args[i], "=", left, right); 00106 o->setOption(left, right); 00107 } 00108 00109 //write the file 00110 PStream out = openFile(dest, mode, "w"); 00111 if(!out) 00112 PLERROR("Could not open file %s for writing",dest.c_str()); 00113 out << *o; 00114 } 00115 00116 } // end of namespace PLearn 00117 00118 00119 /* 00120 Local Variables: 00121 mode:c++ 00122 c-basic-offset:4 00123 c-file-style:"stroustrup" 00124 c-file-offsets:((innamespace . 0)(inline-open . 0)) 00125 indent-tabs-mode:nil 00126 fill-column:79 00127 End: 00128 */ 00129 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :