Hi,
i've implemented a automatic backup-creation to scite (mozers version):
it uses 2 properties:
backup.files => number of backups (1-9)
backup.path => subdir of file's directory to collect the backups

SciteBase.h:670
bool CreateBackup(FilePath saveName); /* F.W. */

SciteIO.cxx:778
/*======================= F.W. ==========================*/
bool FileExists( const char* FileName )
{
  FILE* fp = NULL;
  //will not work if you do not have read permissions
  //to the file, but if you don't have read, it
  //may as well not exist to begin with.
  fp = fopen( FileName, "rb" );
  if( fp != NULL )
  {
      fclose( fp );
      return true;
  }
  return false;
}

char * buildFilename(const char *path, const char *filename, const char *ext, const int i)
{
  char *is=(char *) malloc(2);
  itoa(i,is,10);
  int l=strlen(path)+strlen(filename)+strlen(ext)+2;
  char *fn= (char *) malloc(l+1);
  strcpy(fn,path);
  strcat(fn,filename);
  strcat(fn,".");
  strcat(fn,is);
  strcat(fn,ext);
  free(is);
  return fn;
}

const char * GetFileName(const char* filename)
{
  const char *p=strrchr(filename,'\\');
  if (p==NULL)
  {
    return p;
  } else
  {
    return p+1;
  }
}

SString BuildPath(const char* filename, SString subdir)
{
  const char *p=GetFileName(filename);
  if (subdir !="")
  {
    subdir+="\\";
  }
  int fl=strlen(filename)-strlen(p);
  char *d=(char *) malloc(fl+strlen(subdir.c_str()));
  strncpy(d,filename,fl);
  d[fl]='\0';
  SString res=SString(d);
  free(d);
  if (subdir != "")
  {
    res+=subdir;
    CreateDirectory(res.c_str(),0);
  }
  return res;
}

bool SciTEBase::CreateBackup(FilePath saveName)
{
  bool ret=false;
  //get properties
  int c=props.GetInt("backup.files");
  if ((c>0) && (c < 10))
  {
    SString bp=props.Get("backup.path");
    char *ext=".bk";
    //get filename
    const char *f=GetFileName(saveName.AsFileSystem());
    //get path
    SString pth=BuildPath(saveName.AsFileSystem(), bp);
    if (FileExists(saveName.AsFileSystem()))
    {
      char *lfn=buildFilename(pth.c_str(),f,ext,c);
      if (FileExists(lfn))
      {
        DeleteFile(lfn);
      }
      free(lfn); // last filename
      //rename
      int i;
      for (i=c;i>1;i--)
      {
        char *fn=buildFilename(pth.c_str(),f,ext,i-1);
        char *fn2=buildFilename(pth.c_str(),f,ext,i);
        if (FileExists(fn))
        {
          rename(fn,fn2);
        }
        free(fn2);
        free(fn);
      }
      char *ffn=buildFilename(pth.c_str(),f,ext,1);
      rename(saveName.AsFileSystem(),ffn);
      ret=FileExists(ffn);
      free(ffn); //filename
    }
  }
  return ret;
}
/*================= end F.W. ======================*/

/**
 * Writes the buffer to the given filename.
 */
bool SciTEBase::SaveBuffer(FilePath saveName) {
...
  CreateBackup(saveName);  /* F.W. */
  FILE *fp = saveName.Open(fileWrite);


its my first real c++ routine so pls say if i made some mistakes. I found not much about SString so i don't know, if i have to free memory of it. its currently only working because i don't know the linux filesystem-functions for createDirectory, rename and deletefile.

regards Frank
_______________________________________________
Scite-interest mailing list
[email protected]
http://mailman.lyra.org/mailman/listinfo/scite-interest

Reply via email to