editor.cxx

Go to the documentation of this file.
00001 
00002 
00003 // Copyright 2002, Lowell Boggs Jr.
00004 //
00005 // This file or directory, containing source code for a computer program,
00006 // is Copyrighted by Lowell Boggs, Jr.  987 Regency Drive, Lewisville
00007 // TX (USA), 75067.  You may use, copy, modify, and distribute this
00008 // source file without charge or obligation so long as you agree to
00009 // the following:
00010 //
00011 //  1.  You must indemnify Lowell Boggs against any and all financial
00012 //      obligations caused by its use, misuse, function, or malfunction.
00013 //      Further, you acknowledge that there is no warranty of any kind,
00014 //      whatsoever.
00015 //
00016 //  2.  You agree not to attempt to patent any portion of this original
00017 //      work -- though you may attempt to patent your own extensions to
00018 //      it if you so choose.
00019 //
00020 //  3.  You keep this copyright notice with the file and all copies
00021 //      of the file and do not change it anyway except language translation.
00022 //
00023 // You are responsible for enforcing your own compliance with these
00024 // conditions and may not use this source file if you cannot agree to the
00025 // above terms and conditions.
00026 
00030 
00031 
00032 //@File
00035 
00036 #include <cxxtls/viewermanager.h>
00037 #include <cxxtls/file.h>
00038 #include <list>
00039 #include <set>
00040 #include <cxxtls/directoryviewer.h>
00041 #include <cxxtls/textviewer.h>
00042 #include <cxxtls/keyviewer.h>
00043 #include <texteditor.hxx>
00044 #include <cxxtls/ftpviewer.h>
00045 #include <cxxtls/csvviewer.h>
00046 #include <cxxtls/scripttableviewer.h>
00047 #include <cxxtls/options.h>
00048 #include <cxxtls/symbolviewer.h>
00049 
00050 #include <stdlib.h>
00051 
00052 using namespace cxxtls;
00053 
00054 extern char const *editversion;  // see makefile
00055 
00056 typedef CursorWindow::input_event input_event;
00057 
00058 using namespace std;
00059 
00060 Viewer* read_only_edit(ViewerManager*vm, std::string &fullname)
00061 {
00062   TextEditor* rv = static_cast<TextEditor*>(TextEditor::app(vm,fullname));
00063 
00064   if(rv)
00065   {
00066     rv->set_read_only();
00067   }
00068 
00069   return rv;
00070 
00071 }
00072 
00073 
00074 
00075 int main(int argc, char **argv, char **environ)
00076 {
00077 
00078   static char const *allowed_arguments=
00079   "-ftp," // use compiler concatenation to implement arguments here
00080   "-h,"
00081   "-help,"
00082   "-s,"
00083   "-l:"
00084   ;
00085 
00086   ProgramOptions::global_options_ = new ProgramOptions(allowed_arguments,
00087                                                        argc,
00088                                                        argv,
00089                                                        environ);
00090 
00091   ProgramOptions &options = *ProgramOptions::global_options_;
00092 
00093   ViewerManager vm; 
00094 
00095   vm.executable_version_ = editversion;
00096   vm.executable_name_    = options.argv[0]; // guaranteed to exist
00097 
00098   vm.open();  // need to open the window so that sizes of viewports
00099               // can be calculated.  This must be done AFTER the program
00100               // options are parsed because of the potential of the ProgramOptions
00101               // constructor to call exit().  If the window is open when that happens
00102               // the terminal characteristics will get messed up.
00103         
00104   vm.add_app(DirectoryViewer::app, DirectoryViewer::app_name);
00105   vm.set_directory_app(DirectoryViewer::app);
00106 
00107   vm.add_app(TextViewer::app, TextViewer::app_name);
00108   // vm.add_viewer_app(TextViewer::app, "");  // handles all files extensions
00109   vm.add_viewer_app(read_only_edit, "");  // handles all files extensions
00110 
00111   vm.add_app(TextEditor::app, TextEditor::app_name);
00112   vm.add_editor_app(TextEditor::app, "");  // handles all files extensions
00113 
00114   vm.add_app(CsvViewer::app, "CsvViewer");
00115   vm.add_editor_app(CsvViewer::app, ".csv");  
00116 
00117   vm.add_app(CsvViewer::app, "TsvViewer");
00118   vm.add_editor_app(CsvViewer::app, ".tsv");  
00119 
00120   vm.add_app(KeyViewer::app, KeyViewer::app_name);
00121 
00122   vm.add_app(FTPviewer::app, FTPviewer::app_name);
00123 
00124   vm.add_app(ScriptTableViewer::app, "TableCommander");
00125   vm.add_editor_app(ScriptTableViewer::app, ".stvsh");  
00126 
00127   vm.add_app(SymbolViewer::app, "SymbolViewer");
00128   vm.add_viewer_app(SymbolViewer::app, ".tagpp");  // cpptagdb.exe generated databases
00129 
00130   vm.add_viewer_app(SymbolViewer::app, ".cpptagdb");  // The symbol viewer knows that if the file name 
00131                                                       // extension is .cpptagdb, then it should 
00132                                                       // use cpptagdb.exe to read the file, minus the
00133                                                       // trailing .cpptagdb, using popen()
00134 
00135   // vm.add_editor_app(SymbolViewer::app, ".tagpp");  // you can't really EDIT this kind of file
00136 
00137 
00138   if(options.option("-h") || options.option("-help"))
00139   {
00140     vm.close();
00141 
00142     FileName me;
00143 
00144     if(options.argv.size() >= 1)
00145       me = options.argv[0];
00146 
00147     string prog = me.basename();
00148 
00149 
00150     cout << "This program supports the following command line options" << endl
00151          << endl
00152          << "  " << allowed_arguments << endl
00153          << endl
00154          ;
00155     cout
00156          << "Options have the following meaning and parameters:" << endl
00157          << "  (none)    -- list the current directory" << endl
00158          << "  filedir   -- edit the named file or directory" << endl
00159          << "  -s file   -- view the named file" << endl
00160          << "  -l n file -- edit fil and position to line n" <<endl
00161          << endl
00162          << "  -ftp [h] [u] [p] [d] -- start ftp session" << endl
00163          << "     host, user, password, and local directory are optional" << endl
00164          ;
00165     exit(1);
00166   }
00167   else
00168   if(options.option("-ftp"))
00169   {
00170     string path;
00171 
00172     if(options.argv.size() > 1)
00173     {
00174       // ie there were non-option arguments, treat them as parms to the ftp protocol
00175 
00176       for(size_t i=1; i < options.argv.size(); ++i)
00177       {
00178          path += options.argv[i];
00179          path += ' ';
00180       }
00181 
00182     }
00183 
00184     Viewer* ftpviewer = FTPviewer::app(&vm, path);
00185 
00186     if(ftpviewer)
00187     {
00188       vm.add(ftpviewer, path);
00189     }
00190     else
00191     {
00192       vm.close();
00193       exit(1);
00194     }
00195 
00196   }
00197   else
00198   if(options.argv.size() == 2)
00199   {
00200     // one name specified
00201 
00202     FileName pathname(options.argv[1]);
00203 
00204     pathname = pathname.expand_tildes();
00205 
00206     pathname.convert_to_absolute_path();
00207 
00208     if(pathname.is_dir())
00209     {
00210       pathname.convertToDirectorySearchPattern();
00211     }
00212 
00213 
00214     Viewer* editor;
00215 
00216     if(options.option("-s"))
00217       editor = vm.open(pathname);
00218     else
00219       editor = vm.edit(pathname);
00220 
00221     if(editor == 0)
00222     {
00223       vm.close();
00224       cout << "Error, couldn't find an editor for '" << pathname << "'" << endl;
00225       exit(1);
00226     }
00227 
00228     int line=1;
00229 
00230     if(options.option("-l"))
00231     {
00232       ProgramOptions::value_iterator first = options.option("-l").begin();
00233 
00234       sscanf(first->c_str(), "%d", &line);
00235 
00236       editor->set_row_col_hint(line-1,0);
00237     }
00238 
00239 
00240     vm.add(editor, pathname);
00241 
00242   }
00243   else
00244   if(options.argv.size() > 2)
00245   {
00246     for(size_t i = 1; i < options.argv.size(); ++i)
00247     {
00248       FileName pathname(options.argv[i]);
00249       
00250       pathname = pathname.expand_tildes();
00251 
00252       pathname.convert_to_absolute_path();
00253       
00254       if(pathname.is_dir())
00255       {
00256         pathname.convertToDirectorySearchPattern();
00257       }
00258       
00259       Viewer* editor;
00260       
00261       if(options.option("-s"))
00262         editor = vm.open(pathname);
00263       else
00264         editor = vm.edit(pathname);
00265       
00266       if(editor == 0)
00267       {
00268         vm.close();
00269         cout << "Error, couldn't find an editor for '" << pathname << "'" << endl;
00270         exit(1);
00271       }
00272       
00273       
00274       if(options.option("-l"))
00275       {
00276         int line=1;
00277 
00278         sscanf(options.option("-l").begin()->c_str(), "%d", &line);
00279       
00280         editor->set_row_col_hint(line-1,0);
00281       }
00282       
00283       vm.add(editor, pathname);
00284 
00285     }
00286   }
00287   else
00288   {
00289     FileName curdir(".");
00290     curdir.convert_to_absolute_path();
00291     curdir.convertToDirectorySearchPattern();
00292     vm.add(vm.edit(curdir), curdir);
00293   }
00294 
00295         
00296   vm.run();
00297 
00298 
00299   return 0;
00300 }
Generated on Wed Feb 29 22:50:03 2012 for CXXUtilities by  doxygen 1.6.3