[med-svn] [aghermann] 16/85: SF: patterns fixes and improvements

andrei zavada hmmr-guest at alioth.debian.org
Thu Sep 26 23:46:24 UTC 2013


This is an automated email from the git hooks/post-receive script.

hmmr-guest pushed a commit to branch master
in repository aghermann.

commit d9db86c5537166be13644af898b410520615db2c
Author: Andrei Zavada <johnhommer at gmail.com>
Date:   Tue Sep 10 02:21:07 2013 +0300

    SF: patterns fixes and improvements
    
    * load patterns from symlinked files;
    * rename, not delete, bad files in various pattern locations;
    * regression fixes.
---
 upstream/ChangeLog                                 |    4 +
 upstream/src/aghermann/patterns/patterns.cc        |  180 ++++++++++----------
 upstream/src/aghermann/patterns/patterns.hh        |    7 +-
 upstream/src/aghermann/patterns/patterns.ii        |    2 +-
 .../src/aghermann/ui/sf/d/patterns-profiles.cc     |    4 +-
 .../src/aghermann/ui/sf/d/patterns-profiles_cb.cc  |    6 +-
 6 files changed, 110 insertions(+), 93 deletions(-)

diff --git a/upstream/ChangeLog b/upstream/ChangeLog
index db22760..a12124b 100644
--- a/upstream/ChangeLog
+++ b/upstream/ChangeLog
@@ -1,5 +1,9 @@
 v.0.9.2 (2013-xx-xx)
 	* Separate upstream tree from various distribution-related files.
+	* SF: trivial optimisation in pattern search.
+	* SF: load patterns from symlinked files.
+	* SF: don't blindly delete files in various pattern locations,
+	  just append ~ to their file names.
 
 v.0.9.1.1 (2013-07-24)
 	* Fix 32-bit builds where sizeof(size_t) != sizeof(unsigned long).
diff --git a/upstream/src/aghermann/patterns/patterns.cc b/upstream/src/aghermann/patterns/patterns.cc
index b4289d9..2062d51 100644
--- a/upstream/src/aghermann/patterns/patterns.cc
+++ b/upstream/src/aghermann/patterns/patterns.cc
@@ -34,87 +34,6 @@ template int CPatternTool<TFloat>::do_search( const valarray<TFloat>&, size_t);
 namespace agh {
 namespace pattern {
 
-template <>
-int
-agh::pattern::SPattern<TFloat>::
-load( const string& fname_)
-{
-        const auto& fname = fname_.c_str();
-
-        FILE *fd = fopen( fname, "r");
-        if ( fd ) {
-                size_t  full_sample;
-                double  t1, t2, t3, t4;
-                if ( fscanf( fd,
-                             "%lg  %u %lg %lg  %lg %lg %u"
-                             " %lg %lg %lg %lg"
-                             " %zu %zu %zu %zu\n"
-                             "--DATA--\n",
-                             &Pp.env_scope,
-                             &Pp.bwf_order, &Pp.bwf_ffrom, &Pp.bwf_fupto,
-                             &Pp.dzcdf_step, &Pp.dzcdf_sigma, &Pp.dzcdf_smooth,
-                             &t1, &t2, &t3, &t4,
-                             &samplerate, &context.first, &context.second,
-                             &full_sample) == 15 ) {
-
-                        criteria = CMatch<float>(t1, t2, t3, t4);
-
-                        if ( samplerate == 0 || samplerate > 4096 ||
-                             full_sample == 0 || full_sample > samplerate * 10 ||
-                             context.first > samplerate * 2 ||
-                             context.second > samplerate * 2 ||
-                             not Pp.is_sane() ) {
-                                string msg = agh::str::sasprintf(
-                                        "load_pattern(\"%s\"): bogus data in header; removing file",
-                                        fname);
-                                fprintf( stderr, "%s\n", msg.c_str());
-                                thing.resize( 0);
-                                fclose( fd);
-                                unlink( fname);
-                                return -3;
-                        }
-
-                        thing.resize( full_sample);
-                        for ( size_t i = 0; i < full_sample; ++i ) {
-                                double d;
-                                if ( fscanf( fd, "%la", &d) != 1 ) {
-                                        string msg = agh::str::sasprintf(
-                                                "load_pattern(\"%s\"): short read at sample %zu; removing file",
-                                                fname, i);
-                                        fprintf( stderr, "%s\n", msg.c_str());
-                                        thing.resize( 0);
-                                        fclose( fd);
-                                        unlink( fname);
-                                        throw invalid_argument (msg);
-                                } else
-                                        thing[i] = d;
-                        }
-
-                } else {
-                        thing.resize( 0);
-                        string msg = agh::str::sasprintf( "load_pattern(\"%s\"): bad header, so removing file", fname);
-                        fprintf( stderr, "%s\n", msg.c_str());
-                        fclose( fd);
-                        unlink( fname);
-                        return -2;
-                }
-
-                fclose( fd);
-
-        } else {
-                string msg = agh::str::sasprintf( "Failed to open pattern %s", fname);
-                fprintf( stderr, "%s\n", msg.c_str());
-                return -1;
-        }
-
-        printf( "loaded pattern in %s\n", fname);
-        saved = true;
-        name = agh::str::tokens( fname, "/").back();
-        path = fname;
-
-        return 0;
-}
-
 
 template <>
 int
@@ -168,6 +87,87 @@ save()
 }
 
 
+
+template <>
+int
+agh::pattern::SPattern<TFloat>::
+load( const string& fname_)
+{
+        string msg;
+        using agh::str::sasprintf;
+
+        const char* fname = fname_.c_str();
+
+        FILE *fd = NULL;
+
+        try {
+                {
+                        struct stat attr;
+                        if ( 0 != lstat( fname, &attr) ||!S_ISREG (attr.st_mode) )
+                                return -1;
+                }
+
+                if ( !(fd = fopen( fname, "r")) )
+                        throw invalid_argument ("Failed to open");
+
+                size_t  full_sample;
+                double  t1, t2, t3, t4;
+                if ( 15 != fscanf( fd,
+                                   "%lg  %u %lg %lg  %lg %lg %u"
+                                   " %lg %lg %lg %lg"
+                                   " %zu %zu %zu %zu\n"
+                                   "--DATA--\n",
+                                   &Pp.env_scope,
+                                   &Pp.bwf_order, &Pp.bwf_ffrom, &Pp.bwf_fupto,
+                                   &Pp.dzcdf_step, &Pp.dzcdf_sigma, &Pp.dzcdf_smooth,
+                                   &t1, &t2, &t3, &t4,
+                                   &samplerate, &context.first, &context.second,
+                                   &full_sample) ) {
+                        throw invalid_argument ("Bogus data in header");
+                }
+
+                criteria = CMatch<float>(t1, t2, t3, t4);
+
+                if ( samplerate == 0 || samplerate > 4096 ||
+                     full_sample == 0 || full_sample > samplerate * 10 ||
+                     context.first > samplerate * 2 ||
+                     context.second > samplerate * 2 ||
+                     not Pp.is_sane() )
+                        throw invalid_argument ("Inconsistent data in header");
+
+                thing.resize( full_sample);
+                for ( size_t i = 0; i < full_sample; ++i ) {
+                        double d;
+                        if ( fscanf( fd, "%la", &d) != 1 )
+                                throw invalid_argument (sasprintf( "short read at sample %zu", i));
+                        thing[i] = d;
+                }
+
+                fclose( fd);
+
+                printf( "loaded pattern in %s\n", fname);
+                saved = true;
+                name = agh::str::tokens( fname, "/").back();
+                path = fname;
+
+                return 0;
+
+        } catch (invalid_argument& msg) {
+
+                fprintf( stderr, "load_pattern(\"%s\"): %s\n", fname, msg.what());
+                if ( fd )
+                        fclose( fd);
+
+                if (0 == rename( fname, (string(fname) + "~").c_str()))
+                        fprintf( stderr, "load_pattern(\"%s\"): file renamed as *~\n", fname);
+                else
+                        fprintf( stderr, "load_pattern(\"%s\"): tried, but failed, to rename as *~\n", fname);
+
+                return -2;
+        }
+}
+
+
 template <>
 int
 SPattern<TFloat>::
@@ -199,17 +199,23 @@ load_patterns_from_location<TFloat>( const string& loc, agh::TExpDirLevel origin
 
         if ( total != -1 ) {
                 for ( int i = 0; i < total; ++i ) {
-                        try {
-                                ret.emplace_back( loc + '/' + eps[i]->d_name);
-                                ret.back().origin = origin;
-                        } catch (invalid_argument& ex) {
-                                ;
-                        }
+                        const string fname = loc + '/' + eps[i]->d_name;
+                        struct stat attr;
+                        if ( 0 == lstat( fname.c_str(), &attr) && S_ISREG (attr.st_mode) &&
+                             '~' != fname.back() )
+                                try {
+                                        ret.emplace_back( fname);
+                                        ret.back().origin = origin;
+                                } catch (invalid_argument& ex) {
+                                        ;
+                                }
+
                         free( eps[i]);
                 }
                 free( (void*)eps);
         }
 
+        printf("found %zu patterns in %s\n", ret.size(), agh::exp_dir_level_s(origin));
         return move(ret);
 }
 
diff --git a/upstream/src/aghermann/patterns/patterns.hh b/upstream/src/aghermann/patterns/patterns.hh
index 629ac26..cd7a2a8 100644
--- a/upstream/src/aghermann/patterns/patterns.hh
+++ b/upstream/src/aghermann/patterns/patterns.hh
@@ -225,7 +225,12 @@ struct SPattern {
 
         explicit SPattern<T> (const string& fname)
                 {
-                        load( fname);
+                        if (load( fname))
+                                throw invalid_argument (string("Bad pattern in ") + fname);
+                }
+       ~SPattern<T> ()
+                {
+                        save();
                 }
 
         SPattern&
diff --git a/upstream/src/aghermann/patterns/patterns.ii b/upstream/src/aghermann/patterns/patterns.ii
index 9b0c704..eac2a20 100644
--- a/upstream/src/aghermann/patterns/patterns.ii
+++ b/upstream/src/aghermann/patterns/patterns.ii
@@ -26,7 +26,7 @@ do_search( const valarray<T>& fenv_l,
 {
         size_t fsize = ftarget_freq.size();
         if ( inc == 0 || inc > fsize ) {
-                fprintf( stderr, "%s: bad search increment: %zu\n", __FUNCTION__, inc);
+                fprintf( stderr, "bad search increment: %zu\n", inc);
                 return -1;
         }
 
diff --git a/upstream/src/aghermann/ui/sf/d/patterns-profiles.cc b/upstream/src/aghermann/ui/sf/d/patterns-profiles.cc
index 95f67ec..f92f936 100644
--- a/upstream/src/aghermann/ui/sf/d/patterns-profiles.cc
+++ b/upstream/src/aghermann/ui/sf/d/patterns-profiles.cc
@@ -143,7 +143,9 @@ populate_combo()
         if ( not patterns.empty() ) {
                 GtkTreeIter iter, current_pattern_iter;
                 for ( auto I = patterns.begin(); I != patterns.end(); ++I ) {
-                        gtk_list_store_append( mSFFDPatterns, &iter);
+                        gtk_list_store_append(
+                                mSFFDPatterns,
+                                &iter);
                         gtk_list_store_set(
                                 mSFFDPatterns, &iter,
                                 0, snprintf_buf( "%s %s", agh::exp_dir_level_s(I->origin), I->name.c_str()),
diff --git a/upstream/src/aghermann/ui/sf/d/patterns-profiles_cb.cc b/upstream/src/aghermann/ui/sf/d/patterns-profiles_cb.cc
index baff403..457b737 100644
--- a/upstream/src/aghermann/ui/sf/d/patterns-profiles_cb.cc
+++ b/upstream/src/aghermann/ui/sf/d/patterns-profiles_cb.cc
@@ -69,9 +69,8 @@ bSFFDProfileSave_clicked_cb(
 
                 // replace unnamed, else make a duplicate
                 auto& P = (FD.current_pattern->origin == agh::TExpDirLevel::transient)
-                        ? *FD.current_pattern
-                        : (FD.patterns.insert(FD.current_pattern, *FD.current_pattern),
-                           *(++FD.current_pattern));
+                        ? * FD.current_pattern
+                        : *(FD.current_pattern = FD.patterns.insert(FD.current_pattern, *FD.current_pattern));
 
                 P.name = gtk_entry_get_text( FD.eSFFDPatternSaveName);
 
@@ -88,6 +87,7 @@ bSFFDProfileSave_clicked_cb(
                                 TExpDirLevel::user,
                                 pattern::make_user_location());
 
+                P.path.append( "/").append( P.name);
                 P.saved = false;
 
                 FD.populate_combo();

-- 
Alioth's /git/debian-med/git-commit-notice on /srv/git.debian.org/git/debian-med/aghermann.git



More information about the debian-med-commit mailing list