arbeit
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

nrroComment.cpp

Go to the documentation of this file.
00001 //------------------------------------------------------------------------
00002 //
00003 //   Joe Kniss
00004 //     6-25-03
00005 //                   ________    ____   ___ 
00006 //                  |        \  /    | /  /
00007 //                  +---+     \/     |/  /
00008 //                  +--+|  |\    /|     < 
00009 //                  |  ||  | \  / |  |\  \ 
00010 //                  |      |  \/  |  | \  \ 
00011 //                   \_____|      |__|  \__\
00012 //                       Copyright  2003 
00013 //                      Joe Michael Kniss
00014 //                   <<< jmk@cs.utah.edu >>>
00015 //               "All Your Base are Belong to Us"
00016 //-------------------------------------------------------------------------
00017 
00018 //nrro.h
00019 
00020 #include "nrro.h"
00021 
00022 using namespace std;
00023 
00024 /////////////////////////////////////////////////////////////////////////
00025 //num comments
00026 /////////////////////////////////////////////////////////////////////////
00027 int Nrro::numComments() const
00028 {
00029    if(!_nrrd->cmt) return 0;
00030    return _nrrd->cmtArr->len;
00031 }
00032 
00033 /////////////////////////////////////////////////////////////////////////
00034 //get comment i
00035 /////////////////////////////////////////////////////////////////////////
00036 std::string  Nrro::getComment(int i)
00037 {
00038    if((!_nrrd->cmt) || ( i >= _nrrd->cmtArr->len))
00039       return std::string();
00040    return std::string(_nrrd->cmt[i]);
00041 }
00042 
00043 /////////////////////////////////////////////////////////////////////////
00044 //get comment key
00045 /////////////////////////////////////////////////////////////////////////
00046 std::string  Nrro::getComment(const std::string key)
00047 {
00048    char *cmt = nrrdCommentScan(_nrrd, key.c_str());
00049    if(cmt)
00050    {
00051       return std::string(cmt);
00052    }
00053    return std::string();
00054 }
00055 
00056 /////////////////////////////////////////////////////////////////////////
00057 //add comment
00058 /////////////////////////////////////////////////////////////////////////
00059 void         Nrro::addComment(const std::string comment)
00060 {
00061    nrrdCommentAdd(_nrrd,comment.c_str());
00062 }
00063 
00064 /////////////////////////////////////////////////////////////////////////
00065 //add comment + key
00066 /////////////////////////////////////////////////////////////////////////
00067 void         Nrro::addComment(const std::string key, const std::string comment)
00068 {
00069    std::string cmt = key;
00070    cmt += ":";
00071    cmt += comment;
00072    nrrdCommentAdd(_nrrd,cmt.c_str());
00073 }
00074 
00075 /////////////////////////////////////////////////////////////////////////
00076 //set comment
00077 /////////////////////////////////////////////////////////////////////////
00078 void         Nrro::setComment(int i, const std::string comment)
00079 {
00080    if((!_nrrd->cmt) || ( i >= _nrrd->cmtArr->len))
00081    {
00082       return;
00083    }
00084 
00085    if(comment.empty())
00086    {
00087       NrroDbg("setComment(i), adding an empty comment???");
00088       return;
00089    }
00090 
00091    /// have to be carefull here, nrrd might use malloc!,
00092    /// but we don't really know?
00093    /// Lesson: always use the library's creation deletion model
00094    if(_nrrd->cmt[i]){
00095       if(_nrrd->cmtArr->freeCB)
00096       {
00097          _nrrd->cmtArr->freeCB(_nrrd->cmt[i]);
00098       }
00099       else
00100       {
00101          _nrrd->cmtArr->doneCB(_nrrd->cmt[i]);
00102       }
00103    }
00104    _nrrd->cmt[i] = airStrdup(comment.c_str());
00105 }
00106 
00107 /////////////////////////////////////////////////////////////////////////
00108 //What is "whitespace", pat
00109 /////////////////////////////////////////////////////////////////////////
00110 static const std::string whitespc("\t \n");
00111 
00112 /////////////////////////////////////////////////////////////////////////
00113 //stringHasKey,
00114 //a local function to detect if  a string has a key, but the
00115 //key isn't just a word in the string
00116 /////////////////////////////////////////////////////////////////////////
00117 bool stringHasKey(const std::string &key, const std::string &cmt)
00118 {
00119    int idx;
00120    /// first see if the key is in the string
00121    if((idx=cmt.find(key)) != -1)
00122    {
00123       /// increment the length of the key
00124       idx+= key.length();
00125       /// now find the next non whitespace char
00126       if((idx=cmt.find_first_not_of(whitespc,idx)) != -1)
00127       {
00128          /// if that is a ':'....
00129          if(cmt[idx] == ':')
00130          {
00131             /// see if there is a comment after it,
00132             /// ie. non whitespace characters
00133             if(cmt.find_first_not_of(whitespc, idx+1) != -1)
00134             {
00135                /// if so, comment contains the key,
00136                /// and the key is not just a comment
00137                return true;
00138             }
00139          }
00140       }
00141    }
00142    return false;
00143 }
00144 
00145 /////////////////////////////////////////////////////////////////////////
00146 //set comment key
00147 /////////////////////////////////////////////////////////////////////////
00148 void         Nrro::setComment(const std::string key, const std::string comment)
00149 {
00150    /// no comments yet, just add it
00151    if((!_nrrd->cmt))
00152    {
00153       addComment(key,comment);
00154       return;
00155    }
00156    /// else need to check if the comment already exists
00157    /// doesn't exist yet, just add it
00158    if(!nrrdCommentScan(_nrrd, key.c_str()))
00159    {
00160       addComment(key,comment);
00161       return;
00162    }
00163    /// otherwise nrrdCommentScan found the key+comment
00164    /// but we have to locate it ourselves,
00165    /// that is, find the index of the key+comment and
00166    /// set it to something else.
00167 
00168    /// generate the comment
00169    std::string cmt = key;
00170    cmt += ":";
00171    cmt += comment;
00172 
00173    /// does exist, go find it
00174    for(int i=0; i<numComments(); ++i) /// check each comment
00175    {
00176       /// this uses a local function (above) to detect
00177       /// if the string has the key, but the key isn't just
00178       /// a word in the comment
00179       if( _nrrd->cmt[i] && stringHasKey(key, std::string(_nrrd->cmt[i])) )
00180       {
00181          setComment(i,cmt);
00182          return;
00183       }
00184    }
00185 
00186    /// what?? we should have detected that it wasn't there by now
00187    NrroErr("setComment(), comment not set, not found and not detected empty");
00188    return;      
00189 }
00190 

Send questions, comments, and bug reports to:
jmk