string JsonVehicleRecord::convertToJSON(const VehicleRecord &rVehicleRecord) { // String to represent the vehicle record object string json_obj_string; string json_license = convertString(rVehicleRecord.getLicensePlate()); string json_type = convertString(rVehicleRecord.getVehicleType()); string json_speed= convertFloat(rVehicleRecord.getSpeed()); string json_date = convertDate(rVehicleRecord.getDate()); //Adds the initial character of a json object json_obj_string += OBJ_START_CHAR; //Adds the license plate attribute json_obj_string += RECORD_LICEN_ATTR + ATTR_DECLA_CHAR + json_license; //Adds the separator character between attributes json_obj_string += ATTR_SEPAR_CHAR; //Adds the vehicle type attribute json_obj_string += RECORD_TYPE_ATTR + ATTR_DECLA_CHAR + json_type; //Adds the separator character between attributes json_obj_string += ATTR_SEPAR_CHAR; //Adds the speed attribute json_obj_string += RECORD_SPEED_ATTR + ATTR_DECLA_CHAR + json_speed; //Adds the separator character between attributes json_obj_string += ATTR_SEPAR_CHAR; //Adds the date attribute json_obj_string += RECORD_DATE_ATTR + ATTR_DECLA_CHAR + json_date; //Adds the end character of a json object json_obj_string += OBJ_END_CHAR; return json_obj_string; }
int main( int argc, char *argv[] ) { bool bHelp = false; //!< Help was requested on the command line bool bError = false; //!< An error occurred on the command line float Rx = 0.0; float Ry = 0.0; float Rz = 0.0; float cx=0.0; float cy=0.0; float cz=0.0; int nLevels = 2; int iB, iN, iC; double dMassIn, dMassOut; std::string inName; std::string outName; ifTipsy in; // The input file ofTipsy out; // The output file //Tipsy::SphericalBinner binner; Tipsy::RadialBinner binner; TipsyHeader h; // The header structure TipsyGasParticle g; // A gas particle TipsyDarkParticle d; // A dark particle TipsyStarParticle s; // A star particle uint32_t i; std::clog << "WARNING: This tool is experimental" << std::endl; //! Parse command line for(;;) { int c, option_index=0; char *p; static struct option long_options[] = { { "help", 0, 0, OPT_HELP }, { "radius", 1, 0, OPT_RADIUS }, { "center", 1, 0, OPT_CENTER }, { "centre", 1, 0, OPT_CENTER }, }; c = getopt_long( argc, argv, "hc:r:", long_options, &option_index ); if ( c == -1 ) break; switch(c) { case OPT_HELP: bHelp = true; break; case OPT_RADIUS: assert( optarg != NULL ); if ( Rx > 0 ) { std::clog << "Specify radius only once" << std::endl; bError = true; } p = strtok(optarg,","); assert( p != NULL); Rx = atof(p); p = strtok(NULL,","); if ( p == NULL ) { Ry = Rz = Rx; } else { Ry = atof(p); p = strtok(NULL,","); if ( p == NULL ) BadRadius(); Rz = atof(p); p = strtok(NULL,","); if ( p != NULL ) BadRadius(); } if ( Rx <= 0 || Ry <= 0 || Rz <= 0 ) { std::clog << "Radius must be positive (not zero)" << std::endl; bError = true; } break; case OPT_CENTER: assert( optarg != NULL ); p = strtok(optarg,","); cx = convertFloat(p); p = strtok(NULL,","); cy = convertFloat(p); p = strtok(NULL,","); cz = convertFloat(p); p = strtok(NULL,","); if ( p != NULL ) BadCenter(); break; default: bError = true; break; } } if ( bHelp || bError ) { Usage(argv[0]); exit(1); } if ( Rx <= 0 ) { std::clog << "Specify the binning radius with --radius" << std::endl; exit(1); } if ( optind < argc ) { inName = argv[optind++]; } else { std::cerr << "Missing input file" << std::endl; exit(2); } if ( optind < argc ) { outName = argv[optind++]; } else { std::cerr << "Missing output file" << std::endl; exit(2); } // Open the input file for binning in.open(inName.c_str()); if ( ! in.is_open() ) { fprintf( stderr, "Unable to open Tipsy binary %s\n", argv[1] ); exit(2); } // Open the output file. out.open(outName.c_str()); if ( ! out.is_open() ) { fprintf( stderr, "Unable to create Tipsy binary %s\n", argv[2] ); exit(2); } iB = iN = iC = 0; dMassIn = dMassOut = 0.0; // Read the header from the input and write it to the output. // This won't be correct, but we will fix it up at the end. in >> h; out << h; // Calculate the binning float cr = powf(h.h_nBodies,1.0/3.0); nLevels = 0; do { nLevels++; } while( (cr*=0.5) >= 100 ); std::clog << "Grid: " << (int)cr << ", levels: " << nLevels << std::endl; //binner.setTheta(0.01); //binner.setBox(cx,cy,cz); //binner.setRadius(Radius); binner.setBox(cx,cy,cz); //binner.setBinning( (int)cr, nLevels, Rx, Ry, Rz ); binner.setBinning( nLevels, (int)cr, (int)cr, (int)cr, Rx, Ry, Rz ); // Read every particle and write it to the output file. for( i=0; i<h.h_nSph; i++ ) { in >> g; out << g; } for( i=0; i<h.h_nDark; i++ ) { in >> d; dMassIn += d.mass; if ( !binner.Bin(d) ) { iN++; out << d; dMassOut += d.mass; } else { iB++; } } for( i=0; i<h.h_nStar; i++ ) { in >> s; out << s; } // Write the binned, dark particles while( binner.getParticle(d) ) { iC++; out << d; dMassOut += d.mass; } // Update the header h.h_nDark = iN + iC; h.h_nBodies = h.h_nDark; out.seekp( tipsypos( tipsypos::header, 0 ) ); out << h; // Close the files. out.close(); in.close(); std::clog << "Binned " << iB << " particles into " << iC << " and wrote " << iN << std::endl; std::clog << "Mass check: " << " in: " << dMassIn << " out: " << dMassOut << std::endl; }
oret_t owatchConvertValue(const oval_t * src_val, oval_t * dst_val, char *data_buf, int data_buf_len) { oret_t rc = ERROR; char src_type, dst_type; int len; if (src_val == NULL || dst_val == NULL) return ERROR; src_type = src_val->type; dst_type = dst_val->type; if (dst_type == 's') { if (src_type == 's') { if (src_val->v.v_str == NULL) { dst_val->v.v_str = NULL; dst_val->len = 0; dst_val->time = src_val->time; if (data_buf && data_buf_len) *data_buf = 0; return OK; } if (src_val->len == 0) { dst_val->v.v_str = data_buf; dst_val->len = 0; dst_val->time = src_val->time; if (data_buf && data_buf_len) *data_buf = 0; return OK; } if (src_val->len < 0) return ERROR; if (data_buf == NULL || data_buf_len < src_val->len) return ERROR; oxbcopy(src_val->v.v_str, data_buf, src_val->len); if (src_val->len < data_buf_len) data_buf[src_val->len] = 0; dst_val->v.v_str = data_buf; dst_val->len = src_val->len; dst_val->time = src_val->time; return OK; } len = convertToString(src_type, src_val, data_buf, data_buf_len); if (len < 0) return ERROR; dst_val->v.v_str = data_buf; dst_val->len = len; dst_val->time = src_val->time; return OK; } switch (src_type) { case 'b': rc = convertSignedChar(src_val->v.v_char, dst_type, dst_val); break; case 'B': rc = convertUnsignedChar(src_val->v.v_uchar, dst_type, dst_val); break; case 'h': rc = convertSignedShort(src_val->v.v_short, dst_type, dst_val); break; case 'H': rc = convertUnsignedShort(src_val->v.v_ushort, dst_type, dst_val); break; case 'i': rc = convertSignedInt(src_val->v.v_int, dst_type, dst_val); break; case 'I': rc = convertUnsignedInt(src_val->v.v_uint, dst_type, dst_val); break; case 'l': rc = convertSignedLong(src_val->v.v_long, dst_type, dst_val); break; case 'L': rc = convertUnsignedLong(src_val->v.v_ulong, dst_type, dst_val); break; case 'f': rc = convertFloat(src_val->v.v_float, dst_type, dst_val); break; case 'd': rc = convertDouble(src_val->v.v_double, dst_type, dst_val); break; case 'E': rc = convertEnum(src_val->v.v_enum, dst_type, dst_val); break; case 's': rc = convertFromString(src_val->v.v_str, dst_type, dst_val); break; default: return ERROR; } if (rc == ERROR) return ERROR; switch (dst_val->type) { case 'b': dst_val->len = 1; break; case 'B': dst_val->len = 1; break; case 'h': dst_val->len = 2; break; case 'H': dst_val->len = 2; break; case 'i': dst_val->len = 4; break; case 'I': dst_val->len = 4; break; case 'l': dst_val->len = 4; break; case 'L': dst_val->len = 4; break; case 'q': dst_val->len = 16; break; case 'Q': dst_val->len = 16; break; case 'f': dst_val->len = 4; break; case 'd': dst_val->len = 8; break; case 'D': dst_val->len = 16; break; case 'p': dst_val->len = 4; break; case 'E': dst_val->len = 4; break; case 'v': return ERROR; case 's': return ERROR; default: return ERROR; } dst_val->time = src_val->time; return OK; }
void convert(long double& n, const char* str) { convertFloat(n, str, "long double"); }
void convert(float& n, const char* str) { convertFloat(n, str, "float"); }
void convert(long double& n, const std::string& str) { convertFloat(n, str, "long double"); }
void convert(float& n, const std::string& str) { convertFloat(n, str, "float"); }
void convert(double& n, const String& str) { convertFloat(n, str, "double"); }