#include #include #include #include #include #include "libCfg.h" #include "libdb.h" #include "libPIDF.h" // This subroutinte will create a nice looking string std::string GetUnitString (SDDAS_ULONG data_key, int whichUnit, bool addVarBanded = false) { char tmp [80]; // char num [7]; int p_errno; // sprintf (num, "%d. ", whichUnit); if (ALL_OKAY != (p_errno = ReadPidf (data_key, "", PIDF_UNITS, NOTUSED, whichUnit, LONGDESC, (void *) &tmp))) { std::cerr << "GetUnitString: ERROR - could not get long description!" << std::endl; } std::string unit_str; // = num; unit_str += tmp; unit_str += " ("; if (ALL_OKAY != (p_errno = ReadPidf (data_key, "", PIDF_UNITS, NOTUSED, whichUnit, UNITLAB, (void *) &tmp))) { std::cerr << "GetUnitString: ERROR - could not get unit label!" << std::endl; } unit_str += tmp; unit_str += ")"; if (addVarBanded) unit_str += " (Var. Banded)"; return unit_str; } // Go through the entire PIDF pulling unit names std::vector GetAllUnitNames (SDDAS_ULONG data_key) { std::vector ret_val; int p_errno; int num_units; if (ALL_OKAY != (p_errno = ReadPidf (data_key, "", NOTUSED, NOTUSED, NOTUSED, TOTALUNITS, (void *) &num_units))) { std::cerr << "GetAllUnitNames: ERROR - could not get number of units!" << std::endl; } // Last, with the list of units, place into the list // which ones match the unit id for (int whichUnit = 0; whichUnit < num_units; whichUnit++) { ret_val.push_back (GetUnitString (data_key, whichUnit)); } return (ret_val); } std::vector GetScanUnits (SDDAS_ULONG data_key, int whichBlock) { // read the number of scan blocks int p_errno; std::vector scan_units; std::vector varBanded; std::vector ret_val; // for each of the scan blocks, read the units_index int number_tot_scans; if ((p_errno = ReadPidf (data_key, "", SWEEP_STEP, whichBlock, NOTUSED, NUNITS, (void *) &number_tot_scans)) == ALL_OKAY) { for (int i = 0; i < number_tot_scans; i++) { int unit_num; if ((p_errno = ReadPidf (data_key, "", SWEEP_STEP, whichBlock, i, UNITNUM, (void *) &unit_num)) == ALL_OKAY) { scan_units.push_back (unit_num); } // read the var_units int var_unit; if ((ReadPidf (data_key, "", SWEEP_STEP, whichBlock, i, VARUNITNUM, (void *) &var_unit)) != ALL_OKAY) { var_unit = -1; } varBanded.push_back (var_unit != -1); } } for (unsigned int whichUnit = 0; whichUnit < scan_units.size (); whichUnit++) { ret_val.push_back (GetUnitString (data_key, scan_units [whichUnit], varBanded [whichUnit])); } return ret_val; } int GetNumberOfGroups (SDDAS_ULONG data_key) { int num_groups; int p_errno; if (ALL_OKAY != (p_errno = ReadPidf (data_key, "", PIDF_GROUP, NOTUSED, NOTUSED, NUMOF, (void *) &num_groups))) { return (-1); } return (num_groups); } std::vector GetAllGroups (SDDAS_ULONG data_key, int num_groups) { char **group_names = new char *[num_groups]; int p_errno; for (int i = 0; i < num_groups; ++i) group_names [i] = new char [MAX_PIDF_NAME]; if (ALL_OKAY != (p_errno = ReadPidf (data_key, "", PIDF_GROUP, NOTUSED, NOTUSED, NAMES, (void *) group_names))) { std::cerr << "GetAllGroups: ERROR - could not get group names!" << std::endl; } std::vector ret_val; for (int whichGroup = 0; whichGroup < num_groups; whichGroup++) { ret_val.push_back (group_names [whichGroup]); delete [] group_names [whichGroup]; } delete [] group_names; return (ret_val); } void GetAllUnits (SDDAS_ULONG data_key, int num_groups) { std::vector < std::vector > allDataUnitNumbers; std::vector < SDDAS_INT > allScanUnitNumbers; bool seenGroup [num_groups]; memset (seenGroup, false, sizeof (seenGroup)); // Go through all the sensors and build a list of groups int error_code, number_tot_sensors, gnum, num_units; if ((error_code = ReadPidf (data_key, "", SENSOR, NOTUSED, NOTUSED, NUMOF, (void *) &number_tot_sensors)) == ALL_OKAY) { for (int whichSensor = 0; whichSensor < number_tot_sensors; whichSensor++) { if ((error_code = ReadPidf (data_key, "", SENSOR, whichSensor, NOTUSED, GRPNUM, (void *) &gnum)) == ALL_OKAY) { // Have we already done this group? if (seenGroup [gnum] == false) { if ((error_code = ReadPidf (data_key, "", SENSOR, whichSensor, NOTUSED, NUNITS, (void *) &num_units)) != ALL_OKAY) { std::cerr << "GetAllUnits: ERROR - could not get number of units!" << std::endl; } int *unit_nums = new SDDAS_INT [num_units]; if ((error_code = ReadPidf (data_key, "", SENSOR, whichSensor, NOTUSED, UNITNUMS, (void *) unit_nums)) != ALL_OKAY) { std::cerr << "GetAllUnits: ERROR - could not get unit numbers!" << std::endl; } // This is where we add an entry to the units std::vector unitNumbers; for (int i = 0; i < num_units; ++i) unitNumbers.push_back (unit_nums [i]); delete [] unit_nums; allDataUnitNumbers.push_back (unitNumbers); // Now, do the scan units SDDAS_INT swp_ndx; if ((error_code = ReadPidf (data_key, "", SENSOR, whichSensor, NOTUSED, SWPNDX, (void *) &swp_ndx)) == ALL_OKAY) { allScanUnitNumbers.push_back (swp_ndx); } else allScanUnitNumbers.push_back (-1); seenGroup [gnum] = true; } } else { std::cerr << "GetAllUnits: ERROR - could not get group number!" << std::endl; } } // for } else { std::cerr << "GetAllUnits: ERROR - could not get group number!" << std::endl; } // Print the results for (unsigned int i = 0; i < allDataUnitNumbers.size (); ++i) { std::cout << "GROUP " << i << std::endl; std::cout << "DATA UNITS" << std::endl; for (unsigned int j = 0; j < allDataUnitNumbers [i].size (); ++j) { std::cout << GetUnitString (data_key, allDataUnitNumbers [i][j]) << std::endl; } std::cout << std::endl; std::cout << "SCAN UNITS" << std::endl; if (allScanUnitNumbers [i] != -1) { std::vector scanUnits = GetScanUnits (data_key, allScanUnitNumbers [i]); for (unsigned int j = 0; j < scanUnits.size (); ++j) { std::cout << scanUnits [j] << std::endl; } } } } int main (int argc, char **argv) { SDDAS_ULONG key = 0; if (argc < 2) { puts ("Error! Usage: unitsList P M E I V or "); puts ("Error! Usage: unitsList filename"); return -1; } if (argc > 2) { dbInitialize (); CfgInit (); key = dbGetDataKey (argv [1], argv [2], argv [3], argv [4], argv [5]); if (key == 0) { printf ("dump_pidf: Can't open (%s/%s/%s/%s/%s)\n", argv [1], argv [2], argv [3], argv [4], argv [5]); exit (-1); } } else { char vinst [MAXPATHLEN]; char *beginning; memset (vinst, 0, sizeof (vinst)); beginning = strrchr (argv [1], '/'); if (beginning == NULL) beginning = argv [1]; else beginning++; strncpy (vinst, beginning, strcspn (beginning, ".")); PIDF_SetFileName (argv [1], vinst); } /* std::vector allUnits = GetAllUnitNames (key); std::vector scanUnits = GetScanUnits (key); std::vector goodUnits (allUnits.size () + scanUnits.size ()); std::vector ::iterator endIter; endIter = set_difference (allUnits.begin (), allUnits.end (), scanUnits.begin (), scanUnits.end (), goodUnits.begin ()); goodUnits.erase (endIter, goodUnits.end ()); std::cout << "DATA UNITS" << std::endl; for (unsigned int i = 0; i < goodUnits.size (); i++) { std::cout << goodUnits [i] << std::endl; } std::cout << std::endl; if (!scanUnits.empty ()) { std::cout << "SCAN UNITS" << std::endl; for (unsigned int i = 0; i < scanUnits.size (); i++) { std::cout << scanUnits [i] << std::endl; } std::cout << std::endl; } */ int num_groups = GetNumberOfGroups (key); std::vector allGroups = GetAllGroups (key, num_groups); std::cout << "GROUPS" << std::endl; for (unsigned int i = 0; i < allGroups.size (); i++) { std::cout << allGroups [i] << std::endl; } std::cout << std::endl; GetAllUnits (key, num_groups); if (argc > 2) CfgTerm (); return (0); }