[med-svn] [SCM] aghermann branch, master, updated. 99b1d5a023eee9df74b0e0d6f894516fc79435ad

Andrei Zavada johnhommer at gmail.com
Sun Jul 7 23:03:50 UTC 2013


The following commit has been merged in the master branch:
commit b83af93e24cb3e1aa7eaf6252a1db472e2846fb1
Author: Andrei Zavada <johnhommer at gmail.com>
Date:   Sun Jun 9 19:45:49 2013 +0300

    replace ASPRINTF macro with a proper string agh::str::sasprintf()
    
    DEF_UNIQUE_CHARP found leaking, oh dear.

diff --git a/src/aghermann/expdesign/primaries.cc b/src/aghermann/expdesign/primaries.cc
index 20ae3b2..272e8c0 100644
--- a/src/aghermann/expdesign/primaries.cc
+++ b/src/aghermann/expdesign/primaries.cc
@@ -144,7 +144,7 @@ log_message( const char* fmt, ...)
 	va_list ap;
 	va_start (ap, fmt);
 
-	DEF_UNIQUE_CHARP(buf);
+	char *buf;
 	assert (vasprintf( &buf, fmt, ap) > 0);
 
 	_error_log += buf;
@@ -154,6 +154,8 @@ log_message( const char* fmt, ...)
 	} else
 		fputs( buf, stdout);
 
+	free( (void*)buf);
+
 	va_end (ap);
 }
 
@@ -525,23 +527,23 @@ export_all_modruns( const string& fname) const
 					for ( auto &R : RS.second )
 						if ( R.second.status & ach::CModelRun::modrun_tried ) {
 							auto& M = R.second;
-							DEF_UNIQUE_CHARP (extra);
+							string extra;
 							switch ( M.P().metric ) {
 							case metrics::TType::psd:
-								ASPRINTF( &extra, "%g-%g Hz", M.P().P.psd.freq_from, M.P().P.psd.freq_upto);
+								extra = agh::str::sasprintf( "%g-%g Hz", M.P().P.psd.freq_from, M.P().P.psd.freq_upto);
 								break;
 							case metrics::TType::swu:
-								ASPRINTF( &extra, "%g Hz", M.P().P.swu.f0);
+								extra = agh::str::sasprintf( "%g Hz", M.P().P.swu.f0);
 								break;
 							case metrics::TType::mc:
-								ASPRINTF( &extra, "%g Hz", M.P().P.mc.f0);
+								extra = agh::str::sasprintf( "%g Hz", M.P().P.mc.f0);
 								break;
 							default:
 								throw runtime_error ("What metric?");
 							}
 							fprintf( f, "# ----- Subject: %s;  Session: %s;  Channel: %s;  Metric: %s (%s)\n",
 								 M.subject(), M.session(), M.channel(),
-								 M.P().metric_name(), extra);
+								 M.P().metric_name(), extra.c_str());
 							t = ach::TTunable::rs;
 							do {
 								fprintf( f, "%g%s", M.tx[t] * ach::stock[t].display_scale_factor,
@@ -575,10 +577,11 @@ int
 agh::CExpDesign::
 purge_cached_profiles()
 {
-	DEF_UNIQUE_CHARP (b);
-	ASPRINTF( &b, "find '%s' \\( -name '.*.psd' -or -name '.*.mc' -or -name '.*.swu' \\) -delete",
-		  session_dir().c_str());
-	return system( b);
+	return system(
+		agh::str::sasprintf(
+			"find '%s' \\( -name '.*.psd' -or -name '.*.mc' -or -name '.*.swu' \\) -delete",
+			session_dir().c_str())
+		.c_str());
 }
 
 // Local Variables:
diff --git a/src/aghermann/expdesign/recording.cc b/src/aghermann/expdesign/recording.cc
index 56d3632..ba1d51b 100644
--- a/src/aghermann/expdesign/recording.cc
+++ b/src/aghermann/expdesign/recording.cc
@@ -50,15 +50,13 @@ string
 agh::SProfileParamSet::
 display_name() const
 {
-	DEF_UNIQUE_CHARP (_);
+	using agh::str::sasprintf;
 	switch ( metric ) {
-	case metrics::TType::psd: ASPRINTF( &_, "%s (%g-%g Hz)", metric_name(), P.psd.freq_from, P.psd.freq_upto); break;
-	case metrics::TType::swu: ASPRINTF( &_, "%s (%g Hz)",    metric_name(), P.swu.f0); break;
-	case metrics::TType::mc : ASPRINTF( &_, "%s (%g Hz)",    metric_name(), P.mc.f0); break;
-	default: ASPRINTF( &_, "(invalid metric: %d)", metric); break;
+	case metrics::TType::psd: return sasprintf( "%s (%g-%g Hz)", metric_name(), P.psd.freq_from, P.psd.freq_upto);
+	case metrics::TType::swu: return sasprintf( "%s (%g Hz)",    metric_name(), P.swu.f0);
+	case metrics::TType::mc : return sasprintf( "%s (%g Hz)",    metric_name(), P.mc.f0);
+	default: return sasprintf( "(invalid metric: %d)", metric);
 	}
-	string ret {_};
-	return ret;
 }
 
 string
diff --git a/src/aghermann/metrics/mc.cc b/src/aghermann/metrics/mc.cc
index 3e7524b..0181834 100644
--- a/src/aghermann/metrics/mc.cc
+++ b/src/aghermann/metrics/mc.cc
@@ -41,8 +41,7 @@ string
 metrics::mc::CProfile::
 fname_base() const
 {
-	DEF_UNIQUE_CHARP (_);
-	ASPRINTF( &_,
+	return agh::str::sasprintf(
 		  "%s.%s-%lu"
 		  ":%zu-%g_%g" "_%g" "_%g_%g",
 		  _using_F().filename(), _using_F().channel_by_id(_using_sig_no).name(),
@@ -52,21 +51,18 @@ fname_base() const
 		  Pp.iir_backpolate,
 		  Pp.mc_gain,
 		  Pp.f0fc, Pp.bandwidth);
-	string ret {_};
-	return ret;
 }
 
 string
 metrics::mc::CProfile::
 mirror_fname() const
 {
-	DEF_UNIQUE_CHARP (_);
-	string basename_dot = agh::fs::make_fname_base (_using_F().filename(), "", true);
-	ASPRINTF( &_,
+	return agh::str::sasprintf(
 		  "%s-%s-%lu"
 		  ":%zu-%g_%g" "_%g" "_%g_%g" "_%g_%g@%zu"
 		  ".mc",
-		  basename_dot.c_str(), _using_F().channel_by_id(_using_sig_no).name(),
+		  agh::fs::make_fname_base (_using_F().filename(), "", true).c_str(),
+		  _using_F().channel_by_id(_using_sig_no).name(),
 		  _using_F().dirty_signature( _using_sig_no),
 		  Pp.pagesize,
 		  Pp.scope,
@@ -75,8 +71,6 @@ mirror_fname() const
 		  Pp.f0fc, Pp.bandwidth,
 		  Pp.freq_from, Pp.freq_inc,
 		  sizeof(TFloat));
-	string ret {_};
-	return ret;
 }
 
 int
diff --git a/src/aghermann/metrics/psd.cc b/src/aghermann/metrics/psd.cc
index 80f0ecb..ccdb5fe 100644
--- a/src/aghermann/metrics/psd.cc
+++ b/src/aghermann/metrics/psd.cc
@@ -58,16 +58,13 @@ string
 metrics::psd::CProfile::
 fname_base() const
 {
-	DEF_UNIQUE_CHARP (_);
-	ASPRINTF( &_,
+	return agh::str::sasprintf(
 		  "%s.%s-%lu"
 		  ":%zu-%g-%c%c",
 		  _using_F().filename(), _using_F().channel_by_id(_using_sig_no).name(),
 		  _using_F().dirty_signature( _using_sig_no),
 		  Pp.pagesize, Pp.binsize,
 		  'a'+(char)Pp.welch_window_type, 'a'+(char)Pp.plan_type);
-	string ret {_};
-	return ret;
 }
 
 
@@ -76,19 +73,16 @@ string
 metrics::psd::CProfile::
 mirror_fname() const
 {
-	DEF_UNIQUE_CHARP (_);
-	string basename_dot = agh::fs::make_fname_base (_using_F().filename(), "", true);
-	ASPRINTF( &_,
+	return agh::str::sasprintf(
 		  "%s.%s-%lu"
 		  ":%zu-%g-%c%c@%zu"
 		  ".psd",
-		  basename_dot.c_str(), _using_F().channel_by_id(_using_sig_no).name(),
+		  agh::fs::make_fname_base (_using_F().filename(), "", true).c_str(),
+		  _using_F().channel_by_id(_using_sig_no).name(),
 		  _using_F().dirty_signature( _using_sig_no),
 		  Pp.pagesize, Pp.binsize,
 		  'a'+(char)Pp.welch_window_type, 'a'+(char)Pp.plan_type,
 		  sizeof(double));
-	string ret {_};
-	return ret;
 }
 
 
diff --git a/src/aghermann/metrics/swu.cc b/src/aghermann/metrics/swu.cc
index fb46599..effb9bc 100644
--- a/src/aghermann/metrics/swu.cc
+++ b/src/aghermann/metrics/swu.cc
@@ -40,15 +40,12 @@ string
 metrics::swu::CProfile::
 fname_base() const
 {
-	DEF_UNIQUE_CHARP (_);
-	ASPRINTF( &_,
+	return agh::str::sasprintf(
 		  "%s.%s-%lu"
 		  ":%zu-%g",
 		  _using_F().filename(), _using_F().channel_by_id(_using_sig_no).name(),
 		  _using_F().dirty_signature( _using_sig_no),
 		  Pp.pagesize, Pp.min_upswing_duration);
-	string ret {_};
-	return ret;
 }
 
 
@@ -56,18 +53,15 @@ string
 metrics::swu::CProfile::
 mirror_fname() const
 {
-	DEF_UNIQUE_CHARP (_);
-	string basename_dot = agh::fs::make_fname_base (_using_F().filename(), "", true);
-	ASPRINTF( &_,
+	return agh::str::sasprintf(
 		  "%s.%s-%lu"
 		  ":%zu-%g@%zu"
 		  ".swu",
-		  basename_dot.c_str(), _using_F().channel_by_id(_using_sig_no).name(),
+		  agh::fs::make_fname_base (_using_F().filename(), "", true).c_str(),
+		  _using_F().channel_by_id(_using_sig_no).name(),
 		  _using_F().dirty_signature( _using_sig_no),
 		  Pp.pagesize, Pp.min_upswing_duration,
 		  sizeof(TFloat));
-	string ret {_};
-	return ret;
 }
 
 
diff --git a/src/aghermann/patterns/patterns.cc b/src/aghermann/patterns/patterns.cc
index f8101c5..66768e8 100644
--- a/src/aghermann/patterns/patterns.cc
+++ b/src/aghermann/patterns/patterns.cc
@@ -45,7 +45,6 @@ load_pattern( const char* fname) throw(invalid_argument)
 	SPattern<TFloat>
 		P;
 
-	DEF_UNIQUE_CHARP (buf);
 	FILE *fd = fopen( fname, "r");
 	if ( fd ) {
 		size_t	full_sample;
@@ -72,46 +71,48 @@ load_pattern( const char* fname) throw(invalid_argument)
 			     P.context_before > P.samplerate * 2 ||
 			     P.context_after > P.samplerate * 2 ||
 			     not P.Pp.sane() ) {
-				ASPRINTF( &buf, "load_pattern(\"%s\"): bogus data in header; "
-					  "removing file", fname);
-				fprintf( stderr, "%s\n", buf);
+				string msg = agh::str::sasprintf(
+					"load_pattern(\"%s\"): bogus data in header; removing file",
+					fname);
+				fprintf( stderr, "%s\n", msg.c_str());
 				P.thing.resize( 0);
 				fclose( fd);
 				unlink( fname);
-				throw invalid_argument (buf);
+				throw invalid_argument (msg);
 			}
 
 			P.thing.resize( full_sample);
 			for ( size_t i = 0; i < full_sample; ++i ) {
 				double d;
 				if ( fscanf( fd, "%la", &d) != 1 ) {
-					ASPRINTF( &buf, "load_pattern(\"%s\"): short read at sample %zu; "
-						  "removing file", fname, i);
-					fprintf( stderr, "%s\n", buf);
+					string msg = agh::str::sasprintf(
+						"load_pattern(\"%s\"): short read at sample %zu; removing file",
+						fname, i);
+					fprintf( stderr, "%s\n", msg.c_str());
 					P.thing.resize( 0);
 					fclose( fd);
 					unlink( fname);
-					throw invalid_argument (buf);
+					throw invalid_argument (msg);
 				} else
 					P.thing[i] = d;
 			}
 
 		} else {
 			P.thing.resize( 0);
-			ASPRINTF( &buf, "load_pattern(\"%s\"): bad header, so removing file", fname);
-			fprintf( stderr, "%s\n", buf);
+			string msg = agh::str::sasprintf( "load_pattern(\"%s\"): bad header, so removing file", fname);
+			fprintf( stderr, "%s\n", msg.c_str());
 			P.thing.resize( 0);
 			fclose( fd);
 			unlink( fname);
-			throw invalid_argument (buf);
+			throw invalid_argument (msg);
 		}
 
 		fclose( fd);
 
 	} else {
-		ASPRINTF( &buf, "Failed to open pattern %s", fname);
-		fprintf( stderr, "%s\n", buf);
-		throw invalid_argument (buf);
+		string msg = agh::str::sasprintf( "Failed to open pattern %s", fname);
+		fprintf( stderr, "%s\n", msg.c_str());
+		throw invalid_argument (msg);
 	}
 
 	printf( "loaded pattern in %s\n", fname);
diff --git a/src/aghermann/ui/libcommon.cc b/src/aghermann/ui/libcommon.cc
index fb18367..69def52 100644
--- a/src/aghermann/ui/libcommon.cc
+++ b/src/aghermann/ui/libcommon.cc
@@ -287,9 +287,9 @@ void
 aghui::
 pop_ok_message( GtkWindow *parent,
 		const char* primary_text,
-		const char *fmt, ...)
+		const char* fmt, ...)
 {
-	GtkWidget *msg =
+	auto W = (GtkMessageDialog*)
 		gtk_message_dialog_new_with_markup(
 			parent,
 			(GtkDialogFlags)(GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT),
@@ -297,19 +297,20 @@ pop_ok_message( GtkWindow *parent,
 			GTK_BUTTONS_OK,
 			primary_text, NULL);
 
-	DEF_UNIQUE_CHARP (_);
 	if ( fmt ) {
 		va_list ap;
 		va_start (ap, fmt);
 
+		char *_;
 		assert (vasprintf( &_, fmt, ap) > 0);
 		va_end (ap);
-		gtk_message_dialog_format_secondary_markup( (GtkMessageDialog*)msg, "%s", _);
+		gtk_message_dialog_format_secondary_markup( W, "%s", _);
+		free( (void*)_);
 	}
 
-	gtk_dialog_run( (GtkDialog*)msg);
+	gtk_dialog_run( (GtkDialog*)W);
 
-	gtk_widget_destroy( msg);
+	gtk_widget_destroy( (GtkWidget*)W);
 }
 
 
diff --git a/src/aghermann/ui/mw/mw.cc b/src/aghermann/ui/mw/mw.cc
index 4806019..4517173 100644
--- a/src/aghermann/ui/mw/mw.cc
+++ b/src/aghermann/ui/mw/mw.cc
@@ -323,7 +323,7 @@ load_artifact_detection_profiles()
 	if ( domien ) {
 		while ( !feof (domien) ) {
 			metrics::mc::SArtifactDetectionPP P;
-			DEF_UNIQUE_CHARP (_);
+			char *name = nullptr;
 			int int_estimate_E, int_use_range;
 // at least gcc 4.7.2 fails to recognize "%as" (dynamic allocation), so
 #pragma GCC diagnostic ignored "-Wformat"
@@ -331,7 +331,7 @@ load_artifact_detection_profiles()
 			if ( 16 ==
 			     fscanf( domien, "%a[^\n]\n%la  %la %la  %la %la %la  %la %la  %la %la %la "
 				     "%zu %zu %d %d",
-				     &_,
+				     &name,
 				     &P.scope,
 				     &P.upper_thr, &P.lower_thr,
 				     &P.f0, &P.fc, &P.bandwidth,
@@ -341,12 +341,15 @@ load_artifact_detection_profiles()
 				     &P.smooth_side,
 				     &int_estimate_E,
 				     &int_use_range) ) {
+
 				P.estimate_E = (bool)int_estimate_E;
 				P.use_range = (bool)int_use_range;
-				global_artifact_detection_profiles[_] = P;
+				global_artifact_detection_profiles[name] = P;
 #pragma GCC diagnostic pop
 			} else
 				break;
+
+			free( (void*)name);
 		}
 		fclose( domien);
 	}
@@ -565,10 +568,10 @@ show_changelog()
 
 void
 aghui::SExpDesignUI::
-sb_message( const char* msg) const
+sb_message( const string& msg) const
 {
 	gtk_statusbar_pop( sbMainStatusBar, sbMainContextIdGeneral);
-	gtk_statusbar_push( sbMainStatusBar, sbMainContextIdGeneral, msg);
+	gtk_statusbar_push( sbMainStatusBar, sbMainContextIdGeneral, msg.c_str());
 }
 
 void
@@ -586,9 +589,7 @@ sb_main_progress_indicator( const string& current,
 			    const size_t n, const size_t i,
 			    const aghui::TGtkRefreshMode mode)
 {
-	DEF_UNIQUE_CHARP (b);
-	ASPRINTF( &b, "(%zu of %zu) %s", i, n, current.c_str());
-	sb_message( b);
+	sb_message( agh::str::sasprintf( "(%zu of %zu) %s", i, n, current.c_str()));
 
 	switch ( mode ) {
 	case TGtkRefreshMode::gtk:
diff --git a/src/aghermann/ui/mw/mw.hh b/src/aghermann/ui/mw/mw.hh
index 0e3b2ac..27cb582 100644
--- a/src/aghermann/ui/mw/mw.hh
+++ b/src/aghermann/ui/mw/mw.hh
@@ -329,7 +329,7 @@ class SExpDesignUI
 	int	ctl_params0_siman_params_t_initial_exponent;
 
       // status bar bits
-	void sb_message( const char*) const;
+	void sb_message( const string&) const;
 	void sb_clear() const;
 	void sb_main_progress_indicator( const string&, size_t n, size_t i,
 					 aghui::TGtkRefreshMode);
diff --git a/src/aghermann/ui/sf/d/patterns-profiles.cc b/src/aghermann/ui/sf/d/patterns-profiles.cc
index 101847a..7ecb52c 100644
--- a/src/aghermann/ui/sf/d/patterns-profiles.cc
+++ b/src/aghermann/ui/sf/d/patterns-profiles.cc
@@ -85,37 +85,25 @@ const char*
 string
 make_system_patterns_location()
 {
-	DEF_UNIQUE_CHARP (buf);
-	ASPRINTF( &buf, "%s/patterns", PACKAGE_DATADIR);
-	string ret (buf);
-	return ret;
+	return agh::str::sasprintf( "%s/patterns", PACKAGE_DATADIR);
 }
 
 string
 make_user_patterns_location()
 {
-	DEF_UNIQUE_CHARP (buf);
-	ASPRINTF( &buf, "%s/.local/share/aghermann/patterns", getenv("HOME"));
-	string ret (buf);
-	return ret;
+	return agh::str::sasprintf( "%s/.local/share/aghermann/patterns", getenv("HOME"));
 }
 
 string
 make_experiment_patterns_location( const agh::CExpDesign& ED)
 {
-	DEF_UNIQUE_CHARP (buf);
-	ASPRINTF( &buf, "%s/.patterns", ED.session_dir().c_str());
-	string ret (buf);
-	return ret;
+	return agh::str::sasprintf( "%s/.patterns", ED.session_dir().c_str());
 }
 
 string
 make_subject_patterns_location( const agh::CExpDesign& ED, const agh::CSubject& J)
 {
-	DEF_UNIQUE_CHARP (buf);
-	ASPRINTF( &buf, "%s/.patterns", ED.subject_dir( J).c_str());
-	string ret (buf);
-	return ret;
+	return agh::str::sasprintf( "%s/.patterns", ED.subject_dir( J).c_str());
 }
 
 
diff --git a/src/aghermann/ui/sf/d/phasediff.cc b/src/aghermann/ui/sf/d/phasediff.cc
index fd6b6b0..2c674d8 100644
--- a/src/aghermann/ui/sf/d/phasediff.cc
+++ b/src/aghermann/ui/sf/d/phasediff.cc
@@ -108,11 +108,12 @@ preselect_channel( GtkComboBox *cbox, const string& ch)
 	gboolean valid;
 	valid = gtk_tree_model_get_iter_first( model, &iter);
 	while ( valid ) {
-		DEF_UNIQUE_CHARP (entry);
-
+		char *entry;
 		gtk_tree_model_get( model, &iter,
 				    0, &entry,
 				    -1);
+		unique_ptr<void,void(*)(void*)> entry_deleter (entry, free);
+
 		if ( entry == ch ) {
 			gtk_combo_box_set_active_iter( cbox, &iter);
 			return;
diff --git a/src/common/fs.hh b/src/common/fs.hh
index bfc3f90..3b73b23 100644
--- a/src/common/fs.hh
+++ b/src/common/fs.hh
@@ -79,12 +79,13 @@ exists_and_is_writable( const T& _dir)
 
 template<class T>
 int
-mkdir_with_parents( const T& _dir)
+mkdir_with_parents( const T& dir)
 {
-	string dir (_dir);
-	DEF_UNIQUE_CHARP(_);
-	ASPRINTF( &_, "mkdir -p '%s'", dir.c_str());
-	return system( _);
+	return system(
+		agh::str::sasprintf(
+			"mkdir -p '%s'",
+			string (dir).c_str())
+		.c_str());
 }
 
 
diff --git a/src/common/lang.hh b/src/common/lang.hh
index 748f84e..2a7a98a 100644
--- a/src/common/lang.hh
+++ b/src/common/lang.hh
@@ -35,17 +35,19 @@ using namespace std;
 #define unlikely(x)	__builtin_expect (!!(x), 0)
 
 
-#define	DEF_UNIQUE_CHARP(p)				\
-	char* p = nullptr;				\
-	std::unique_ptr<void,void(*)(void*)> p##_pp(p,free);
+#define ASPRINTF(...) \
+	assert (asprintf(__VA_ARGS__) > 0)
+
+
+#define	MAKE_UNIQUE_CHARP(p)				\
+	unique_ptr<void,void(*)(void*)> p##_pp(p,free);
+
 
 #define DELETE_DEFAULT_METHODS(T)		\
 	T () = delete;				\
 	T (const T&) = delete;			\
 	void operator=( const T&) = delete;
 
-#define ASPRINTF(...) \
-	assert (asprintf(__VA_ARGS__) > 0)
 
 typedef unsigned long hash_t;
 
diff --git a/src/common/libcommon.cc b/src/common/libcommon.cc
index 5800f97..9896dff 100644
--- a/src/common/libcommon.cc
+++ b/src/common/libcommon.cc
@@ -15,6 +15,7 @@
 #include <string>
 #include <list>
 
+#include <stdarg.h>
 #include <unistd.h>
 #include <errno.h>
 #include <wchar.h>
@@ -60,6 +61,21 @@ pad( const string& r0, size_t to)
 
 
 
+string
+agh::str::
+sasprintf( const char* fmt, ...)
+{
+	char *_;
+	va_list ap;
+	va_start (ap, fmt);
+	assert (vasprintf( &_, fmt, ap) > 0);
+	va_end (ap);
+
+	string ret {_};
+	free( (void*)_);
+	return ret;
+}
+
 
 
 list<string>
@@ -173,25 +189,17 @@ dhms( double seconds, int dd)
 		d = (int)seconds/60/60/24 % (60*60*24);
 	double	f = seconds - floor(seconds);
 
-	DEF_UNIQUE_CHARP (f_);
-	if ( dd == 0 )
-		f_ = strdup( "");
-	else
-		ASPRINTF( &f_, ".%0*d", dd, (int)(f*pow(10, dd)));
-
-	DEF_UNIQUE_CHARP (_);
-
-	if ( d > 0 )
-		ASPRINTF( &_, "%dd %dh %dm %d%ss", d, h, m, s, f_);
-	else if ( h > 0 )
-		ASPRINTF( &_, "%dh %dm %d%ss", h, m, s, f_);
-	else if ( m > 0 )
-		ASPRINTF( &_, "%dm %d%ss", m, s, f_);
-	else
-		ASPRINTF( &_, "%d%ss", s, f_);
-
-	string ret (_);
-	return ret;
+	using agh::str::sasprintf;
+	string	f_ = ( dd == 0 )
+		? ""
+		: sasprintf( ".%0*d", dd, (int)(f*pow(10, dd)));
+	return ( d > 0 )
+		? sasprintf( "%dd %dh %dm %d%ss", d, h, m, s, f_.c_str())
+		: ( h > 0 )
+		? sasprintf( "%dh %dm %d%ss", h, m, s, f_.c_str())
+		: ( m > 0 )
+		? sasprintf( "%dm %d%ss", m, s, f_.c_str())
+		: sasprintf( "%d%ss", s, f_.c_str());
 }
 
 string
@@ -208,18 +216,12 @@ dhms_colon( double seconds, int dd)
 		d = (int)seconds/60/60/24 % (60*60*24);
 	double	f = seconds - floor(seconds);
 
-	DEF_UNIQUE_CHARP (f_);
-	if ( dd == 0 )
-		f_ = strdup( "");
-	else
-		ASPRINTF( &f_, ".%0*d", dd, (int)(f*pow(10, dd)));
+	using agh::str::sasprintf;
+	string	f_ = ( dd == 0 )
+		? ""
+		: sasprintf( ".%0*d", dd, (int)(f*pow(10, dd)));
 
-	DEF_UNIQUE_CHARP (_);
-
-	ASPRINTF( &_, "%dd %02d:%02d:%02d%ss", d, h, m, s, f_);
-
-	string ret (_);
-	return ret;
+	return sasprintf( "%dd %02d:%02d:%02d%ss", d, h, m, s, f_.c_str());
 }
 
 
diff --git a/src/common/string.hh b/src/common/string.hh
index f678642..029c19e 100644
--- a/src/common/string.hh
+++ b/src/common/string.hh
@@ -28,6 +28,9 @@ namespace str {
 string trim( const string& r0);
 string pad( const string& r0, size_t to);
 
+string sasprintf( const char* fmt, ...) __attribute__ ((format (printf, 1, 2)));
+
+
 
 template <typename C>
 string
diff --git a/src/libsigfile/source-base.cc b/src/libsigfile/source-base.cc
index b0475fa..9f2405c 100644
--- a/src/libsigfile/source-base.cc
+++ b/src/libsigfile/source-base.cc
@@ -108,10 +108,13 @@ unsigned long
 sigfile::SFilterPack::
 dirty_signature() const
 {
-	DEF_UNIQUE_CHARP (tmp);
+//	DEF_UNIQUE_CHARP (tmp);
+	char *tmp;
 	ASPRINTF( &tmp, "%g%d%g%d%d",
 		  low_pass_cutoff, low_pass_order, high_pass_cutoff, high_pass_order, (int)notch_filter);
-	return hash<std::string>() (tmp);
+	string t2 {tmp};
+	free( tmp);
+	return hash<string>() (t2);
 }
 
 
diff --git a/src/tools/edfcat.cc b/src/tools/edfcat.cc
index 8887b30..2621170 100644
--- a/src/tools/edfcat.cc
+++ b/src/tools/edfcat.cc
@@ -192,10 +192,8 @@ list<pair<sigfile::SChannel, size_t>>
 make_channel_headers_for_CEDFFile( size_t n, const char *fmt, size_t samplerate)
 {
 	list<pair<sigfile::SChannel, size_t>> ret;
-	for ( size_t i = 0; i < n; ++i ) {
-		DEF_UNIQUE_CHARP (_);
-		ret.emplace_back( (ASPRINTF( &_, fmt, i), string (_)), samplerate);
-	}
+	for ( size_t i = 0; i < n; ++i )
+		ret.emplace_back( agh::str::sasprintf( fmt, i), samplerate);
 	return ret;
 }
 
@@ -275,12 +273,8 @@ exec_convert( const SOperation::SObject& obj)
       // read data
 	{
 		ifstream ifs (obj.c_str());
-		if ( not ifs.good() ) {
-			DEF_UNIQUE_CHARP (_);
-			if ( asprintf( &_, "Convert: Couldn't open file %s", obj.c_str()) )
-				;
-			throw runtime_error (_);
-		}
+		if ( not ifs.good() )
+			throw runtime_error (agh::str::sasprintf( "Convert: Couldn't open file %s", obj.c_str()) );
 
 		string linebuf;
 		// figure # of fields
@@ -369,10 +363,10 @@ exec_prune( const SOperation::SObject& obj)
 	list<pair<sigfile::SChannel, size_t>> selected_channels;
 	for ( auto& select_this : obj.channels ) {
 		if ( select_this >= F.n_channels() ) {
-			DEF_UNIQUE_CHARP (_);
-			ASPRINTF( &_, "Prune: Requested channel #%zu (1-based) in file %s which only has %zu channel(s)",
-				  select_this, F.filename(), F.n_channels());
-			throw invalid_argument (_);
+			throw invalid_argument
+				(agh::str::sasprintf(
+					"Prune: Requested channel #%zu (1-based) in file %s which only has %zu channel(s)",
+					select_this, F.filename(), F.n_channels()));
 		}
 		string label (F[select_this].header.label, 16);
 //		strncpy( &label[0], F[select_this].header.label, 16);

-- 
Sleep experiment manager



More information about the debian-med-commit mailing list