00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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 "mime_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);
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 }