Changeset 797


Ignore:
Timestamp:
20/07/06 20:07:47 (6 years ago)
Author:
pfp
Message:

Added ability to deal with multiple types of binary data.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/applications/NXtranslate/binary/BinaryRetriever.cpp

    r796 r797  
    1414using std::endl; 
    1515 
    16 typedef uint32_t data_t; 
    17  
     16static const string INT8("INT8"); 
     17static const string INT16("INT16"); 
     18static const string INT32("INT32"); 
     19static const string UINT8("UINT8"); 
     20static const string UINT16("UINT16"); 
     21static const string UINT32("UINT32"); 
     22static const string FLOAT32("FLOAT32"); 
     23static const string FLOAT64("FLOAT64"); 
     24static const string BYTE("BYTE"); 
     25 
     26static const int DEFAULT_TYPE=NX_UINT32; 
    1827 
    1928/** 
     
    8089 
    8190/** 
     91 * This function turns the type as a string into an integer for use 
     92 * with NeXus. 
     93 */ 
     94static int getDataType(const string &str_type){ 
     95  if(str_type.empty()){ 
     96    return DEFAULT_TYPE; 
     97  }else if(str_type==INT8){ 
     98    return NX_INT8; 
     99  }else if(str_type==INT16){ 
     100    return NX_INT16; 
     101  }else if(str_type==INT32){ 
     102    return NX_INT32; 
     103  }else if(str_type==UINT8){ 
     104    return NX_UINT8; 
     105  }else if(str_type==UINT16){ 
     106    return NX_UINT16; 
     107  }else if(str_type==UINT32){ 
     108    return NX_UINT32; 
     109  }else if(str_type==FLOAT32){ 
     110    return NX_FLOAT32; 
     111  }else if(str_type==FLOAT64){ 
     112    return NX_FLOAT64; 
     113  }else if(str_type==BYTE){ 
     114    return NX_CHAR; 
     115  }else{ 
     116    throw invalid_argument("Invalid type: "+str_type); 
     117  } 
     118} 
     119 
     120/** 
     121 * This function turns the type as a string into a platform dependent 
     122 * size. 
     123 */ 
     124static size_t getDataTypeSize(const int type){ 
     125  if(type==NX_INT8){ 
     126    return sizeof(int8_t); 
     127  }else if(type==NX_INT16){ 
     128    return sizeof(int16_t); 
     129  }else if(type==NX_INT32){ 
     130    return sizeof(int32_t); 
     131  }else if(type==NX_UINT8){ 
     132    return sizeof(uint8_t); 
     133  }else if(type==NX_UINT16){ 
     134    return sizeof(uint16_t); 
     135  }else if(type==NX_UINT32){ 
     136    return sizeof(uint32_t); 
     137  }else if(type==NX_FLOAT32){ 
     138    return sizeof(float); 
     139  }else if(type==NX_FLOAT64){ 
     140    return sizeof(double); 
     141  }else if(type==NX_CHAR){ 
     142    return sizeof(char); 
     143  }else{ 
     144    throw invalid_argument("This statement should never be reached"); 
     145  } 
     146} 
     147 
     148/** 
    82149 * This is the method for retrieving data from a file. The string must 
    83150 * be of the form "type:offset,delta,number". 
     
    95162    } 
    96163 
     164  // break the location string into a type and sizing information 
     165  int type; 
     166  string sizing; 
     167  { 
     168    vector<string> temp=string_util::split(location,":"); 
     169    cout << "SIZE=" << temp.size() << endl; 
     170    if(temp.size()==1){ 
     171      type=getDataType(""); 
     172      sizing=location; 
     173    }else if(temp.size()==2){ 
     174      type=getDataType(temp[0]); 
     175      sizing=temp[1]; 
     176    }else{ 
     177      throw invalid_argument("can only specify one type in location string"); 
     178    } 
     179    cout << "\"" << type << "\" \"" << sizing << "\"" << endl; 
     180  } 
     181 
    97182  // break the location string into three parts: file_size,data_start,data_size 
    98183  string file_size_str; 
     
    100185  string size_str; 
    101186  { 
    102     vector<string> temp=string_util::split(location,"]["); 
     187    vector<string> temp=string_util::split(sizing,"]["); 
    103188    if(temp.size()!=3) 
    104189      { 
     
    163248  // allocate the space for the result 
    164249  void *data; 
    165   if(NXmalloc(&data,rank,dims,NX_UINT32)!=NX_OK) 
     250  cout << "TYPE=" << type << endl; 
     251  if(NXmalloc(&data,rank,dims,type)!=NX_OK) 
    166252    { 
    167253      throw runtime_error("NXmalloc failed"); 
     
    169255 
    170256  // prepare data buffer 
    171   size_t data_size=sizeof(data_t); 
     257  size_t data_size=getDataTypeSize(type); 
    172258  /* 
    173259  size_t num_items=1; 
     
    204290  size_t num_items=*(size.rbegin()); 
    205291  size_t buffer_size=num_items*data_size; 
    206   data_t data_buffer[num_items]; 
     292  char   data_buffer[buffer_size]; 
    207293 
    208294  // push through the file grabbing the proper bits 
    209295  scalar_position=data_size*calculate_position(file_size,pos); 
    210296  data_file.seekg(scalar_position,std::ios::beg); 
    211   data_file.read(reinterpret_cast<char *>(data_buffer),buffer_size); 
     297  data_file.read(data_buffer,buffer_size); 
    212298 
    213299  // copy into final array 
    214   memcpy((static_cast<data_t *>(data))+data_index,data_buffer,buffer_size); 
     300  memcpy((static_cast<char *>(data))+data_index*data_size,data_buffer,buffer_size); 
    215301  data_index+=num_items; 
    216302 
     
    222308      data_file.read(reinterpret_cast<char *>(data_buffer),buffer_size); 
    223309      // copy into final array 
    224       memcpy((static_cast<data_t *>(data))+data_index,data_buffer,buffer_size); 
     310      memcpy((static_cast<char *>(data))+data_index*data_size,data_buffer,buffer_size); 
    225311      data_index+=num_items; 
    226312    } 
Note: See TracChangeset for help on using the changeset viewer.