Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

odicop.cc

Go to the documentation of this file.
00001 // -*- C++ -*- 00002 00003 // odicop.cc 00004 // Copyright (C) 2004 Norman Casagrande (casagran@iro.umontreal.ca) 00005 // 00006 // Redistribution and use in source and binary forms, with or without 00007 // modification, are permitted provided that the following conditions are met: 00008 // 00009 // 1. Redistributions of source code must retain the above copyright 00010 // notice, this list of conditions and the following disclaimer. 00011 // 00012 // 2. Redistributions in binary form must reproduce the above copyright 00013 // notice, this list of conditions and the following disclaimer in the 00014 // documentation and/or other materials provided with the distribution. 00015 // 00016 // 3. The name of the authors may not be used to endorse or promote 00017 // products derived from this software without specific prior written 00018 // permission. 00019 // 00020 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 00021 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00022 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN 00023 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 00025 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00026 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00027 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00028 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00029 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 // 00031 // This file is part of the PLearn library. For more information on the PLearn 00032 // library, go to the PLearn Web site at www.plearn.org 00033 00034 00035 /* ******************************************************* 00036 * $Id: odicop.cc,v 1.4 2004/06/10 13:17:23 tihocan Exp $ 00037 ******************************************************* */ 00038 00039 #include <iostream> 00040 #include <sstream> 00041 #include <cstdlib> 00042 #include <dirent.h> 00043 #include <unistd.h> 00044 #include <sys/stat.h> 00045 00046 using namespace std; 00047 00048 bool isdir(const string& path); 00049 bool islink(const string& path); 00050 void makedir(const string& dir); 00051 int goAndCreateDir(string sourcedir, string destdir, string spc); 00052 void copyAndLinkObjs(string& sourceOBJdir, string& original_sourcedir, string& destOBJdir); 00053 00054 int main(int argc, char *argv[]) 00055 { 00056 00057 if (argc < 3) 00058 { 00059 cout << "odicop <sourcedir> <destdir>" << endl; 00060 return 0; 00061 } 00062 00063 string sourcedir = argv[1]; 00064 string destdir = argv[2]; 00065 00066 if (sourcedir[sourcedir.length()-1] != '/') 00067 sourcedir += '/'; 00068 00069 if (destdir[destdir.length()-1] != '/') 00070 destdir += '/'; 00071 00072 goAndCreateDir(sourcedir, destdir, ""); 00073 00074 return EXIT_SUCCESS; 00075 } 00076 00077 00078 // recursive function 00079 int goAndCreateDir(string sourcedir, string destdir, string spc) 00080 { 00081 string newSourceDir; 00082 string newDestDir; 00083 string command; 00084 cout << spc << "Examining dir " << sourcedir << endl; 00085 spc += " "; 00086 00087 DIR* d = opendir(sourcedir.c_str()); 00088 if(!d) 00089 { 00090 cerr << "Could not open directory " << sourcedir.c_str() << endl; 00091 return 1; 00092 } 00093 struct dirent* dent; 00094 00095 bool hasCode = false; 00096 bool hasOBJS = false; 00097 00098 while( (dent = readdir(d)) != 0) 00099 { 00100 00101 string s = dent->d_name; 00102 00103 if(s=="." || s=="..") 00104 continue; 00105 00106 if (s.find("OBJS") != string::npos) 00107 { 00108 if (islink(sourcedir + s)) 00109 { 00110 command = "rm " + sourcedir + s; 00111 system(command.c_str()); 00112 } 00113 else 00114 hasOBJS = true; 00115 } 00116 00117 if (!isdir(sourcedir + s)) 00118 { 00119 if (s.rfind(".cc") != string::npos) 00120 hasCode = true; 00121 00122 continue; 00123 } 00124 00125 // ignore CVS dirs 00126 if (s.find("CVS") != string::npos) 00127 continue; 00128 00129 newSourceDir = sourcedir + s + "/"; 00130 newDestDir = destdir + s + "/"; 00131 makedir(newDestDir); 00132 00133 if(s.find("OBJS") != string::npos) 00134 { 00135 // OBJ dir found! 00136 cout << spc << "-> Copying and creating link.."; 00137 cout.flush(); 00138 copyAndLinkObjs(newSourceDir, sourcedir, newDestDir); 00139 cout << "done!" << endl; 00140 } 00141 else 00142 { 00143 // normal dir 00144 goAndCreateDir(newSourceDir, newDestDir, spc); 00145 } 00146 00147 } 00148 00149 if (hasCode && !hasOBJS) 00150 { 00151 cout << spc << "-> Creating OBJS dir and linking it.."; 00152 00153 // checks if directory already exists in the destination 00154 00155 if (!isdir(destdir + "OBJS")) 00156 makedir(destdir + "OBJS"); 00157 00158 command = "ln -s " + destdir + "OBJS " + sourcedir; 00159 system(command.c_str()); 00160 00161 cout << "done!" << endl; 00162 } 00163 00164 00165 closedir(d); 00166 00167 return 0; 00168 } 00169 00170 void copyAndLinkObjs(string& sourceOBJdir, string& original_sourcedir, string& destOBJdir) 00171 { 00172 string command; 00173 00174 // copy all the object files 00175 command = "cp -R " + sourceOBJdir + "*" + " " + destOBJdir; 00176 system(command.c_str()); 00177 00178 // delete the old OBJ directory 00179 command = "rm -R " + sourceOBJdir; 00180 system(command.c_str()); 00181 00182 // make the symbolic link of the OBJ directory 00183 command = "ln -s " + destOBJdir + " " + original_sourcedir; 00184 system(command.c_str()); 00185 00186 } 00187 00188 00189 void makedir(const string& dir) 00190 { 00191 // directory already exists!! 00192 if (isdir(dir)) 00193 return; 00194 00195 // make the symbolic link of the OBJS directory 00196 string mkdirCommand = "mkdir " + dir; 00197 system(mkdirCommand.c_str()); 00198 } 00199 00200 bool isdir(const string& path) 00201 { 00202 struct stat statusinfo; 00203 int status; 00204 00205 status = lstat(path.c_str(), &statusinfo); 00206 00207 if (status != 0) 00208 return false; 00209 00210 if (S_ISDIR(statusinfo.st_mode)) 00211 return true; 00212 else 00213 return false; 00214 } 00215 00216 bool islink(const string& path) 00217 { 00218 struct stat statusinfo; 00219 int status; 00220 00221 status = lstat(path.c_str(), &statusinfo); 00222 00223 if (status != 0) 00224 return false; 00225 00226 if (S_ISLNK(statusinfo.st_mode)) 00227 return true; 00228 else 00229 return false; 00230 00231 } 00232

Generated on Tue Aug 17 16:00:05 2004 for PLearn by doxygen 1.3.7