Logo Search packages:      
Sourcecode: djvulibre version File versions

GP< ByteStream > ByteStream::create ( const int  fd,
char const *const   mode,
const bool  closeme 
) [static, inherited]

Constructs a ByteStream for accessing the stdio file f#. Argument mode# indicates the type of the stdio file, as in the well known stdio function fopen#. Destroying the ByteStream object will not close the stdio file f# unless closeme is true.

Definition at line 1189 of file ByteStream.cpp.

References ByteStream::Stdio::init(), and GBaseString::length().

{
  GP<ByteStream> retval;
  const char *default_mode="rb";
#if HAS_MEMMAP
  if (   (!mode&&(fd!=0)&&(fd!=1)&&(fd!=2)) 
      || (mode&&(GUTF8String("rb") == mode)))
  {
    MemoryMapByteStream *rb=new MemoryMapByteStream();
    retval=rb;
    GUTF8String errmessage=rb->init(fd,closeme);
    if(errmessage.length())
    {
      retval=0;
    }
  }
  if(!retval)
#endif
  {
    int fd2 = fd;
    FILE *f = 0;
    if (fd == 0 && !closeme 
        && (!mode || mode[0]=='r') )
      {
        f=stdin;
        default_mode = "r";
        fd2=(-1);
      }
    else if (fd == 1 && !closeme 
             && (!mode || mode[0]=='a' || mode[0]=='w') )
      {
        default_mode = "a";
        f=stdout;
        fd2 = -1;
      }
    else if (fd == 2 && !closeme
             && (!mode || mode[0]=='a' || mode[0]=='w') )
      {
        default_mode = "a";
        f=stderr;
        fd2 = -1;
      }
    else
      {
#ifndef UNDER_CE
        if(!closeme)
          {
            fd2 = dup(fd);
          } 
        f=fdopen(fd2,(char*)(mode?mode:default_mode));
#else
        if(!closeme)
          {
            fd2 = -1;
          }
#endif
      }

    if(!f)
      {
#ifndef UNDER_CE
        if(fd2>= 0)
          close(fd2);
#endif
        G_THROW( ERR_MSG("ByteStream.open_fail2") );
      }
    Stdio *sbs=new Stdio();
    retval=sbs;
    GUTF8String errmessage=sbs->init(f,mode?mode:default_mode,(fd2>=0));
    if(errmessage.length())
      {
        G_THROW(errmessage);
      }
  }
  return retval;
}


Generated by  Doxygen 1.6.0   Back to index