Exemple #1
0
/**
 * Export midi notes of  data instance into midifile, needs external program midiconvert
 *
 * @param lpsFilename Name of file to export
 * @param iSst        Pointer to instance to export
 * @param lpsFiletype Type of file to export
 * @return <code>O_K</code> if successful, a (negative) error code otherwise
 */
INT16 CGEN_PROTECTED CDlpFile_Midi_ExportMidi
(
  CDlpFile*   _this,
  const char* lpsFilename,
  CDlpObject* iSrc,
  const char* lpsFiletype
)
{
  char  lpsTempFile[L_PATH];
  char  lpsCmdline    [3*L_PATH] = "";
  INT16 nErr                     =O_K;

  strcpy(lpsTempFile,dlp_tempnam(NULL,"dlabpro_midi_export"));
  sprintf(lpsCmdline,"midiconvert %s %s", lpsTempFile, lpsFilename);

  CData *idSrc = AS(CData,iSrc);
  CData_InsertRecs(idSrc, 0, 1, 1);
  CData_Dstore(idSrc, CData_GetDescr(idSrc,DESCR0),0,5);
  IF_NOK(CDlpFile_ExportAsciiFromData(_this,lpsTempFile,iSrc,"csv"))
       nErr = IERROR(iSrc,FIL_EXPORT,lpsTempFile,"csv",0);
  CData_DeleteRecs(idSrc,0,1);

  if (system(lpsCmdline)!=0) { nErr=IERROR(_this,FIL_EXEC,lpsCmdline,0,0); }

  if (remove(lpsTempFile)==-1) nErr=IERROR(_this,FIL_REMOVE,"temporary ",lpsTempFile,0);

  /* Clean up */
  return nErr;
}
Exemple #2
0
/**
 * Compress the given file: create a temporary output file. Remove original
 * file and rename temporary file after successfull completion of gz_compress().
 *
 * @param lpsInfile
 *       Pointer to filename of input file
 * @param lpsMode
 *       Mode of file compression. ("wb[0-9]")
 * @return <code>TRUE</code> if compression succeeded, <code>FALSE</code> otherwise.
 */
BOOL dlp_fzip(const char *lpsInfile, const char *lpsMode)
{
#ifndef __NOZLIB
  char   lpsFile[L_PATH];
  char   lpsDir[L_PATH];
  char  *lpsOutfile = NULL;
  BOOL   bSuccess = TRUE;
    FILE  *lpInfile;
    gzFile lpOutfile;

  /* Open input file */
    lpInfile = fopen(lpsInfile, "rb");
    if (lpInfile == NULL) {
        perror(lpsInfile);
        return FALSE;
    }

  /* Create and open temporary output file */
  dlp_splitpath(lpsInfile,lpsDir,lpsFile);
  lpsOutfile = dlp_tempnam(NULL,lpsFile);
    lpOutfile = gzopen(lpsOutfile, lpsMode);
    if (lpOutfile == NULL) {
        fprintf(stderr, "zlib@zlib_fcompress(): can't gzopen %s\n", lpsOutfile);
        fclose(lpInfile);
        return FALSE;
    }


    bSuccess = gz_compress(lpInfile, lpOutfile);

  if(bSuccess)
  {
      strncpy(lpsFile,lpsInfile,L_PATH-2);
      strcat(lpsFile,"~");
      rename(lpsInfile,lpsFile);
      if(gz_rename(lpsOutfile,lpsInfile)==0)
      {
    if(unlink(lpsFile))  perror("unlink");
      }
      else
      {
    fprintf(stderr, "zlib@zlib_fcompress(): failed to compress file. Save %s uncompressed\n", lpsInfile);
    unlink(lpsInfile);
    rename(lpsFile,lpsInfile);
      }
  }
  else
  {
      fprintf(stderr, "zlib@zlib_fcompress(): failed to compress file. Save %s uncompressed\n", lpsInfile);
      if(unlink(lpsOutfile)) perror("unlink");
  }

  return TRUE;
#else /* __NOZLIB */
  return FALSE;
#endif
}
Exemple #3
0
/*
 * Manual page at process.def
 */
INT32 CGEN_PUBLIC CProcess::Start()
{
  const SMic* pMic    = NULL;                                                   // Method invocation context of Start()
  CFunction*  iCaller = NULL;                                                   // Function calling Start()
  CFunction*  iFnc    = NULL;                                                   // Process function
  StkItm*     pStkItm = NULL;                                                   // Stack item
  INT32       nArgs   = 0;                                                      // Number of process function arguments

  // Validate and initialize                                                    // ------------------------------------
  if (m_nState!=0)                                                              // Not virginal
    return IERROR(this,PRC_CANTSTART,"multiple starts not allowed",0,0);        //   Forget it!
  if (!(pMic = CDlpObject_MicGet(_this))) return -1;                            // Get method invocation context
  iCaller = (CFunction*)CDlpObject_OfKind("function",pMic->iCaller);            // Get calling CFunction
  if (!iCaller) return -1;                                                      // Must be a function!

  // Initialize process                                                         // ------------------------------------
  sprintf(m_psTmpFile,"%s%ld",dlp_tempnam(NULL,"~dLabPro#process#"),(long)dlp_time());// Initialize temp. file name prefix

  // Marshal arguments                                                          // ------------------------------------
  if (!(pStkItm=iCaller->StackGet(0))) return IERROR(this,PRC_TOOFEWARGS,0,0,0);// Get stack top
  if (pStkItm->nType==T_INSTANCE)                                               // Stack top is an instance
    iFnc = (CFunction*)CDlpObject_OfKind("function",pStkItm->val.i);            //   Get function to be called
  if (iFnc)                                                                     // This process is a function call
  {                                                                             // >>
    IFIELD_RESET(CDlpObject,"dto");                                             //   Create data transfer object
    nArgs = CData_GetNRecs(iFnc->m_idArg);                                      //   Get number of function arguments
    Marshal(m_iDto,iCaller,nArgs);                                              //   Marshal arguments for transfer
  }                                                                             // <<
  else                                                                          // This process is a program call
    dlp_strcpy(m_psCmdLine,iCaller->PopString(0));                              //   Get program command line

#ifdef USE_FORK
  if (iFnc)                                                                     // This process is a function call
  {                                                                             // >>
    m_hPid=fork();                                                              //   Fork the process
    if(m_hPid>0){                                                               //   Parent process >>
      m_nState |= PRC_DATASENT;                                                 //     Remember data have been sent
      m_nState |= PRC_RUNNING;                                                  //     Set running flag
      m_hThread = 0;                                                            //     Clear thread handle
      return O_K;                                                               //     Everything is fine
    }                                                                           //   <<
    if(m_hPid==0) return DoJobFork(iCaller,iFnc);                               //   The child process runs the function
    return IERROR(this,PRC_CANTSTART,"fork() failed",0,0);                      //   On error (fid<0) we return
  }                                                                             // <<
#endif
  // Start job in watcher thread                                                // ------------------------------------
  m_hPid = 0;                                                                   // Reset process id
  SendData();                                                                   // Send transfer data
  m_hThread = dlp_create_thread(DoJob,this);                                    // Do the job and watch it

  return O_K;                                                                   // Yo!
}
Exemple #4
0
/**
 * Try to uncompress a file. Return immidiately if it is not compressed. Otherwise
 * uncompress it to a temporary file and return the (temporary) filename in lpsOutfile.
 * The filename is returned in an internal static buffer. Thus any subsequent
 * calls destroy the value.<code>free</code> does not need to be called to
 * deallocate this pointer
 *
 * @param lpsInfile
 *       Pointer to filename of input file
 * @param lpsInfile
 *       Pointer to filename of output file
 * @return <code>TRUE</code> if file has been uncompressed, <code>FALSE</code> otherwise.
 */
BOOL dlp_funzip(const char *lpsInfile,char const **lpsOutfile)
{
#ifndef __NOZLIB
  char   lpsFile[L_PATH];
  char   lpsDir[L_PATH];
  BOOL   bSuccess       = TRUE;
    FILE  *lpOutfile      = NULL;
    gzFile lpInfile       = NULL;

  /* Do nothing if file is not compressed */
    lpInfile = gzopen(lpsInfile, "rb");
    if (!lpInfile) return FALSE;
    if (gzdirect(lpInfile))
    {
      gzclose(lpInfile);
      return FALSE;
    }

  /* Create and open temporary output file */
  dlp_splitpath(lpsInfile,lpsDir,lpsFile);
  *lpsOutfile = dlp_tempnam(NULL,lpsFile);
    lpOutfile = fopen(*lpsOutfile, "wb");
    if (lpOutfile == NULL)
    {
        perror(*lpsOutfile);
        gzclose(lpInfile);
        unlink(*lpsOutfile);
        return FALSE;
    }

    bSuccess = gz_uncompress(lpInfile, lpOutfile);

  if(!bSuccess)
  {
        unlink(*lpsOutfile);
    *lpsOutfile=lpsInfile;
    return FALSE;
  }

    return TRUE;
#else /* __NOZLIB */
  return FALSE;
#endif
}
Exemple #5
0
/**
 * Import midi notes of a midifile into data, needs external program midiconvert
 *
 * @param lpsFilename Name of file to import
 * @param iDst        Pointer to instance to import
 * @param lpsFiletype Type of file to import
 * @return <code>O_K</code> if successful, a (negative) error code otherwise
 */
INT16 CGEN_PROTECTED CDlpFile_Midi_ImportMidi
(
  CDlpFile*   _this,
  const char* lpsFilename,
  CDlpObject* iDst,
  const char* lpsFiletype
)
{
   char  lpsTempFile[L_PATH];
   char  lpsCmdline    [3*L_PATH] = "";
   INT16 nErr                     =O_K;

   strcpy(lpsTempFile,dlp_tempnam(NULL,"dlabpro_midi_import"));
   sprintf(lpsCmdline,"midiconvert %s %s", lpsFilename, lpsTempFile);

   if (system(lpsCmdline)!=0) { nErr=IERROR(_this,FIL_EXEC,lpsCmdline,0,0); }
   else {
      CData *idDst = AS(CData,iDst);
      /*Prepare data*/
      CData_Reset(iDst,TRUE);
      CData_AddComp(idDst,"CHAN",T_UCHAR);
      CData_AddComp(idDst,"VOL",T_UCHAR);
      CData_AddComp(idDst,"INST",T_UCHAR);
      CData_AddComp(idDst,"NOTE",T_UCHAR);
      CData_AddComp(idDst,"TIME",T_UINT);
      CData_AddComp(idDst,"LGTH",T_UINT);
      CData_AddComp(idDst,"VEL",T_UCHAR);
      /*import*/
      IF_NOK(CDlpFile_ImportAsciiToData(_this,lpsTempFile,iDst,"csv"))
        nErr = IERROR(iDst,FIL_IMPORT,lpsTempFile,"csv",0);
      /*Set midifilter specific data descriptions and clean data*/
      CData_SetDescr(idDst, DESCR0, CData_Dfetch(idDst,0,5));
      CData_DeleteRecs(idDst,0,1);
   }
   if (remove(lpsTempFile)==-1) nErr=IERROR(_this,FIL_REMOVE,"temporary ",lpsTempFile,0);
   /* Clean up */
   return nErr;
}
Exemple #6
0
/**
 * 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;
}