Main Page   Modules   Alphabetical List   Compound List   File List   Compound Members   File Members  

psstream.c

Go to the documentation of this file.
00001 /*  $Id: psstream.c,v 1.3 2000/12/02 07:26:03 dbryson Exp $
00002 **
00003 **  Copyright (c) 2000 Derry Bryson <derry@techass.com>
00004 **
00005 **  Description:
00006 **  
00007 **    Postscript Library
00008 **
00009 **
00010 **  License:
00011 **
00012 **    This library is free software; you can redistribute it and/or
00013 **    modify it under the terms of the GNU Lesser General Public
00014 **    License as published by the Free Software Foundation; either
00015 **    version 2.1 of the License, or (at your option) any later version.
00016 **
00017 **    This library is distributed in the hope that it will be useful,
00018 **    but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 **    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020 **    Lesser General Public License for more details.
00021 **
00022 **    You should have received a copy of the GNU Lesser General Public
00023 **    License along with this library; if not, write to the Free Software
00024 **    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 **
00026 **
00027 **  Contact:
00028 **
00029 **      Mail:
00030 **
00031 **        Technology Associates, Inc.
00032 **        LIBPS Project
00033 **        1455 Deming Way #11
00034 **        Sparks, NV  89431
00035 **        USA
00036 **
00037 **      Email:
00038 **
00039 **        libps@techass.com
00040 **
00041 **      See our website at:
00042 **
00043 **        libps.sourceforge.net
00044 **
00045 */
00046 
00075 #ifdef HAVE_CONFIG_H
00076 # include "psconfig.h"
00077 #endif
00078 
00079 #include <stdio.h>
00080 #include <stdlib.h>
00081 #include <stdarg.h>
00082 
00083 #include "ps/ps.h"
00084 
00093 PSStreamFuncs
00094   psStandardStreamFuncs =
00095   {
00096     (psFileRead)fread,          
00097     (psFileWrite)fwrite,        
00098     (psFileTell)ftell,          
00099     (psFileSeek)fseek
00100   };
00101 
00109 PSStreamFuncs*
00110 psStreamFuncsAlloc(void)
00111 {
00112   return calloc(1, sizeof(PSStreamFuncs));
00113 }
00114 
00123 void
00124 psStreamFuncsFree(PSStreamFuncs *funcs)
00125 {
00126   if(funcs)
00127     free(funcs);
00128 }
00129 
00137 PSStream*
00138 psStreamAlloc(void* stream, PSStreamFuncs* funcs)
00139 {
00140   PSStream
00141     *str;
00142     
00143   str = calloc(1, sizeof(PSStream));
00144   if(str)
00145   {
00146     str->ungetchar = -1;
00147     str->stream = stream;
00148     str->funcs = funcs;
00149   }
00150   
00151   return str;
00152 }
00153 
00162 void 
00163 psStreamFree(PSStream *str)
00164 {
00165   if(str)
00166     free(str);
00167 }
00168 
00183 size_t 
00184 psRead(void* ptr, size_t size, size_t nmemb, PSStream* str)
00185 {
00186   int
00187     n;
00188     
00189   /*
00190   **  Note:  should probably support ungetchar here somehow.
00191   */
00192   
00193   if(str->funcs->read)
00194   {
00195     n = str->funcs->read(ptr, size, nmemb, str->stream);
00196     if(n != nmemb)
00197       str->eof = 1;
00198 #if 0      
00199     if(!n)
00200       pserrno = PSERR_STREAMREADERROR;
00201 #endif      
00202     return n;
00203   }
00204 
00205   pserrno = PSERR_STREAMREADERROR;    
00206   return 0;
00207 }
00208 
00223 size_t 
00224 psWrite(const void* ptr, size_t size, size_t nmemb, PSStream* str)
00225 {
00226   int 
00227     n;
00228     
00229   if(str->funcs->write)
00230   {
00231     n = str->funcs->write(ptr, size, nmemb, str->stream);
00232     if(!n)
00233       pserrno = PSERR_STREAMWRITEERROR;
00234     return n;
00235   }
00236     
00237   return 0;
00238 }
00239 
00250 long 
00251 psTell(PSStream* str)
00252 {
00253   if(str->funcs->tell)
00254     return str->funcs->tell(str->stream);
00255     
00256   pserrno = PSERR_STREAMTELLERROR;
00257   return -1;
00258 }
00259 
00272 int 
00273 psSeek(PSStream* str, long offset, int whence)
00274 {
00275   str->ungetchar = -1;
00276   
00277   if(str->funcs->seek)
00278   {
00279     str->eof = 0;
00280     return str->funcs->seek(str->stream, offset, whence);
00281   }
00282     
00283   pserrno = PSERR_STREAMSEEKERROR;
00284   return -1;
00285 }
00286 
00297 int 
00298 psGetC(PSStream* str)
00299 {
00300   unsigned char
00301     c;
00302   
00303   if(str->ungetchar != -1)
00304   {
00305     c = str->ungetchar;
00306     str->ungetchar = -1;
00307     return c;
00308   }
00309   
00310   if(psRead(&c, 1, 1, str) == 1)
00311     return c;
00312     
00313   str->eof = 1;
00314   return EOF;
00315 }
00316 
00329 int 
00330 psUngetC(int c, PSStream* str)
00331 {
00332   str->eof = 0;
00333   
00334   if(str->ungetchar != -1)
00335     return 0;
00336     
00337   str->ungetchar = c;
00338   return c;
00339 }
00340 
00352 int 
00353 psPutC(int c, PSStream* str)
00354 {
00355   unsigned char
00356     buf;
00357     
00358   buf = (unsigned char)c;
00359   if(psWrite(&buf, 1, 1, str) != 1)
00360     return EOF;
00361     
00362   return (int)buf; 
00363 }
00364 
00380 char*
00381 psGetS(char* s, int size, PSStream* str)
00382 {
00383   int
00384     c,
00385     i;
00386     
00387   char
00388     *p = s;
00389     
00390   for(i = 0; i < (size - 1) && (c = psGetC(str)) != EOF; i++)
00391   {
00392     *p++ = c;
00393     if(c == '\n')
00394       break;
00395   }
00396     
00397   *p = 0;
00398     
00399   return i ? s : NULL;
00400 }
00401 
00414 int 
00415 psPutS(char* s, PSStream* str)
00416 {
00417   if(psWrite(s, 1, strlen(s), str) != strlen(s))  
00418     return EOF;
00419   return 1;
00420 }
00421 
00433 int
00434 psEof(PSStream* str)
00435 {
00436   return str->eof;
00437 }
00438 
00450 int
00451 psPrintf(PSStream* str, char *format, ...)
00452 {
00453   char
00454     buf[PS_PRINTF_BUF_LEN];
00455     
00456   va_list 
00457     val;
00458     
00459   va_start(val, format);
00460 #ifdef HAVE_VSNPRINTF  
00461   vsnprintf(buf, PS_PRINTF_BUF_LEN, format, val);
00462 #else
00463   vprintf(buf, format, val);
00464 #endif  
00465   va_end(val);
00466   return psWrite(buf, strlen(buf), 1, str);
00467 }

Generated at Mon Dec 11 22:46:27 2000 for Postscript Utility Library by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000