home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / vsoup128.zip / mts.hh < prev    next >
Text File  |  1997-02-06  |  5KB  |  155 lines

  1. //  $Id: mts.hh 1.17 1997/02/06 09:54:36 hardy Exp $
  2. //
  3. //  This progam/module was written by Hardy Griech based on ideas and
  4. //  pieces of code from Chin Huang (cthuang@io.org).  Bug reports should
  5. //  be submitted to rgriech@ibm.net.
  6. //
  7. //  This file is part of soup++ for OS/2.  Soup++ including this file
  8. //  is freeware.  There is no warranty of any kind implied.  The terms
  9. //  of the GNU Gernal Public Licence are valid for this piece of software.
  10. //
  11. //  multithreading save library functions
  12. //
  13.  
  14.  
  15. #ifndef __MTS_HH__
  16. #define __MTS_HH__
  17.  
  18. #include <regexp.h>
  19. #include <stdio.h>
  20.  
  21. #if defined(OS2)  &&  defined(__MT__)
  22. #include <errno.h>
  23. #define BEGINTHREAD(f,arg)  { while (_beginthread((f),NULL,100000,(arg)) == -1) { \
  24.                                  if (errno != EINVAL) { \
  25.                      perror("beginthread"); \
  26.                      exit( EXIT_FAILURE ); }}}
  27. #else
  28. #define BEGINTHREAD(f,arg)  (f)(arg)
  29. #endif
  30.  
  31. #include "sema.hh"        // all semaphore classes included !
  32.  
  33.  
  34. class TFile {
  35.  
  36. public:
  37.     enum TMode {mnotopen,mread,mwrite,mreadwrite};
  38.     enum OMode {otext,obinary};
  39.  
  40. private:
  41.     int handle;
  42.     TMode mode;
  43.     unsigned char *buffer;
  44.     int buffSize;
  45.     int buffEnd;
  46.     int buffNdx;
  47.     int buffEof;
  48.  
  49. protected:
  50.     const char *fname;
  51.     long filePos;
  52.     long fileLen;
  53.     
  54. public:
  55.     TFile( void );
  56.     virtual ~TFile();
  57.  
  58.     virtual void close( int killif0=0, int forceKill=0 );
  59.     void remove( void );
  60.     int  open( const char *name, TMode mode, OMode textmode, int create=0 );
  61.     int  write( const void *buff, int bufflen );
  62.     int  putcc( char c );
  63.     int  fputs( const char *s );
  64.     int  printf( const char *fmt, ... ) __attribute__ ((format (printf, 2, 3)));
  65.     int  vprintf( const char *fmt, va_list arg_ptr );
  66.     int  read( void *buff, int bufflen );
  67.     int  getcc( void );
  68.     char *fgets( char *buff, int bufflen, int skipCrLf=0 );
  69.     int  scanf( const char *fmt, void *a1 );
  70. ////    int  scanf( const char *fmt, ... ) __attribute__ ((format (scanf, 2, 3)));
  71.     int  flush( void ) { return 1; };
  72.     virtual int  truncate( long length );
  73.     virtual long seek( long offset, int origin );
  74.     virtual long tell( void );
  75.  
  76.     const char *getName( void ) { return fname; };
  77.     int  isOpen( void ) { return mode != mnotopen; };
  78.  
  79. private:
  80.     void invalidateBuffer( void ) { buffEof = buffNdx = buffEnd = 0; };
  81.     int  fillBuff( void );
  82.  
  83. protected:
  84.     virtual int iread( void *buff, int bufflen );
  85.     virtual int iwrite( const void *buff, int bufflen );
  86. };
  87.  
  88.  
  89.  
  90. class TFileTmp: public TFile {
  91.  
  92. private:
  93.     int memBuffSize;
  94.     unsigned char *memBuff;
  95.  
  96. public:
  97.     TFileTmp( void );
  98.     ~TFileTmp();
  99.  
  100.     void close( void );
  101.     int  open( const char *pattern, int buffSize=40000 );
  102.     int  read( void *buff, int bufflen );
  103.     int  truncate( long offset );
  104.     long seek( long offset, int origin );
  105.  
  106. private:
  107.     virtual int iread( void *buff, int bufflen );
  108.     virtual int iwrite( const void *buff, int bufflen );
  109. };
  110.  
  111.  
  112. int hprintfT( int handle, const char *fmt, ... ) __attribute__ ((format (printf, 2, 3)));
  113. int hputsT( const char *s, int handle );
  114. int sprintfT( char *dst, const char *fmt, ... ) __attribute__ ((format (printf, 2, 3)));
  115. int vsprintfT( char *dst, const char *fmt, va_list arg_ptr );
  116. int printfT( const char *fmt, ... ) __attribute__ ((format (printf, 1, 2)));
  117. int vprintfT( const char *fmt, va_list arg_ptr );
  118. int sscanfT( const char *src, const char *fmt, ... ) __attribute__ ((format (scanf, 2, 3)));
  119.  
  120. size_t fwriteT( const void *buffer, size_t size, size_t count, FILE *out );
  121. FILE *popenT( const char *command, const char *mode );
  122. int pcloseT( FILE *io );
  123. int removeT( const char *fname );
  124. int renameT( const char *oldname, const char *newname );
  125.  
  126. regexp *regcompT( const char *exp );
  127. int regexecT( const regexp *cexp, const char *target );
  128.  
  129. const char *xstrdup( const char *src );
  130. void xstrdup( const char **dst, const char *src );
  131.  
  132.  
  133. //
  134. //  multithreading save counter
  135. //
  136. class TProtCounter {
  137. private:
  138.     TProtCounter( const TProtCounter &right );   // not allowed !
  139.     operator = ( const TProtCounter &right );    // not allowed !
  140. public:
  141.     TProtCounter( void ) { Cnt = 0; }
  142.     ~TProtCounter() {}
  143.     operator = ( const unsigned long &right ) { Cnt = right;  return *this; }
  144.  
  145.     TProtCounter & operator += (const unsigned long &offs) { blockThread();  Cnt += offs;  unblockThread();  return *this;  }
  146.     TProtCounter & operator ++(void) { blockThread();  ++Cnt;  unblockThread();  return *this; }
  147.     TProtCounter & operator --(void) { blockThread();  --Cnt;  unblockThread();  return *this; }
  148.     operator unsigned long() { unsigned long res;  blockThread();  res = Cnt;  unblockThread();  return res; }
  149. private:
  150.     unsigned long Cnt;
  151. };
  152.  
  153.  
  154. #endif   // __MTS_HH__
  155.