From 8b170f11c72096275d10b5fbcfc7dc66c1bfbb39 Mon Sep 17 00:00:00 2001
From: "Joachim Wuttke (home)" <j.wuttke@fz-juelich.de>
Date: Sat, 27 Oct 2012 13:33:48 +0200
Subject: [PATCH] FSel now hidden in mem.cpp; mem.h publishes sel_...
 functions. Repaired bug introduced on 26oct: by default, zentries are
 unprotected

---
 pub/src/commands.cpp |  10 +-
 pub/src/frida2.cpp   |   6 +-
 pub/src/jsel.cpp     |   3 +-
 pub/src/list.cpp     |  56 +++---
 pub/src/list.h       |   1 +
 pub/src/mem.cpp      | 439 ++++++++++++++++++-------------------------
 pub/src/mem.h        |  34 ++--
 pub/src/plot.cpp     |   4 +-
 pub/src/zentry.cpp   |   4 +
 pub/src/zentry.h     |   1 +
 10 files changed, 234 insertions(+), 324 deletions(-)

diff --git a/pub/src/commands.cpp b/pub/src/commands.cpp
index 50830094..0aa7c942 100644
--- a/pub/src/commands.cpp
+++ b/pub/src/commands.cpp
@@ -46,6 +46,9 @@ bool fridaCommand( string cmd )
         cmd.erase(cmd.size()-1,1);
     }
 
+    // If there are new files, make them new default
+    NOlm::sel_collect_begin();
+
     if (cmd == "h" || cmd == "?") {
         cout <<
             "This is Frida, version " VERSION "\n"
@@ -364,9 +367,9 @@ bool fridaCommand( string cmd )
             "  kn    next\n"
             "  kv    previous\n";
     } else if (cmd == "kn") {
-        NOlm::SEL.increment( +1 );
+        NOlm::sel_increment( +1 );
     } else if (cmd == "kv") {
-        NOlm::SEL.increment( -1 );
+        NOlm::sel_increment( -1 );
 
     } else if (cmd == "m") {
         cout << "Commands to manipulate data:\n"
@@ -565,5 +568,8 @@ bool fridaCommand( string cmd )
         return false; // command not found
     }
 
+    // If there are new files, make them new default
+    NOlm::sel_collect_end();
+
     return true;
 }
diff --git a/pub/src/frida2.cpp b/pub/src/frida2.cpp
index 32f2b170..57533781 100644
--- a/pub/src/frida2.cpp
+++ b/pub/src/frida2.cpp
@@ -67,7 +67,7 @@ int main()
 
         try{
             // Get a word from the user:
-            string cmd = wask( NOlm::SEL.strg() + " > " );
+            string cmd = wask( NOlm::sel_str() + " > " );
 
             // Empty input is just ignored.
             if ( cmd=="" )
@@ -75,7 +75,7 @@ int main()
 
             // Is it a list?
             if ( cmd.find_first_of("-*0123456789")==0 ) {
-                if( NOlm::SEL.try_set( cmd ) )
+                if( NOlm::sel_try( cmd ) )
                     continue; // yes, it was a list
             }
 
@@ -88,8 +88,6 @@ int main()
                 printf( "last command took %g\n", time_cmd );
                 continue;
             } else if ( fridaCommand( cmd ) ) {
-                // If there are new files, make them new default
-                NOlm::SEL.update();
                 continue;
             }
 
diff --git a/pub/src/jsel.cpp b/pub/src/jsel.cpp
index 09bd2780..0381023c 100644
--- a/pub/src/jsel.cpp
+++ b/pub/src/jsel.cpp
@@ -10,8 +10,7 @@
 
 #include <read-plus/ask.hpp>
 
-using namespace std;
-
+#include "defs.h"
 #include "olf.h"
 #include "mem.h"
 #include "list.h"
diff --git a/pub/src/list.cpp b/pub/src/list.cpp
index 0989c6cd..7d6e7468 100644
--- a/pub/src/list.cpp
+++ b/pub/src/list.cpp
@@ -1,9 +1,9 @@
-//**************************************************************************//
-//* FRIDA: flexible rapid interactive data analysis                        *//
-//* list: list of indices (more info in .h file)                           *//
-//* (C) Joachim Wuttke 1990-, v2(C++) 2001-                                *//
-//* http://apps.jcns.fz-juelich.de                                         *//
-//**************************************************************************//
+//***************************************************************************//
+//* FRIDA: flexible rapid interactive data analysis                         *//
+//* list: list of indices (more info in .h file)                            *//
+//* (C) Joachim Wuttke 1990-, v2(C++) 2001-                                 *//
+//* http://apps.jcns.fz-juelich.de                                          *//
+//***************************************************************************//
 
 #include <iostream>
 #include <vector>
@@ -15,9 +15,9 @@
 #include "list.h"
 
 
-//**************************************************************************//
-//*  Constructors                                                          *//
-//**************************************************************************//
+//***************************************************************************//
+//*  Constructors                                                           *//
+//***************************************************************************//
 
 //! Initialize undefined list.
 
@@ -70,30 +70,16 @@ CList::CList( uint from, uint to ) :
     }
 }
 
+//***************************************************************************//
+//*  Manipulate list                                                        *//
+//***************************************************************************//
 
-//**************************************************************************//
-//*  Interactive initialization (broken)                                   *//
-//**************************************************************************//
+//! Reset to default initial state.
 
-
-/* 
-void CList::ask_and_set( const string& quest )
+void CList::clear()
 {
+    *this = CList();
 }
-input help:
-            cout << "required input type: index list (without blanks)\n"
-                "examples:\n"
-                "    0,2,7         comma-separated list of values\n"
-                "    0-7           range\n"
-                "    0-7.2         each 2nd: 0,2,4,6\n"
-                "    -             empty list\n"
-                "    *             full range, according to context\n"
-                "    8-*           from 8 to end of range\n";
-*/
-
-//**************************************************************************//
-//*  Manipulate list                                                       *//
-//**************************************************************************//
 
 //! Set bounds and evaluate instruction.
 
@@ -145,9 +131,9 @@ void CList::append( const uint val )
 }
 
 
-//**************************************************************************//
-//*  Inspect                                                               *//
-//**************************************************************************//
+//***************************************************************************//
+//*  Inspect                                                                *//
+//***************************************************************************//
 
 bool CList::contains(uint val) const
 {
@@ -201,9 +187,9 @@ string CList::sel2text(void) const
 }
 
 
-//**************************************************************************//
-//*  parse string                                                          *//
-//**************************************************************************//
+//***************************************************************************//
+//*  parse string                                                           *//
+//***************************************************************************//
 
 void CList::parse( string inp )
 {
diff --git a/pub/src/list.h b/pub/src/list.h
index 01e0fb61..c2c6141b 100644
--- a/pub/src/list.h
+++ b/pub/src/list.h
@@ -53,6 +53,7 @@ public:
     void evaluate( uint _inf, uint _sup );
 
     // modify:
+    void clear();
     void increment( const int step );
     void sort();
     void sortReverse();
diff --git a/pub/src/mem.cpp b/pub/src/mem.cpp
index f0594618..8463723c 100644
--- a/pub/src/mem.cpp
+++ b/pub/src/mem.cpp
@@ -1,230 +1,101 @@
-//**************************************************************************//
-//* FRIDA: flexible rapid interactive data analysis                        *//
-//* mem: online memory                                                     *//
-//* (C) Joachim Wuttke 1990-, v2(C++) 2001-                                *//
-//* http://apps.jcns.fz-juelich.de                                         *//
-//**************************************************************************//
+//***************************************************************************//
+//* FRIDA: flexible rapid interactive data analysis                         *//
+//* mem: online memory and file seclection                                  *//
+//* (C) Joachim Wuttke 1990-, v2(C++) 2001-                                 *//
+//* http://apps.jcns.fz-juelich.de                                          *//
+//***************************************************************************//
 
 #include <iostream>
 #include <string>
 #include <vector>
 #include <algorithm>
 
-#include <trivia/mystd.hpp>
-#include <read-plus/ask.hpp>
-
-using namespace std;
-
+#include "defs.h"
 #include "olf.h"
 #include "mem.h"
 #include "list.h"
 
 
+//***************************************************************************//
+//*  Singleton instances                                                    *//
+//***************************************************************************//
+
 namespace NOlm {
-    // Singleton instances.
     vector<POlo> MOM;
-    CFileSelection SEL;
     bool overwrite;
-
-    CList FSel;
-    CList FSelOld;
-    CList FSelNew;
+    CList FSel, FSelOld, FSelNew;
 };
 
 
-//**************************************************************************//
-//*  NOlm                                                                  *//
-//**************************************************************************//
-
-
-//! Append copy of file *olo to online memory.
-
-void NOlm::OloAdd( POlo f, int k )
-{
-    if( overwrite && k!=-1 ){
-        // replace input file
-        if( k>=MOM.size() )
-            throw "BUG: inconsistent k in OlfAdd";
-        MOM[k] = f;
-    } else {
-        // append at end of online memory
-        MOM.push_back( f );
-        FSelNew.append( MOM.size()-1 );
-    }
-}
-
-void NOlm::OloAdd( POld fd, int k ) { OloAdd( (POlo)fd, k ); }
-void NOlm::OloAdd( POlc fc, int k ) { OloAdd( (POlo)fc, k ); }
-
-
-//! Remove selected files from online memory.
+//***************************************************************************//
+//*  File selection                                                         *//
+//***************************************************************************//
 
-void NOlm::OlfDel()
-{
-    for (uint i = FSel.size()-1; i!=(uint)-1; --i) {
-        MOM.erase(MOM.begin()+FSel.V[i]);
-    }
-/*    SelPop();
-    SelPop();
-    if( MOM.size() > 0 )
-        FSel = CList(0,MOM.size()-1);
-    else
-        FSel = CList();
-*/
-}
-
-
-//! Append copy of selected files to online memory.
-
-void NOlm::OlfCopy()
-{
-    IterateO fiter;
-    POlo fin;
-    while ( (fin=fiter()) ){
-        POlo fout( fin->new_olo() );
-        fout->copy_mainvec( fin );
-        OloAdd( fout );
-    }
-}
-
-
-//! Move files to end of online memory.
+namespace NOlm {
 
-void NOlm::OlfExchange()
-{
-    FSelOld = FSel;
-    FSel = CList();
-    vector<POlo> aux;
-    for (uint i=FSelOld.size()-1; i!=(uint)-1; --i) {
-        aux.push_back( MOM[FSelOld.V[i]] );
-        MOM.erase( MOM.begin()+FSelOld.V[i] );
+    //! Receive new file selection.
+    void SelPush( CList& _FSel )
+    {
+        FSelOld = FSel;
+        FSel = _FSel;
     }
-    for ( uint i=0; i<FSelOld.size(); ++i ){
-        MOM.push_back( aux[i] );
-        FSel.append( MOM.size()-1 );
+    
+    //! Go back to previous file selection.
+    void SelPop()
+    {
+        FSel = FSelOld;
+        FSelOld = CList();
     }
-}
 
-//**************************************************************************//
-//*  File selection: Implementation                                        *//
-//**************************************************************************//
-
-namespace NOlm {
-
-    class MyFileSelection {
-    private: 
-    public:
-        MyFileSelection();
-        bool try_set( const string& inp );
-        void update();
-        void increment( int step );
-        string strg() const;
-        POlo first() const;
-    };
-
-    MyFileSelection::
-
-    MyFileSelection::MyFileSelection()
-    {
+    //! Show current file selection.
+    string sel_str()
+    { 
+        return FSel.str();
     }
 
-    bool MyFileSelection::try_set( const string& inp )
+    //! Return true if file selection can be taken from 'inp'.
+    bool sel_try( const string& inp )
     {
-        return false;
+        try {
+            CList newSel( inp );
+            NOlm::SelPush( newSel );
+            return true;
+        } catch ( ... ) {
+            return false;
+        }
     }
 
-    void MyFileSelection::update()
+    //! Start collection of new files.
+    void sel_collect_begin()
     {
+        FSelNew.clear();
     }
 
-    void MyFileSelection::increment( int step )
+    //! End collection of new files, and make them the new default selection.
+    void sel_collect_end()
     {
+        if( !overwrite && FSelNew.size()>0 )
+            SelPush( FSelNew );
     }
 
-    string MyFileSelection::strg() const
+    void sel_increment( int step )
     {
-        return "blabla";
+        for( uint i=0; i<FSel.size(); ++i )
+            FSel.V[i] += step;
     }
 
-    POlo MyFileSelection::first() const
+    POlo sel_first()
     {
-        return POlo();
+        if( FSel.size()>0 )
+            throw "empty file selection";
+        return MOM[FSel.V[0]];
     }
 }
 
-/*
-//! Receive new file selection.
-
-void NOlm::SelPush( CList& _FSel )
-{
-    FSelOld = FSel;
-    FSel = _FSel;
-}
-
-
-//! Go back to previous file selection.
-
-void NOlm::SelPop()
-{
-    FSel = FSelOld;
-    FSelOld = CList();
-}
-*/
-
-/* update()
-                if( !NOlm::overwrite )
-                    
-&& NOlm::FSelNew.size()>0 )
-                    NOlm::SelPush( NOlm::FSelNew );
-*/
-
-/* try_set( string inp )
-                bool islist = true;
-                try {
-                    CList newSel( cmd );
-                } catch ( ... ) {
-                    islist = false;
-                }
-                if ( islist ) {
-                    NOlm::SelPush( newSel );
-                    continue;
-                }
-                // it's not a list, let's look what it is instead ...
-                */
-
-//**************************************************************************//
-//*  File selection: Interface                                             *//
-//**************************************************************************//
-
-namespace NOlm {
-
-    CFileSelection::CFileSelection()
-    { pimpl = new MyFileSelection(); }
-    
-    bool CFileSelection::try_set( const string& inp )
-    { return pimpl->try_set( inp ); }
-
-    void CFileSelection::update()
-    { pimpl->update(); }
-
-    void CFileSelection::increment( int step )
-    { pimpl->increment( step ); }
-
-    string CFileSelection::strg() const
-    { return pimpl->strg(); }
-
-    POlo CFileSelection::first() const
-    { return pimpl->first(); }
-}
-
-//**************************************************************************//
-//*  Zentry selection                                                      *//
-//**************************************************************************//
 
-
-//**************************************************************************//
-//*  Iterators: Implementation                                             *//
-//**************************************************************************//
+//***************************************************************************//
+//*  Iterators: Implementation                                              *//
+//***************************************************************************//
 
 namespace NOlm {
 
@@ -252,103 +123,151 @@ namespace NOlm {
     };
 
     void MyIterate::reset()
-    {
-        iV = 0;
-    };
+    { iV = 0; };
 
     POlo MyIterate::get()
-    {
-        return MOM[Sel.V[iV++]];
-    }
+    { return MOM[Sel.V[iV++]]; }
 
     bool MyIterate::going() const
-    {
-        return iV<Sel.size();
-    }
+    { return iV<Sel.size(); }
 
     uint MyIterate::k() const
-    {
-        return Sel.V[iV-1];
-    }
+    { return Sel.V[iV-1]; }
 
     uint MyIterate::iteration() const
-    {
-        return iV-1;
-    }
+    { return iV-1; }
 
     uint MyIterate::size() const
+    { return Sel.V.size(); }
+}
+
+
+//***************************************************************************//
+//*  Iterators: Interface                                                   *//
+//***************************************************************************//
+
+namespace NOlm {
+
+    IterateO::IterateO( int mode ) :
+        pimpl( new MyIterate( mode ) )
     {
-        return Sel.V.size();
+        if ( !(mode & IterTOL) && !size() )
+            throw "empty file selection";
     }
-}
 
-//**************************************************************************//
-//*  Iterators: Interface                                                  *//
-//**************************************************************************//
+    IterateO::~IterateO()
+    {}
 
-NOlm::IterateO::IterateO( int mode )
-{
-    pimpl = new MyIterate( mode );
-    if ( !(mode & IterTOL) && !size() )
-        throw "empty file selection";
-}
+    POlo IterateO::operator()()
+    { return pimpl->going() ? pimpl->get() : POlo(); }
 
-NOlm::IterateO::~IterateO()
-{
-    delete pimpl;
-}
+    void IterateO::reset()
+    { return pimpl->reset(); }
 
-POlo NOlm::IterateO::operator()()
-{
-    return pimpl->going() ? pimpl->get() : POlo();
-}
+    uint IterateO::k() const
+    { return pimpl->k(); }
 
-void NOlm::IterateO::reset()
-{
-    return pimpl->reset();
-}
+    uint IterateO::iteration() const
+    { return pimpl->iteration(); }
 
-uint NOlm::IterateO::k() const
-{
-    return pimpl->k();
-}
+    uint IterateO::size() const
+    { return pimpl->size(); }
 
-uint NOlm::IterateO::iteration() const
-{
-    return pimpl->iteration();
-}
+    POld IterateD::operator()()
+    {
+        POld e;
+        while ( pimpl->going() && !(e = P2D( pimpl->get() )) ) {};
+        return e;
+    };
 
-uint NOlm::IterateO::size() const
-{
-    return pimpl->size();
-}
+    uint IterateD::size() const
+    {
+        IterateD iter2;
+        uint ret = 0;
+        for( ; iter2(); ++ret );
+        return ret;
+    }
 
-POld NOlm::IterateD::operator()()
-{
-    POld e;
-    while ( pimpl->going() && !(e = P2D( pimpl->get() )) ) {};
-    return e;
-};
+    POlc IterateC::operator()()
+    {
+        POlc e;
+        while ( pimpl->going() && !(e = P2C( pimpl->get() )) ) {};
+        return e;
+    }
 
-uint NOlm::IterateD::size() const
-{
-    IterateD iter2;
-    uint ret = 0;
-    for( ; iter2(); ++ret );
-    return ret;
+    uint IterateC::size() const
+    {
+        IterateC iter2;
+        uint ret = 0;
+        for( ; iter2(); ++ret );
+        return ret;
+    }
 }
 
-POlc NOlm::IterateC::operator()()
-{
-    POlc e;
-    while ( pimpl->going() && !(e = P2C( pimpl->get() )) ) {};
-    return e;
-}
 
-uint NOlm::IterateC::size() const
-{
-    IterateC iter2;
-    uint ret = 0;
-    for( ; iter2(); ++ret );
-    return ret;
+//***************************************************************************//
+//*  Modify online memory, and adapt file selection                         *//
+//***************************************************************************//
+
+namespace NOlm {
+
+    //! Append copy of file *olo to online memory.
+    void OloAdd( POlo f, int k )
+    {
+        if( overwrite && k!=-1 ){
+            // replace input file
+            if( k>=MOM.size() )
+                throw "BUG: inconsistent k in OlfAdd";
+            MOM[k] = f;
+        } else {
+        // append at end of online memory
+            MOM.push_back( f );
+            FSelNew.append( MOM.size()-1 );
+        }
+    }
+
+    void OloAdd( POld fd, int k ) { OloAdd( (POlo)fd, k ); }
+    void OloAdd( POlc fc, int k ) { OloAdd( (POlo)fc, k ); }
+
+    //! Remove selected files from online memory.
+    void OlfDel()
+    {
+        for (uint i = FSel.size()-1; i!=(uint)-1; --i) {
+            MOM.erase(MOM.begin()+FSel.V[i]);
+        }
+        SelPop();
+        SelPop();
+        if( MOM.size() > 0 )
+            FSel = CList(0,MOM.size()-1);
+        else
+            FSel = CList();
+    }
+
+    //! Append copy of selected files to online memory.
+    void OlfCopy()
+    {
+        IterateO fiter;
+        POlo fin;
+        while ( (fin=fiter()) ){
+            POlo fout( fin->new_olo() );
+            fout->copy_mainvec( fin );
+            OloAdd( fout );
+        }
+    }
+    
+    //! Move files to end of online memory.
+    void OlfExchange()
+    {
+        FSelOld = FSel;
+        FSel = CList();
+        vector<POlo> aux;
+        for (uint i=FSelOld.size()-1; i!=(uint)-1; --i) {
+            aux.push_back( MOM[FSelOld.V[i]] );
+            MOM.erase( MOM.begin()+FSelOld.V[i] );
+        }
+        for ( uint i=0; i<FSelOld.size(); ++i ){
+            MOM.push_back( aux[i] );
+            FSel.append( MOM.size()-1 );
+        }
+    }
 }
diff --git a/pub/src/mem.h b/pub/src/mem.h
index 8b56db42..e44f2b93 100644
--- a/pub/src/mem.h
+++ b/pub/src/mem.h
@@ -1,24 +1,21 @@
 //**************************************************************************//
 //* FRIDA: flexible rapid interactive data analysis                        *//
-//* mem: online memory                                                     *//
+//* mem: online memory and file selection                                  *//
 //* (C) Joachim Wuttke 1990-, v2(C++) 2001-                                *//
 //* http://apps.jcns.fz-juelich.de                                         *//
 //**************************************************************************//
 
 namespace NOlm {
 
-    //! The file selection administration.
-    class CFileSelection {
-    protected:
-        class MyFileSelection* pimpl;
-    public:
-        CFileSelection();
-        bool try_set( const string& inp );
-        void update();
-        void increment( int step );
-        string strg() const;
-        POlo first() const;
-    };
+    // Access file selection:
+    string sel_str();
+    POlo sel_first();
+
+    // Modify file selection:
+    bool sel_try( const string& inp );
+    void sel_collect_begin();
+    void sel_collect_end();
+    void sel_increment( int step );
 
     //! Modify online memory. File selection should be changed accordingly.
     void OloAdd( POlo olo, int k=-1 );
@@ -28,10 +25,10 @@ namespace NOlm {
     void OlfExchange();
     void OlfCopy();
 
-    //! Iterate over files of all types.
+    //! Iterate over all files in selection.
     class IterateO {
     protected:
-        class MyIterate* pimpl;
+        unique_ptr<class MyIterate> pimpl;
     public:
         IterateO( int mode=0 );
         ~IterateO();
@@ -46,14 +43,14 @@ namespace NOlm {
     const int IterALL = 1; // select all online files
     const int IterTOL = 2; // tolerate empty selection
 
-    //! Iterate over data files only.
+    //! Iterate over data files in selection.
     class IterateD : public IterateO { 
     public:
         POld operator()();
         uint size() const;
     };
 
-    //! Iterate over curve files only.
+    //! Iterate over curve files in selection.
     class IterateC : public IterateO {
     public:
         POlc operator()();
@@ -64,7 +61,6 @@ namespace NOlm {
     //! The actual online files:
     extern vector<POlo> MOM; // the mother of all on-line data
 
-    //! The actual file selection administration:
-    extern CFileSelection SEL;
+    //! Allow command to overwrite its input files?
     extern bool overwrite;
 };
diff --git a/pub/src/plot.cpp b/pub/src/plot.cpp
index 0b872757..902b735c 100644
--- a/pub/src/plot.cpp
+++ b/pub/src/plot.cpp
@@ -428,8 +428,8 @@ void NPlot::Plot( class CPlot *plot, bool add, const string& mode )
             determine_Yrange( plot, &JSel );
 
         // build label: (zur Zeit nur vom ersten File; definiere +=)
-        CCoord xCo = NOlm::SEL.first()->xco;
-        CCoord yCo = NOlm::SEL.first()->yco;
+        CCoord xCo = NOlm::sel_first()->xco;
+        CCoord yCo = NOlm::sel_first()->yco;
 
         // draw new frame:
         plot->clearFrame();
diff --git a/pub/src/zentry.cpp b/pub/src/zentry.cpp
index 66fb90f4..35ed9cf6 100644
--- a/pub/src/zentry.cpp
+++ b/pub/src/zentry.cpp
@@ -20,6 +20,10 @@
 //*  class Zentry                                                          *//
 //**************************************************************************//
 
+CZentry::CZentry() :
+    protect(false)
+{}
+
 //! Partial copy, to initialize a new Zentry from an existing one.
 
 void CZentry::copy_z_base( PZentry ein )
diff --git a/pub/src/zentry.h b/pub/src/zentry.h
index 12805640..1a1b9171 100644
--- a/pub/src/zentry.h
+++ b/pub/src/zentry.h
@@ -13,6 +13,7 @@ class CZentry {
     vector<double> z, dz;
     bool protect;
 
+    CZentry();
     virtual ~CZentry() {}; // magic: makes dynamic_cast possible
 
     void copy_z_base( boost::shared_ptr<class CZentry> ein );
-- 
GitLab