예제 #1
0
파일: dlp_file.c 프로젝트: thias42/dLabPro
/**
 * Create an absolute or full path name for the specified relative path name.
 *
 * @param lpsAbsPath
 *          Pointer to a buffer to be filled with the absolute path.
 * @param lpsRelPath
 *          Pointer to a null-terminated string containing the relative path.
 * @param nMaxLen
 *          Maximum length of the absolute path name buffer
 *          (<code>lpsAbsPath</code>).
 * @return <code>lpsAbsPath</code> if successful, <code>NULL</code> in case of
 *         errors.
 */
char* dlp_fullpath(char* lpsAbsPath, const char* lpsRelPath, INT32 nMaxLen)
{
  char        lpsCwd [L_PATH+1];                                                /* Saved current working directory   */
  char        lpsDir [L_PATH+1];                                                /* Directory part of lpsRelPath      */
  char        lpsFile[L_PATH+1];                                                /* Filename part of lpsRelPath       */
  struct stat filestat;                                                         /* File status struct                */
  if (!lpsAbsPath || nMaxLen<=0) return NULL;                                   /* No buffer, no service!            */
  *lpsAbsPath='\0';                                                             /* Initialize result to empty string */
  dlp_splitpath(lpsRelPath,lpsDir,lpsFile);                                     /* Split relative path               */
  if (!dlp_strlen(lpsDir)) dlp_strcpy(lpsDir,".");                              /* No directory -> use current one   */
  if(getcwd(lpsCwd,L_PATH)==NULL) return NULL;                                  /* Save current working directory    */
  if (dlp_chdir(lpsDir,FALSE)!=0) { dlp_chdir(lpsCwd,FALSE); return NULL;  }    /* Change to relative directory ...  */
  if(getcwd(lpsDir,L_PATH)==NULL) return NULL;                                  /* ... and determine its full path   */
  dlp_chdir(lpsCwd,FALSE);                                                      /* Change back to saved working dir. */
  if (nMaxLen<(INT32)(dlp_strlen(lpsDir)+dlp_strlen(lpsFile)+2)) return NULL;   /* Check return buffer size          */
  sprintf(lpsAbsPath,"%s%c%s",lpsDir,C_DIR,lpsFile);                            /* Create absolute path              */
#ifdef _WINDOWS                                                                 /* -- WINDOZE -->                    */
  dlp_strreplace(lpsAbsPath,"\\","/");                                          /* Replace backslashes by slashes    */
/*  dlp_strlwr(lpsAbsPath); */                                                    /* Convert to lower case             */
#endif                                                                          /* <--                               */
  if (stat(lpsAbsPath,&filestat)!=0) return NULL;                               /* Check file status (must exist)    */
  return lpsAbsPath;                                                            /* Return pointer to buffer          */
}
예제 #2
0
파일: fnc_iam.cpp 프로젝트: gitgun/dLabPro
/*
 * Manual page at function.def
 */
INT16 CGEN_PUBLIC CFunction::Cd(const char* lpsDir)
{
  if (dlp_chdir(lpsDir,FALSE)==0) return O_K;                                   // Change directory
  return IERROR(this,ERR_CHDIR,lpsDir,0,0);                                     // Failed? --> error message
}
예제 #3
0
파일: file_imex.c 프로젝트: gitgun/dLabPro
/**
 * Import and export of non-native file formats.
 *
 * @param _this       This instance.
 * @param sFilename   Name of file to import.
 * @param sFilter     Filter to use for import.
 * @param iInst       Instance where to save imported data.
 * @param nMode       Import or export mode.
 * @return <code>O_K</code> if successful, a (negative) error code otherwise
 */
INT16 CGEN_PROTECTED CDlpFile_ImportExport
(
  CDlpFile*   _this,
  const char* sFilename,
  const char* sFilter,
  CDlpObject* iInst,
  INT16 nMode
)
{
  INT16    retVal     = O_K;
  lnode_t* lpNode     = NULL;
  CFILE_FTYPE* lpType = NULL;
  FILE* lpFile        = NULL;
  char lpsFilenameLoc[L_PATH];
  char lpsCmd[L_PATH*2];

  CHECK_THIS_RV(FALSE);

  if(!dlp_strlen(sFilename)) return IERROR(_this,FIL_FILENAME,0     ,0,0);
  if(!iInst                ) return IERROR(_this,ERR_NULLARG,"iInst",0,0);

  dlp_strcpy(lpsFilenameLoc,sFilename);

  /* create target directory if necessary */
  if(nMode==EXPORT_FILE)
  {
    char lpNewDir[L_PATH] = "";
    char lpCurDir[L_PATH] = "";

#ifndef __TMS
    if(!_this->m_bExecute)
    {
      if(getcwd(lpCurDir,L_PATH) == NULL) return IERROR(_this,ERR_GETCWD,0,0,0);
      dlp_splitpath(lpsFilenameLoc,lpNewDir,NULL);
      if(0<dlp_strlen(lpNewDir))
      {
        if(dlp_chdir(lpNewDir,TRUE))
        {
          dlp_chdir(lpCurDir,FALSE);
          return
          IERROR(_this,ERR_FILEOPEN,lpsFilenameLoc,
              "writing (failed to create directory)",0);
        }
        dlp_chdir(lpCurDir,FALSE);
      }
    }
#endif
  }

#ifndef __TMS
  /* Execute lpsFilenameLoc (initialization & execute if import) */
  if(_this->m_bExecute)
  {
    char lpsBase[L_PATH];
    char *lpsTmp;
    dlp_splitpath(lpsFilenameLoc,NULL,lpsBase);
    if(strchr(lpsBase,' ')) *strchr(lpsBase,' ')='\0';
    lpsTmp=dlp_tempnam(NULL,lpsBase);
    snprintf(lpsCmd, L_PATH*2-1, "%s %c %s", lpsFilenameLoc, nMode==EXPORT_FILE?'<':'>', lpsTmp);
    dlp_strcpy(lpsFilenameLoc,lpsTmp);
    if(nMode==IMPORT_FILE) if(dlp_system(lpsCmd)!=0) return IERROR(_this,ERR_FILEOPEN,lpsCmd,"executing",0);
  }
#endif

  /* test if file is readable/writable */
  if(nMode==IMPORT_FILE)
    lpFile = fopen(lpsFilenameLoc,"r");
  else if(nMode==EXPORT_FILE)
    lpFile = fopen(lpsFilenameLoc,"a");
  else DLPASSERT(FMSG("Unknown operation mode."));

  if(!lpFile)
  {
    if (nMode==IMPORT_FILE)
      return IERROR(_this,ERR_FILEOPEN,sFilename,"reading",0);
    else if (nMode==EXPORT_FILE)
      return IERROR(_this,ERR_FILEOPEN,sFilename,"writing",0);
  }
  fclose(lpFile);

  /* lookup filter function from list */
  lpType = (CFILE_FTYPE*)dlp_calloc(1,sizeof(CFILE_FTYPE));
  if(!lpType) return IERROR(_this,ERR_NOMEM,0,0,0);

  dlp_strncpy(lpType->lpName,sFilter,L_NAMES);
  dlp_strncpy(lpType->lpClassName,iInst->m_lpClassName,L_NAMES);
  lpType->nMode = nMode;

  lpNode = list_find(_this->m_lpFtypes,lpType,CDlpFile_CompareFTypeList);
  if(!lpNode)
  {
    dlp_free(lpType);
    return
      IERROR(_this,FIL_NOIMEX,nMode==IMPORT_FILE?"import":"export",sFilter,
        iInst->m_lpClassName);
  }
  dlp_free(lpType);
  lpType = NULL;

  /* Invoke import function */
  lpType = (CFILE_FTYPE*)lnode_get(lpNode);
  retVal = lpType->FilterFunc(_this,lpsFilenameLoc,iInst,lpType->lpName);

#ifndef __TMS
  /* Execute lpsFilenameLoc (execute if export & finalization) */
  if(_this->m_bExecute)
  {
    if(nMode==EXPORT_FILE) dlp_system(lpsCmd);
    unlink(lpsFilenameLoc);
  }else
#endif
  if(nMode==EXPORT_FILE && _this->m_bZip) dlp_fzip(lpsFilenameLoc,"wb6");

  if(retVal != O_K)
  {
    if(nMode==IMPORT_FILE) return IERROR(_this,FIL_IMPORT,lpsFilenameLoc,sFilter,0);
    else                   return IERROR(_this,FIL_EXPORT,lpsFilenameLoc,sFilter,0);
  }

  return O_K;
}