00001
00002
00003 #include "config.h"
00004 #include "Ancillary.h"
00005 #include "debug.h"
00006
00007 #ifdef HAVE_UNISTD_H
00008 #include <unistd.h>
00009 #endif
00010
00011 namespace libdap {
00012
00057 string
00058 Ancillary::find_ancillary_file( const string &pathname,
00059 const string &ext,
00060 const string &dir,
00061 const string &file )
00062 {
00063 string::size_type slash = pathname.rfind('/') + 1;
00064 string directory = pathname.substr(0, slash);
00065 string filename = pathname.substr(slash);
00066 string basename = pathname.substr(slash, pathname.rfind('.') - slash);
00067
00068 DBG(cerr << "find ancillary file params: " << pathname << ", " << ext
00069 << ", " << dir << ", " << file << endl);
00070 DBG(cerr << "find ancillary file comp: " << directory << ", " << filename
00071 << ", " << basename << endl);
00072
00073 string dot_ext = "." + ext;
00074
00075 string name = directory + basename + dot_ext;
00076 if (access(name.c_str(), F_OK) == 0)
00077 return name;
00078
00079 name = pathname + dot_ext;
00080 if (access(name.c_str(), F_OK) == 0)
00081 return name;
00082
00083 name = directory + ext;
00084 if (access(name.c_str(), F_OK) == 0)
00085 return name;
00086
00087 name = dir + basename + dot_ext;
00088 if (access(name.c_str(), F_OK) == 0)
00089 return name;
00090
00091 name = directory + file + dot_ext;
00092 if (access(name.c_str(), F_OK) == 0)
00093 return name;
00094
00095 name = dir + file + dot_ext;
00096 if (access(name.c_str(), F_OK) == 0)
00097 return name;
00098
00099 name = dir + ext;
00100 if (access(name.c_str(), F_OK) == 0)
00101 return name;
00102
00103 return "";
00104 }
00105
00106
00107
00108
00109
00110
00111
00112
00126 string
00127 Ancillary::find_group_ancillary_file( const string &name, const string &ext )
00128 {
00129
00130
00131
00132 string::size_type slash = name.find_last_of('/');
00133 string dirname = name.substr(0, slash);
00134 string filename = name.substr(slash + 1);
00135 string rootname = filename.substr(0, filename.find_last_of('.'));
00136
00137
00138
00139 string::iterator rootname_iter = rootname.begin();
00140 string::iterator rootname_end_iter = rootname.end();
00141 if (isdigit(*rootname_iter)) {
00142 while (rootname_iter != rootname_end_iter
00143 && isdigit(*++rootname_iter))
00144 ;
00145
00146
00147
00148 string new_name = dirname;
00149 new_name.append("/");
00150 new_name.append(rootname_iter, rootname_end_iter);
00151 new_name.append(ext);
00152 DBG(cerr << "New Name (iter): " << new_name << endl);
00153 if (access(new_name.c_str(), F_OK) == 0) {
00154 return new_name;
00155 }
00156 }
00157
00158 string::reverse_iterator rootname_riter = rootname.rbegin();
00159 string::reverse_iterator rootname_end_riter = rootname.rend();
00160 if (isdigit(*rootname_riter)) {
00161 while (rootname_riter != rootname_end_riter
00162 && isdigit(*++rootname_riter))
00163 ;
00164 string new_name = dirname;
00165 new_name.append("/");
00166
00167
00168
00169
00170 new_name.append(rootname_end_riter.base(), rootname_riter.base());
00171 new_name.append(ext);
00172 DBG(cerr << "New Name (riter): " << new_name << endl);
00173 if (access(new_name.c_str(), F_OK) == 0) {
00174 return new_name;
00175 }
00176 }
00177
00178
00179
00180
00181 return "";
00182 }
00183
00184 void
00185 Ancillary::read_ancillary_das( DAS &das,
00186 const string &pathname,
00187 const string &dir,
00188 const string &file )
00189 {
00190 string name = find_ancillary_file( pathname, "das", dir, file ) ;
00191
00192 FILE *in = fopen( name.c_str(), "r" ) ;
00193 if( in )
00194 {
00195 das.parse( in ) ;
00196 int res = fclose( in ) ;
00197 if( res )
00198 {
00199 DBG(cerr << "DODSFilter::read_ancillary_das - Failed to close file " << (void *)in << endl ;) ;
00200 }
00201 }
00202 }
00203
00204 void
00205 Ancillary::read_ancillary_dds( DDS &dds,
00206 const string &pathname,
00207 const string &dir,
00208 const string &file )
00209 {
00210 string name = find_ancillary_file( pathname, "dds", dir, file ) ;
00211
00212 FILE *in = fopen( name.c_str(), "r" ) ;
00213 if( in )
00214 {
00215 dds.parse( in ) ;
00216 int res = fclose( in ) ;
00217 if( res )
00218 {
00219 DBG(cerr << "DODSFilter::read_ancillary_das - Failed to close file " << (void *)in << endl ;) ;
00220 }
00221 }
00222 }
00223
00224 }
00225