Skip to content

Commit f7c6716

Browse files
author
mikejiang
committed
Convert string to const char * before writing to h5. #2
1 parent 9ce1e91 commit f7c6716

File tree

3 files changed

+63
-48
lines changed

3 files changed

+63
-48
lines changed

inst/include/cytolib/CytoFrame.hpp

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ const H5std_string DATASET_NAME( "data");
3030
* used for communicate with h5 since h5 doesn't support customized container data type
3131
*/
3232
struct KEY_WORDS_SIMPLE{
33-
string key, value;
34-
KEY_WORDS_SIMPLE(const string & k, const string & v):key(k),value(v){};
33+
const char * key;
34+
const char * value;
35+
KEY_WORDS_SIMPLE(const char * k, const char * v):key(k),value(v){};
3536
};
3637

3738
class CytoFrame;
@@ -221,14 +222,14 @@ class CytoFrame{
221222
hsize_t dim_pne[] = {2};
222223
ArrayType pne(datatype, 1, dim_pne);
223224

224-
CompType param_type(sizeof(cytoParam));
225-
param_type.insertMember("channel", HOFFSET(cytoParam, channel), str_type);
226-
param_type.insertMember("marker", HOFFSET(cytoParam, marker), str_type);
227-
param_type.insertMember("min", HOFFSET(cytoParam, min), datatype);
228-
param_type.insertMember("max", HOFFSET(cytoParam, max), datatype);
229-
param_type.insertMember("PnG", HOFFSET(cytoParam, PnG), datatype);
230-
param_type.insertMember("PnE", HOFFSET(cytoParam, PnE), pne);
231-
param_type.insertMember("PnB", HOFFSET(cytoParam, PnB), PredType::NATIVE_INT8);
225+
CompType param_type(sizeof(cytoParam_cstr));
226+
param_type.insertMember("channel", HOFFSET(cytoParam_cstr, channel), str_type);
227+
param_type.insertMember("marker", HOFFSET(cytoParam_cstr, marker), str_type);
228+
param_type.insertMember("min", HOFFSET(cytoParam_cstr, min), datatype);
229+
param_type.insertMember("max", HOFFSET(cytoParam_cstr, max), datatype);
230+
param_type.insertMember("PnG", HOFFSET(cytoParam_cstr, PnG), datatype);
231+
param_type.insertMember("PnE", HOFFSET(cytoParam_cstr, PnE), pne);
232+
param_type.insertMember("PnB", HOFFSET(cytoParam_cstr, PnB), PredType::NATIVE_INT8);
232233

233234
return param_type;
234235

@@ -251,12 +252,43 @@ class CytoFrame{
251252
DSetCreatPropList plist;
252253
plist.setChunk(1, dim_param);
253254
DataSet ds = file.createDataSet( "params", get_h5_datatype_params(DataTypeLocation::H5), dsp_param, plist);
254-
255-
ds.write(&params[0], get_h5_datatype_params(DataTypeLocation::MEM));
256-
257-
255+
auto params_char = params_c_str();
256+
ds.write(&params_char[0], get_h5_datatype_params(DataTypeLocation::MEM));
257+
}
258+
/**
259+
* Convert string to cstr in params for writing to h5
260+
* @return
261+
*/
262+
vector<cytoParam_cstr> params_c_str() const{
263+
auto nParams = params.size();
264+
vector<cytoParam_cstr> res(nParams);
265+
for(unsigned i = 0; i < nParams; i++)
266+
{
267+
res[i].channel = params[i].channel.c_str();
268+
res[i].marker = params[i].marker.c_str();
269+
res[i].min = params[i].min;
270+
res[i].max = params[i].max;
271+
res[i].PnG = params[i].PnG;
272+
res[i].PnE[0] = params[i].PnE[0];
273+
res[i].PnE[1] = params[i].PnE[1];
274+
res[i].PnB = params[i].PnB;
275+
}
276+
return res;
277+
}
278+
/**
279+
* Convert string to cstr in keys/pdata for writing to h5
280+
* @return
281+
*/
282+
template<class T>
283+
vector<KEY_WORDS_SIMPLE> to_kw_vec(const T & x) const{
284+
//convert to vector
285+
vector<KEY_WORDS_SIMPLE> keyVec;
286+
for(const auto & e : x)
287+
{
288+
keyVec.push_back(KEY_WORDS_SIMPLE(e.first.c_str(), e.second.c_str()));
289+
}
290+
return keyVec;
258291
}
259-
260292
virtual void write_h5_keys(H5File file) const
261293
{
262294
CompType key_type = get_h5_datatype_keys();
@@ -267,14 +299,7 @@ class CytoFrame{
267299
plist.setChunk(1, dim_key);
268300
DataSet ds = file.createDataSet( "keywords", key_type, dsp_key, plist);
269301

270-
//convert to vector
271-
vector<KEY_WORDS_SIMPLE> keyVec;
272-
for(const auto & e : keys_)
273-
{
274-
keyVec.push_back(KEY_WORDS_SIMPLE(e.first, e.second));
275-
}
276-
277-
302+
auto keyVec = to_kw_vec<KEY_WORDS>(keys_);
278303
ds.write(&keyVec[0], key_type );
279304

280305
}
@@ -292,14 +317,7 @@ class CytoFrame{
292317

293318
DataSet ds = file.createDataSet( "pdata", key_type, dsp_pd, plist);
294319

295-
//convert to vector
296-
297-
vector<KEY_WORDS_SIMPLE> keyVec;
298-
for(std::pair<std::string, string> e : pheno_data_)
299-
{
300-
keyVec.push_back(KEY_WORDS_SIMPLE(e.first, e.second));
301-
}
302-
320+
auto keyVec = to_kw_vec<PDATA>(pheno_data_);
303321
ds.write(&keyVec[0], key_type );
304322

305323
}

inst/include/cytolib/H5CytoFrame.hpp

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ class H5CytoFrame:public CytoFrame{
111111
DataSet ds = file.openDataSet("params");
112112
hsize_t size[1] = {params.size()};
113113
ds.extend(size);
114-
ds.write(&params[0], param_type );
114+
auto params_char = params_c_str();
115+
116+
ds.write(&params_char[0], param_type );
115117
ds.flush(H5F_SCOPE_LOCAL);
116118
is_dirty_params = false;
117119
}
@@ -120,13 +122,7 @@ class H5CytoFrame:public CytoFrame{
120122
{
121123
CompType key_type = get_h5_datatype_keys();
122124
DataSet ds = file.openDataSet("keywords");
123-
124-
//convert to vector
125-
vector<KEY_WORDS_SIMPLE> keyVec;
126-
for(const auto & e : keys_)
127-
{
128-
keyVec.push_back(KEY_WORDS_SIMPLE(e.first, e.second));
129-
}
125+
auto keyVec = to_kw_vec<KEY_WORDS>(keys_);
130126

131127
hsize_t size[1] = {keyVec.size()};
132128
ds.extend(size);
@@ -140,14 +136,7 @@ class H5CytoFrame:public CytoFrame{
140136
CompType key_type = get_h5_datatype_keys();
141137
DataSet ds = file.openDataSet("pdata");
142138

143-
//convert to vector
144-
145-
vector<KEY_WORDS_SIMPLE> keyVec;
146-
for(std::pair<std::string, string> e : pheno_data_)
147-
{
148-
keyVec.push_back(KEY_WORDS_SIMPLE(e.first, e.second));
149-
}
150-
139+
auto keyVec = to_kw_vec<PDATA>(pheno_data_);
151140
hsize_t size[1] = {keyVec.size()};
152141
ds.extend(size);
153142

inst/include/cytolib/readFCSHeader.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,15 @@ struct cytoParam{
100100
EVENT_DATA_TYPE PnE[2];
101101
int PnB;
102102
};
103-
103+
//for writing to h5 since writing vlen string causes segfault on mac
104+
struct cytoParam_cstr{
105+
const char * channel;
106+
const char * marker;
107+
EVENT_DATA_TYPE min, max, PnG;
108+
// pair<EVENT_DATA_TYPE, EVENT_DATA_TYPE> PnE;//replace pair with simple array since it is not clear how to create compound type for pair
109+
EVENT_DATA_TYPE PnE[2];
110+
int PnB;
111+
};
104112

105113
};
106114

0 commit comments

Comments
 (0)