SASC-NG
DVBSoftCAM

cStructLoader Class Reference

#include <data.h>

Inheritance diagram for cStructLoader:

List of all members.

Public Member Functions

 cStructLoader (const char *Type, const char *Filename, int Flags)
virtual ~cStructLoader ()
void AddItem (cStructItem *n, const char *com, cStructItem *ref)
void DelItem (cStructItem *d, bool keep=false)
cStructItem * NextValid (cStructItem *it) const
void SetCfgDir (const char *cfgdir)
virtual void Load (bool reload)
virtual void Save (void)
void Purge (void)
void SafeClear (void)
void Disable (void)

Protected Member Functions

void CheckAccess (void)
bool CheckUnmodified (void)
void LoadFinished (void)
void OpenFailed (void)
bool CheckDoSave (void)
time_t MTime (bool log)
virtual cStructItem * ParseLine (char *line)=0
void Modified (bool mod=true)
bool IsModified (void) const
void ListLock (bool rw)
void ListUnlock (void)
virtual void PreLoad (void)
virtual void PostLoad (void)

Protected Attributes

int flags
const char * type
const char * filename
char * path
time_t mtime

Friends

class cStructLoaders

Detailed Description

Definition at line 111 of file data.h.


Constructor & Destructor Documentation

cStructLoader::cStructLoader ( const char *  Type,
const char *  Filename,
int  Flags 
)

Definition at line 183 of file data.c.

References filename, flags, mtime, path, SL_CUSTOMMASK, and type.

:lock(true)
{
  path=0; mtime=0;
  type=Type; filename=Filename; flags=Flags&SL_CUSTOMMASK;
  cStructLoaders::Register(this);
}
cStructLoader::~cStructLoader ( ) [virtual]

Definition at line 191 of file data.c.

References free, and path.

{
  free(path);
}

Member Function Documentation

void cStructLoader::AddItem ( cStructItem *  n,
const char *  com,
cStructItem *  ref 
)

Definition at line 196 of file data.c.

References cSimpleListBase::Add(), cSimpleList< cStructItem >::First(), ListLock(), ListUnlock(), Modified(), cSimpleList< cStructItem >::Next(), cStructItem::SetComment(), and cStructItem::Special().

{
  n->SetComment(com);
  ListLock(true);
  cStructItem *a=0;
  if(ref) { // insert before reference
    for(a=First(); a; a=Next(a))
      if(Next(a)==ref) break;
    }
  if(!a) { // insert before first non-special
    for(a=First(); a;) {
      cStructItem *nn=Next(a);
      if(nn && !nn->Special()) break;
      a=nn;
      }
    }
  Add(n,a);
  Modified();
  ListUnlock();
}
void cStructLoader::CheckAccess ( void  ) [protected]

Definition at line 257 of file data.c.

References L_GEN_ERROR, L_GEN_WARN, path, PRINTF, SL_CLRFLAG, SL_NOACCESS, and SL_SETFLAG.

Referenced by cStructLoaderPlain::Load(), and Load().

{
  if(access(path,R_OK|W_OK)!=0) {
    if(errno!=EACCES)
      PRINTF(L_GEN_ERROR,"failed access %s: %s",path,strerror(errno));
    PRINTF(L_GEN_WARN,"no write permission on %s. Changes will not be saved!",path);
    SL_SETFLAG(SL_NOACCESS);
    }
  else SL_CLRFLAG(SL_NOACCESS);
}
bool cStructLoader::CheckDoSave ( void  ) [protected]
bool cStructLoader::CheckUnmodified ( void  ) [protected]

Definition at line 268 of file data.c.

References L_CORE_LOAD, mtime, MTime(), path, PRINTF, SL_TSTFLAG, and SL_WATCH.

Referenced by CheckDoSave().

{
  time_t curr_mtime=MTime(false);
  if(mtime && mtime<curr_mtime && SL_TSTFLAG(SL_WATCH)) {
     PRINTF(L_CORE_LOAD,"abort save as file %s has been changed externaly",path);
     return false;
     }
  return true;
}
void cStructLoader::DelItem ( cStructItem *  d,
bool  keep = false 
)
void cStructLoader::Disable ( void  ) [inline]

Definition at line 149 of file data.h.

References SL_DISABLED, and SL_SETFLAG.

Referenced by cSmartCards::Disable(), InitAll(), and cSoftCAM::Load().

bool cStructLoader::IsModified ( void  ) const [inline, protected]

Definition at line 132 of file data.h.

References SL_MODIFIED, and SL_TSTFLAG.

Referenced by CheckDoSave(), and Load().

{ return SL_TSTFLAG(SL_MODIFIED); }
void cStructLoader::Load ( bool  reload) [virtual]

Reimplemented in cStructLoaderPlain.

Definition at line 302 of file data.c.

References cSimpleListBase::Add(), CheckAccess(), cSimpleListBase::Clear(), DelItem(), cSimpleList< cStructItem >::First(), IsModified(), L_CORE_LOAD, L_GEN_ERROR, L_GEN_INFO, ListLock(), ListUnlock(), LoadFinished(), Modified(), mtime, MTime(), cSimpleList< cStructItem >::Next(), OpenFailed(), ParseLine(), path, PostLoad(), PreLoad(), PRINTF, cStructItem::SetComment(), skipspace(), SL_CLRFLAG, SL_DISABLED, SL_LOADED, SL_SETFLAG, SL_TSTFLAG, SL_WATCH, strreplace(), and type.

{
  if(SL_TSTFLAG(SL_DISABLED) || (reload && !SL_TSTFLAG(SL_WATCH))) return;
  FILE *f=fopen(path,"r");
  if(f) {
    PreLoad();
    int curr_mtime=MTime(true);
    ListLock(true);
    bool doload=false;
    if(!reload) {
      Clear(); Modified(false);
      mtime=curr_mtime;
      doload=true;
      }
    else if(mtime<curr_mtime) {
      PRINTF(L_CORE_LOAD,"detected change of %s",path);
      if(IsModified())
        PRINTF(L_CORE_LOAD,"discarding in-memory changes");
      for(cStructItem *a=First(); a; a=Next(a)) DelItem(a);
      Modified(false);
      mtime=curr_mtime;
      doload=true;
      }
    if(doload) {
      SL_SETFLAG(SL_LOADED);
      PRINTF(L_GEN_INFO,"loading %s from %s",type,path);
      CheckAccess();
      int lineNum=0, num=0;
      char buff[4096];
      while(fgets(buff,sizeof(buff),f)) {
        lineNum++;
        if(!index(buff,'\n') && !feof(f)) {
          PRINTF(L_GEN_ERROR,"file %s readbuffer overflow line#%d",path,lineNum);
          SL_CLRFLAG(SL_LOADED);
          break;
          }
        strreplace(buff,'\n',0); strreplace(buff,'\r',0); // chomp
        bool hasContent=false;
        char *ls;
        for(ls=buff; *ls; ls++) {
          if(*ls==';' || *ls=='#') {              // comment
            if(hasContent)
              while(ls>buff && ls[-1]<=' ') ls--; // search back to non-whitespace
            break;
            }
          if(*ls>' ') hasContent=true;            // line contains something usefull
          }
        cStructItem *it=0;
        if(hasContent) {
          char save=*ls;
          *ls=0; it=ParseLine(skipspace(buff)); *ls=save;
          if(!it) {
            PRINTF(L_GEN_ERROR,"file %s has error in line #%d",path,lineNum);
            ls=buff;
            }
          else num++;
          }
        else ls=buff;
        if(!it) it=new cCommentItem;
        if(it) {
          it->SetComment(ls);
          Add(it);
          }
        else {
          PRINTF(L_GEN_ERROR,"out of memory loading file %s",path);
          SL_CLRFLAG(SL_LOADED);
          break;
          }
        }
      ListUnlock();
      PRINTF(L_CORE_LOAD,"loaded %d %s from %s",num,type,path);
      PostLoad();
      }
    else ListUnlock();

    fclose(f);
    LoadFinished();
    }
  else
    OpenFailed();
}
void cStructLoader::LoadFinished ( void  ) [protected]

Definition at line 285 of file data.c.

References L_CORE_LOAD, path, PRINTF, SL_CLRFLAG, SL_LOADED, SL_SHUTUP, and SL_TSTFLAG.

Referenced by cStructLoaderPlain::Load(), and Load().

{
  SL_CLRFLAG(SL_SHUTUP);
  if(!SL_TSTFLAG(SL_LOADED))
    PRINTF(L_CORE_LOAD,"loading %s terminated with error. Changes will not be saved!",path);
}
void cStructLoader::Modified ( bool  mod = true) [inline, protected]
time_t cStructLoader::MTime ( bool  log) [protected]

Definition at line 244 of file data.c.

References L_GEN_ERROR, L_GEN_WARN, path, and PRINTF.

Referenced by CheckUnmodified(), Load(), and Save().

{
  struct stat64 st;
  if(stat64(path,&st)!=0) {
    if(log) {
      PRINTF(L_GEN_ERROR,"failed fstat %s: %s",path,strerror(errno));
      PRINTF(L_GEN_WARN,"automatic reload of %s disabled",path);
      }
    st.st_mtime=0;
    }
  return st.st_mtime;
}
cStructItem * cStructLoader::NextValid ( cStructItem *  it) const
void cStructLoader::OpenFailed ( void  ) [protected]
virtual cStructItem* cStructLoader::ParseLine ( char *  line) [protected, pure virtual]
virtual void cStructLoader::PostLoad ( void  ) [inline, protected, virtual]

Reimplemented in cPlainKeys, cOverrides, and cTpsKeys.

Definition at line 136 of file data.h.

Referenced by cStructLoaderPlain::Load(), and Load().

{}
virtual void cStructLoader::PreLoad ( void  ) [inline, protected, virtual]

Reimplemented in cStructLoaderPlain, cOverrides, and cTpsKeys.

Definition at line 135 of file data.h.

Referenced by Load().

{}
void cStructLoader::Purge ( void  )
void cStructLoader::SafeClear ( void  )
void cStructLoader::Save ( void  ) [virtual]

Reimplemented in cStructLoaderPlain.

Definition at line 397 of file data.c.

References CheckDoSave(), cSafeFile::Close(), cSimpleList< cStructItem >::First(), L_CORE_LOAD, ListLock(), ListUnlock(), Modified(), MTime(), mtime, cSimpleList< cStructItem >::Next(), cSafeFile::Open(), path, PRINTF, and type.

{
  if(CheckDoSave()) {
    cSafeFile f(path);
    if(f.Open()) {
      ListLock(false);
      for(cStructItem *it=First(); it; it=Next(it))
        if(!it->Deleted() && !it->Save(f)) break;
      f.Close();
      mtime=MTime(true);
      Modified(false);
      ListUnlock();
      PRINTF(L_CORE_LOAD,"saved %s to %s",type,path);
      }
    }
}
void cStructLoader::SetCfgDir ( const char *  cfgdir)

Definition at line 238 of file data.c.

References AddDirectory(), filename, free, and path.

{
  free(path);
  path=strdup(AddDirectory(cfgdir,filename));
}

Friends And Related Function Documentation

friend class cStructLoaders [friend]

Definition at line 112 of file data.h.


Member Data Documentation

const char * cStructLoader::filename [protected]

Definition at line 119 of file data.h.

Referenced by cStructLoader(), and SetCfgDir().

int cStructLoader::flags [protected]

Definition at line 118 of file data.h.

Referenced by cStructLoader().

time_t cStructLoader::mtime [protected]

Definition at line 121 of file data.h.

Referenced by CheckUnmodified(), cStructLoader(), Load(), and Save().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines