CSVDDS.cc

Go to the documentation of this file.
00001 // CSVDDS.cc
00002 
00003 // This file is part of bes, A C++ back-end server implementation framework
00004 // for the OPeNDAP Data Access Protocol.
00005 
00006 // Copyright (c) 2004-2009 University Corporation for Atmospheric Research
00007 // Author: Stephan Zednik <zednik@ucar.edu> and Patrick West <pwest@ucar.edu>
00008 // and Jose Garcia <jgarcia@ucar.edu>
00009 //
00010 // This library is free software; you can redistribute it and/or
00011 // modify it under the terms of the GNU Lesser General Public
00012 // License as published by the Free Software Foundation; either
00013 // version 2.1 of the License, or (at your option) any later version.
00014 // 
00015 // This library is distributed in the hope that it will be useful,
00016 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 // 
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023 //
00024 // You can contact University Corporation for Atmospheric Research at
00025 // 3080 Center Green Drive, Boulder, CO 80301
00026  
00027 // (c) COPYRIGHT University Corporation for Atmospheric Research 2004-2005
00028 // Please read the full copyright statement in the file COPYRIGHT_UCAR.
00029 //
00030 // Authors:
00031 //      zednik      Stephan Zednik <zednik@ucar.edu>
00032 //      pwest       Patrick West <pwest@ucar.edu>
00033 //      jgarcia     Jose Garcia <jgarcia@ucar.edu>
00034 
00035 #include <vector>
00036 #include <string>
00037 
00038 #include "CSVDDS.h"
00039 #include "BESInternalError.h"
00040 #include "BaseTypeFactory.h"
00041 #include "DDS.h"
00042 #include "Error.h"
00043 
00044 #include "Str.h"
00045 #include "Int16.h"
00046 #include "Int32.h"
00047 #include "Float32.h"
00048 #include "Float64.h"
00049 #include "cgi_util.h"
00050 
00051 #include "Array.h"
00052 
00053 #include "CSV_Obj.h"
00054 
00055 void csv_read_descriptors(DDS &dds, const string &filename) {
00056   
00057   vector<string> fieldList;
00058   string type;
00059   int recordCount;
00060   int index;
00061 
00062   Array* ar;
00063   void* data;
00064   BaseType *bt = NULL;
00065 
00066   CSV_Obj* csvObj = new CSV_Obj();
00067   csvObj->open(filename);
00068   csvObj->load();
00069 
00070   dds.set_dataset_name(name_path(filename));
00071 
00072   fieldList = csvObj->getFieldList();
00073   recordCount = csvObj->getRecordCount();
00074 
00075   for(vector<string>::iterator it = fieldList.begin(); it != fieldList.end(); it++) {
00076     type = csvObj->getFieldType(*it);
00077     ar = dds.get_factory()->NewArray(string(*it));
00078     data = csvObj->getFieldData(*it);
00079 
00080     if(type.compare(string(STRING)) == 0) {
00081       string* strings = new string[recordCount];
00082 
00083       bt = dds.get_factory()->NewStr(string(*it));
00084       ar->add_var(bt);
00085       ar->append_dim(recordCount, "record");
00086       
00087       index = 0;
00088       for(vector<string>::iterator it = ((vector<string>*)data)->begin(); 
00089           it != ((vector<string>*)data)->end(); it++) {
00090         strings[index] = *it;
00091         index++;
00092       }
00093       
00094       ar->set_value(strings, recordCount);
00095       
00096     } else if(type.compare(string(INT16)) == 0) {
00097       short* int16 = new short[recordCount];
00098       bt = dds.get_factory()->NewInt16(*it);
00099       ar->add_var(bt);
00100       ar->append_dim(recordCount, "record");
00101 
00102       index = 0;
00103       for(vector<short>::iterator it = ((vector<short>*)data)->begin();
00104           it != ((vector<short>*)data)->end(); it++) {
00105         int16[index] = *it;
00106         index++;
00107       }
00108 
00109       ar->set_value(int16, recordCount);
00110 
00111     } else if(type.compare(string(INT32)) == 0) {
00112       int* int32 = new int[recordCount];
00113       bt = dds.get_factory()->NewInt32(*it);
00114       ar->add_var(bt);
00115       ar->append_dim(recordCount, "record");
00116 
00117       index = 0;
00118       for(vector<int>::iterator it = ((vector<int>*)data)->begin();
00119           it != ((vector<int>*)data)->end(); it++) {
00120         int32[index] = *it;
00121         index++;
00122       }
00123 
00124       ar->set_value((dods_int32*)int32, recordCount); //blah!
00125 
00126     } else if(type.compare(string(FLOAT32)) == 0) {
00127       float* floats = new float[recordCount];
00128       bt = dds.get_factory()->NewFloat32(*it);
00129       ar->add_var(bt);
00130       ar->append_dim(recordCount, "record");
00131 
00132       index = 0;
00133       for(vector<float>::iterator it = ((vector<float>*)data)->begin(); 
00134           it != ((vector<float>*)data)->end(); it++) {
00135         floats[index] = *it;
00136         index++;
00137       }
00138 
00139       ar->set_value(floats, recordCount);
00140 
00141     } else if(type.compare(string(FLOAT64)) == 0) {
00142       double* doubles = new double[recordCount];
00143       bt = dds.get_factory()->NewFloat64(*it);
00144       ar->add_var(bt);
00145       ar->append_dim(recordCount, "record");
00146 
00147       index = 0;
00148       for(vector<double>::iterator it = ((vector<double>*)data)->begin(); 
00149           it != ((vector<double>*)data)->end(); it++) {
00150         doubles[index] = *it;
00151         index++;
00152       }
00153 
00154       ar->set_value(doubles, recordCount);
00155     } else {
00156         throw BESInternalError( "Bad Things Man", __FILE__, __LINE__ ) ;
00157     }
00158 
00159     dds.add_var(ar);
00160     delete ar;
00161     delete bt;
00162   }
00163 
00164   delete csvObj;
00165 }

Generated on 19 Feb 2010 for OPeNDAP Hyrax Back End Server (BES) by  doxygen 1.6.1