Skip to content

Commit 5e69466

Browse files
committed
Some small cleanups.
Fix SpecUtils Threadpool not going serial execution on macOS, when selected for in CMake. Fixed some warnings, and some small cleanup.
1 parent ada2f08 commit 5e69466

File tree

9 files changed

+46
-29
lines changed

9 files changed

+46
-29
lines changed

SpecUtils/SpecUtilsAsync.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,16 @@
3333
#include <functional>
3434
#include <condition_variable>
3535

36-
37-
#if( __APPLE__ && __clang__ )
36+
#if( SpecUtils_USING_NO_THREADING )
37+
#define ThreadPool_USING_SERIAL 1
38+
#elif( SpecUtils_USE_WT_THREADPOOL )
39+
#define ThreadPool_USING_WT 1
40+
#elif( __APPLE__ && __clang__ )
3841
//On apple systems, when we use Apples compilers, we can use Grand Central
3942
// Dispatch (GCD) to power the queue.
4043
// ToDo: Would !defined(__GNUC__) be better than __clang__ ?
4144
#include <dispatch/dispatch.h>
4245
#define ThreadPool_USING_GCD 1
43-
#elif( SpecUtils_USE_WT_THREADPOOL )
44-
#define ThreadPool_USING_WT 1
45-
#elif( SpecUtils_USING_NO_THREADING )
46-
#define ThreadPool_USING_SERIAL 1
4746
#else
4847
#define ThreadPool_USING_THREADS 1
4948
#if( __APPLE__ )

src/ParseUtils.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,9 @@ void expand_counted_zeros( const vector<float> &data, vector<float> &return_answ
147147
if( (*iter) <= 0.5f )
148148
throw runtime_error( "Invalid counted zeros: less than one number of zeros" );
149149

150+
if( (*iter) > 65537.0f )
151+
throw runtime_error( "Invalid counted zeros: too large number of zeros" );
152+
150153
const size_t nZeroes = ((iter==data.end()) ? 0u : static_cast<size_t>(floor(*iter + 0.5f)) );
151154

152155
if( (answer.size() + nZeroes) > 131072 )

src/SerialToDetectorModel.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,10 @@ namespace SerialToDetectorModel
262262
uint32_t val = 0;
263263
try
264264
{
265-
val = stoul( strval, nullptr, 10 );
265+
unsigned long tmp_val = stoul( strval, nullptr, 10 );
266+
//I guess we'll truncate, although we shouldnt actually get this, given the known
267+
// length of these serial numbers
268+
val = static_cast<uint32_t>( tmp_val );
266269
if( strval.size() >= 2 && val < 100 )
267270
continue;
268271
}catch(...)

src/SpecFile_cnf.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ namespace
170170
//duration in years is larger than an int32, divide by a million years
171171
if ( (duration / 31557600.0) > static_cast<double>(INT32_MAX) )
172172
{
173-
int32_t y_duration = static_cast<int32_t>(t_duration / 1e6);
173+
int32_t y_duration = SpecUtils::float_to_integral<int32_t>(t_duration / 1e6);
174174
const auto y_bytes = to_bytes(y_duration);
175175
std::copy(begin(y_bytes), end(y_bytes), begin(bytes));
176176
//set the flags
@@ -884,7 +884,7 @@ bool SpecFile::write_cnf( std::ostream &output, std::set<int> sample_nums,
884884
}
885885

886886
//TODO: implement converted shape calibration information into CNF files
887-
//shape calibration, just use the defualt values for NaI detectors if the type cotains any NaI, if not use Ge defaults
887+
//shape calibration, just use the default values for NaI detectors if the type cotains any NaI, if not use Ge defaults
888888
const string& detector_type = summed->detector_type();
889889
enter_CAM_value("SQRT", cnf_file, acqp_loc + 0x464, cam_type::cam_string);
890890
if(detector_type.find("NaI") == 0 || detector_type.find("nai") == 0 || detector_type.find("NAI") == 0)

src/SpecFile_gr135.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ bool SpecFile::write_binary_exploranium_gr130v0( std::ostream &output ) const
817817

818818
//Lets write to a buffer before writing to the stream to make things a
819819
// little easier in terms of keeping track of position.
820-
char buffer[561];
820+
uint8_t buffer[561];
821821
memset( buffer, 0, sizeof(buffer) );
822822

823823
memcpy( buffer + 0, "ZZZZ", 4 );
@@ -841,9 +841,9 @@ bool SpecFile::write_binary_exploranium_gr130v0( std::ostream &output ) const
841841
const date::hh_mm_ss<time_point_t::duration> time_of_day = date::make_time(t - t_as_days);
842842
const unsigned day = static_cast<unsigned>( t_ymd.day() );
843843
const unsigned month = static_cast<unsigned>( t_ymd.month() );
844-
const int hour = time_of_day.hours().count();
845-
const int mins = time_of_day.minutes().count();
846-
const int secs = time_of_day.seconds().count();
844+
const int hour = static_cast<int>( time_of_day.hours().count() );
845+
const int mins = static_cast<int>( time_of_day.minutes().count() );
846+
const int secs = static_cast<int>( time_of_day.seconds().count() );
847847

848848
buffer[7] = static_cast<uint8_t>(year);
849849
buffer[8] = static_cast<uint8_t>(month);
@@ -914,7 +914,7 @@ bool SpecFile::write_binary_exploranium_gr130v0( std::ostream &output ) const
914914

915915
buffer[559] = 0; //CHSUM check sum
916916

917-
output.write( buffer, 560 );
917+
output.write( (const char *)buffer, 560 );
918918

919919
++nwrote;
920920
}//for( size_t measn = 0; measn < nmeas; ++measn )
@@ -975,7 +975,7 @@ bool SpecFile::write_binary_exploranium_gr135v2( std::ostream &output ) const
975975

976976
//Lets write to a buffer before writing to the stream to make things a
977977
// little easier in terms of keeping track of position.
978-
char buffer[76+2*noutchannel] = { '\0' };
978+
uint8_t buffer[76+2*noutchannel] = { '\0' };
979979
memset( buffer, 0, sizeof(buffer) );
980980

981981
memcpy( buffer + 0, "ZZZZ", 4 );
@@ -1002,9 +1002,9 @@ bool SpecFile::write_binary_exploranium_gr135v2( std::ostream &output ) const
10021002
const date::hh_mm_ss<time_point_t::duration> time_of_day = date::make_time(t - t_as_days);
10031003
const unsigned day = static_cast<unsigned>( t_ymd.day() );
10041004
const unsigned month = static_cast<unsigned>( t_ymd.month() );
1005-
const int hour = time_of_day.hours().count();
1006-
const int mins = time_of_day.minutes().count();
1007-
const int secs = time_of_day.seconds().count();
1005+
const int hour = static_cast<int>( time_of_day.hours().count() );
1006+
const int mins = static_cast<int>( time_of_day.minutes().count() );
1007+
const int secs = static_cast<int>( time_of_day.seconds().count() );
10081008

10091009
buffer[13] = static_cast<uint8_t>(year - 2000);
10101010
buffer[14] = static_cast<uint8_t>(month);
@@ -1104,7 +1104,7 @@ bool SpecFile::write_binary_exploranium_gr135v2( std::ostream &output ) const
11041104

11051105
//2*nch+76 unsigned char CHSUM check sum
11061106

1107-
output.write( buffer, 76+2*noutchannel );
1107+
output.write( (const char *)buffer, 76+2*noutchannel );
11081108

11091109
++nwrote;
11101110
}//for( size_t measn = 0; measn < nmeas; ++measn )

src/SpecFile_n42.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -940,12 +940,12 @@ namespace
940940
//add_spectra_to_measurement_node_in_2012_N42_xml(...): Adds the given
941941
// spectra to the specified RadMeasurementNode. All measurements should
942942
// have the sample sample number, and the entries in calibid should
943-
// coorespond one to one to the entries in measurements.
943+
// correspond one to one to the entries in measurements.
944944
// If something drastically goes wrong, and an exception is thrown somewhere
945945
// this function will not throw, it will print an error to stderror and not
946946
// insert itself into the DOM; this is so this function is safe to call in
947947
// its own thread with no error handling. I expect this to never happen, so
948-
// I'm not bothing with any better error handling.
948+
// I'm not bothering with any better error handling.
949949

950950
void add_spectra_to_measurement_node_in_2012_N42_xml( ::rapidxml::xml_node<char> *RadMeasurement,
951951
const std::vector< std::shared_ptr<const SpecUtils::Measurement> > measurements,
@@ -1089,8 +1089,11 @@ void add_spectra_to_measurement_node_in_2012_N42_xml( ::rapidxml::xml_node<char>
10891089

10901090
for( size_t i = 0; i < measurements.size(); ++i )
10911091
{
1092+
assert( i < calibids.size() );
1093+
10921094
const size_t calibid = calibids[i];
1093-
const std::shared_ptr<const Measurement> m = measurements[i];
1095+
const shared_ptr<const Measurement> m = measurements[i];
1096+
assert( m );
10941097

10951098
char livetime[32], calibstr[32], spec_id_cstr[48];
10961099

@@ -1114,8 +1117,7 @@ void add_spectra_to_measurement_node_in_2012_N42_xml( ::rapidxml::xml_node<char>
11141117
//Probably shouldnt ever make it here.
11151118
snprintf( spec_id_cstr, sizeof(spec_id_cstr), "Sample%iDet%iSpectrum", m->sample_number(), m->detector_number() );
11161119
}
1117-
1118-
spec_id_cstr[sizeof(spec_id_cstr) - 1] = '0'; //jic
1120+
spec_id_cstr[sizeof(spec_id_cstr) - 1] = '\0'; //jic
11191121
string spec_idstr = spec_id_cstr;
11201122

11211123
// If this is a derived data Measurement, we will append derived data properties to the "id"

src/SpecFile_spc.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ bool SpecFile::write_ascii_spc( std::ostream &output,
10831083
// other file formats to SPC, but such is life
10841084
if(!res.nuclide_.empty() && !res.nuclide_type_.empty())
10851085
{
1086-
const string postfix = string("") + char('0' + i);
1086+
const string postfix = std::to_string(i);
10871087
output << pad_iaea_prefix( "Nuclide" + postfix ) << res.nuclide_ << "\r\n";
10881088
if( SpecUtils::istarts_with(res.remark_, "Strength ") )
10891089
output << pad_iaea_prefix( "Strength" + postfix ) << res.nuclide_ << "\r\n";
@@ -1093,7 +1093,7 @@ bool SpecFile::write_ascii_spc( std::ostream &output,
10931093
output << pad_iaea_prefix( "Confidence" + postfix ) << res.id_confidence_ << "\r\n";
10941094
}else if(!res.nuclide_.empty())
10951095
{
1096-
const string postfix = string("") + char('1' + i);
1096+
const string postfix = std::to_string(i + 1);
10971097
output << pad_iaea_prefix( "NuclideID" + postfix ) << res.nuclide_ << "\r\n";
10981098
}
10991099
}//
@@ -1392,6 +1392,7 @@ bool SpecFile::write_binary_spc( std::ostream &output,
13921392
case DetectorType::VerifinderNaI:
13931393
case DetectorType::VerifinderLaBr:
13941394
case DetectorType::KromekD3S:
1395+
case DetectorType::RadiaCode:
13951396
case DetectorType::Unknown:
13961397
defaultname = "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0";
13971398
break;

src/SpecFile_spe.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,13 @@ bool SpecFile::load_from_iaea( std::istream& istr )
808808
vector<float> parts;
809809
SpecUtils::split_to_floats( line.c_str(), line.size(), parts );
810810
if( parts.size() == 2 )
811-
bin_to_energy.push_back( make_pair(static_cast<int>(parts[0]),parts[1]) );
811+
{
812+
// Only add this point if the channel and energy are reasonable.
813+
const int bin = float_to_integral<int>(parts[0]);
814+
const float energy = parts[1];
815+
if( (bin >= 0) && (bin < 131072) && (energy >= 0.0f) && (energy < 3000000.0f) )
816+
bin_to_energy.emplace_back( bin, parts[1] );
817+
}
812818
}
813819
}//while( SpecUtils::safe_get_line( istr, line ) )
814820
}//if( file says how many entries to expect )
@@ -1058,10 +1064,10 @@ bool SpecFile::load_from_iaea( std::istream& istr )
10581064
continue;
10591065

10601066
int numresults = 0;
1061-
if( !toInt(analines[0], numresults) || numresults <= 0 )
1067+
if( !toInt(analines[0], numresults) || (numresults <= 0) )
10621068
continue;
10631069

1064-
if( (analines.size()-1) != numresults*4 )
1070+
if( (analines.size()-1) != (4*static_cast<size_t>(numresults)) )
10651071
{
10661072
string remark;
10671073
for( size_t i = 0; i < analines.size(); ++i )

src/StringAlgo.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,6 +1621,9 @@ std::wstring convert_from_utf8_to_utf16( const std::string &input )
16211621

16221622
std::string sequencesToBriefString( const std::set<int> &sample_numbers )
16231623
{
1624+
if( sample_numbers.empty() )
1625+
return "";
1626+
16241627
stringstream editVal;
16251628

16261629
int added = 0;

0 commit comments

Comments
 (0)