Go to Overview over all GrAL packages.
Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

src/Utility/safe-file.C

Go to the documentation of this file.
00001 
00002 // $LICENSE
00003 
00004 
00005 #include "Utility/safe-file.h"
00006 
00007 #include <iostream>
00008 #include <fstream>
00009 #include <string> 
00010 #include <stdlib.h>
00011 
00012  
00013 int file_interactive::open(ifstream& in, std::string const& filename)
00014   {
00015     if(in.is_open()) {
00016       in.close();
00017     }
00018 
00019    const char* name = filename.c_str();
00020    in.open(name);
00021 
00022    std::string my_filename(filename);
00023      while (! in.is_open()) {
00024      std::cerr << "could not open input file \"" << my_filename << "\" !\n"
00025                << "please enter new filename: ";
00026 
00027      std::cin >> my_filename;
00028 
00029      const char* my_name = my_filename.c_str();
00030      in.open(my_name);
00031    }
00032    return is_open;
00033   }
00034 
00035 int file_interactive::open_gz(std::ifstream& in, std::string const& filename, int strictness)
00036 {
00037   
00038   if(in.is_open())
00039     in.close();
00040   
00041   int res = (int) failed;
00042   std::string my_name(filename);
00043   const char* name = filename.c_str();
00044   do { 
00045     in.open(name);
00046     if( in.is_open()) 
00047       res = (int) is_open;
00048     else { // try ".gz"
00049       in.open((my_name+".gz").c_str());
00050       if(in.is_open()) {
00051         in.close();
00052         res = (int) is_open_gz;
00053         system( ("gunzip " + my_name+".gz").c_str());
00054         in.open(name);
00055       }
00056       // ERROR: file could not be opened: prompt user
00057       else {
00058         std::cerr << "could not open neither \"" << my_name << "\" nor \""
00059              << my_name + ".gz" << "\" !\n";
00060         if ((strictness == (int)insist)) {
00061           std::cerr <<  "please enter new filename: ";
00062           std::cin >> my_name;
00063           name = my_name.c_str();
00064         }
00065       }
00066     }
00067   }  while (! in.is_open() && (strictness == (int)insist));
00068     
00069   return res;
00070 }
00071 
00072 void file_interactive::close(std::ifstream& in, std::string const& nm, int gz)
00073 {
00074   if(in.is_open())
00075     in.close();
00076   if(gz == (int) is_open_gz)
00077     system(("gzip " + nm).c_str());
00078 }
00079 
00080 void file_interactive::close(std::ofstream& out, std::string const& nm, int gz)
00081 {
00082   if(out.is_open())
00083     out.close();
00084   if(gz == (int) is_open_gz)
00085     system(("gzip " + nm).c_str());
00086 }
00087 
00088 int file_interactive::open(std::ofstream& out, std::string const& filename) 
00089   //std::ios_base::openmode ios_mode)
00090 {
00091   int res = (int)failed;
00092   if(out.is_open())
00093     out.close();
00094   
00095   const char* name = filename.c_str();
00096   //  out.open(name, ios_mode);
00097   out.open(name);
00098   
00099   std::string my_name = filename;
00100   while (!out.is_open()) {
00101     std::cerr << "could not open output file \"" << my_name << "\" !\n"
00102               << "please enter new filename: ";
00103     std::cin  >> my_name;
00104     
00105      const char* new_name = my_name.c_str();
00106      out.open(new_name);
00107   }
00108   if(out.is_open())
00109     res = is_open;
00110 
00111   return res;
00112 }
00113 

Copyright (c) Guntram Berti 1997-2002. See the GrAL Homepage for up-to-date information.

Generated at Tue Feb 26 15:57:02 2002 for Utilities by doxygen 1.2.11-20011104 written by Dimitri van Heesch, © 1997-2000