Logo Search packages:      
Sourcecode: sam2p version File versions

FILE * Filter::UngetFILED::getFILE ( bool  seekable_p  ) 

Reading (this) and reading the returned FILE* will be equivalent (unless this->getUnget() is used later). Creates a temporary file if this->unget is not empty.

Parameters:
seekable_p if true, the returned FILE* must be seekable. Possibly creates a temporary file.

Definition at line 319 of file gensio.cpp.

References SimBuffer::Flat::getLength(), GenBuffer::isEmpty(), isSeekable(), Error::sev(), and SimBuffer::B::term0().

Referenced by seek().

                                               {
  FILE *tf;
  if (!unget.isEmpty() || (seekable_p && !isSeekable())) { do_temp:
    /* must create a temporary file */
    SimBuffer::B tmpnam;
    if (filename==NULLP) Error::sev(Error::NOTICE) << "Filter::UngetFILED" << ": using temp for" << " `-' (stdin)" << (Error*)0;
                else Error::sev(Error::NOTICE) << "Filter::UngetFILED" << ": using temp for" << ": " << FNQ2(filename,strlen(filename)) << (Error*)0;
    if (NULLP==(tf=Files::open_tmpnam(tmpnam, "wb+"))) {
      if (filename==NULLP) Error::sev(Error::EERROR) << "Filter::UngetFILED" << ": cannot open temp file for" << " `-' (stdin)" << (Error*)0;
                      else Error::sev(Error::EERROR) << "Filter::UngetFILED" << ": cannot open temp file for" << ": " << FNQ2(filename,strlen(filename)) << (Error*)0;
    }
    tmpnam.term0();
    /* vvv change filename, so CM_unlinkp can work */
    if (filename!=NULLP) {
      if (0!=(closeMode&CM_unlinkp)) { remove(filename); }
      delete [] filename;
    }
    strcpy(const_cast<char*>(filename=new char[tmpnam.getLength()+1]), tmpnam());
    Files::tmpRemoveCleanup(filename);
    if (unget.getLength()-ofs==fwrite(unget()+ofs, 1, unget.getLength()-ofs, tf)) {
      static const slen_t BUFSIZE=4096; /* BUGFIX at Sat Apr 19 15:43:59 CEST 2003 */
      char *buf=new char[BUFSIZE];
      unsigned got;
      // fprintf(stderr, "ftf=%ld ofs=%d\n", ftell(f), ofs);
      while ((0<(got=fread(buf, 1, BUFSIZE, f)))
             && got==fwrite(buf, 1, got, tf)) {}
      // fprintf(stderr,"got=%d ftell=%d\n", got, ftell(tf));
      delete [] buf;
    }
    unget.forgetAll(); ofs=0;
    fflush(tf); rewind(tf);
    if (ferror(tf) || ferror(f))
      Error::sev(Error::EERROR) << "Filter::UngetFILED" << ": cannot write temp file" << (Error*)0;
    if (0!=(closeMode&CM_closep)) fclose(f);
    closeMode|=CM_closep|CM_unlinkp; /* close and unlink the temporary file */
    /* ^^^ Imp: verify VC++ compilation, +others */
    f=tf;
  } else if (f==NULLP) { /* no real file open */
    #if OS_COTY==COTY_UNIX
      tf=fopen("/dev/null","rb");
    #else
      #if OS_COPTY==COTY_WIN9X || OS_COTY==COTY_WINNT
        tf=fopen("nul","rb");
      #else
        tf=(FILE*)NULLP;
      #endif
    #endif
    if (tf==NULLP) goto do_temp; /* perhaps inside chroot() */
    close();
    closeMode|=CM_closep|CM_unlinkp; /* close and unlink the temporary file */
    f=tf;
  }
  closeMode|=CM_seekablep;
  return f;
}


Generated by  Doxygen 1.6.0   Back to index