home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ctb_291.zip / source / stream.cpp < prev    next >
C/C++ Source or Header  |  1996-08-14  |  2KB  |  133 lines

  1. /*
  2. ** Module   :STREAM.CPP
  3. ** Abstract :Simple file i/o streams implementation
  4. **
  5. ** Copyright (C) Sergey I. Yevtushenko
  6. ** Log:
  7. ** Update : Sat  11-05-96
  8. ** Update : Wed  07-01-92
  9. */
  10.  
  11. #include <stream.hpp>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <io.h>
  15. #include <fcntl.h>
  16. #include <sys/stat.h>
  17.  
  18. #define BUFFER_SIZE 512
  19. #define OPEN_RW     O_RDWR|O_BINARY
  20. #define OPEN_CR     O_RDWR|O_BINARY|O_CREAT|O_TRUNC,S_IREAD|S_IWRITE
  21.  
  22. int File::open(char *name, char *ext, int a_mode)
  23. {
  24.     switch (mode)
  25.     {
  26.         case WR:
  27.             write(hnd, buff, count);
  28.         case RD:
  29.             close(hnd);
  30.             break;
  31.     }
  32.     if (!fname)
  33.         fname = new char[_MAX_PATH];
  34.     if (!fname)
  35.         return -1;
  36.     strcpy(fname, name);
  37.     if (!strchr(name, '.'))
  38.         strcat(fname, ext);
  39.     mode = a_mode;
  40.     if (!buff)
  41.         buff = new char[BUFFER_SIZE];
  42.     if (!buff)
  43.         return -1;
  44.     memset(buff, 0, BUFFER_SIZE);
  45.     switch (a_mode)
  46.     {
  47.         case RD:
  48.             hnd = ::open(fname, OPEN_RW);
  49.             if (hnd >= 0)
  50.             {
  51.                 count = ::read(hnd, buff, BUFFER_SIZE);
  52.                 pos = 0;
  53.             }
  54.             else
  55.             {
  56.                 mode = BAD;
  57.                 return -2;
  58.             }
  59.             break;
  60.         case WR:
  61.             hnd = ::open(fname, OPEN_CR);
  62.             if (hnd >= 0)
  63.             {
  64.                 count = 0;
  65.                 pos = 0;
  66.             }
  67.             else
  68.             {
  69.                 mode = BAD;
  70.                 return -3;
  71.             }
  72.             break;
  73.         default:
  74.             return -4;
  75.     }
  76.     return 0;
  77. }
  78. File::~File()
  79. {
  80.     switch (mode)
  81.     {
  82.         case WR:
  83.             ::write(hnd, buff, count);
  84.         case RD:
  85.             ::close(hnd);
  86.             break;
  87.     }
  88.     delete buff;
  89.     buff = 0;
  90.     delete fname;
  91.     fname = 0;
  92. }
  93. int File::get()
  94. {
  95.     if (mode == BAD || mode == WR || !count)
  96.         return -1;
  97.     if (count == pos)
  98.     {
  99.         count = ::read(hnd, buff, BUFFER_SIZE);
  100.         if (!count)
  101.             return -1;
  102.         pos = 0;
  103.     }
  104.     return buff[pos++];
  105. }
  106. int File::put(int ch)
  107. {
  108.     if (mode == BAD || mode == RD)
  109.         return -1;
  110.     buff[count++] = ch;
  111.     if (count == BUFFER_SIZE)
  112.     {
  113.         ::write(hnd, buff, BUFFER_SIZE);
  114.         count = 0;
  115.     }
  116.     return 0;
  117. }
  118. int File::put(char *str)
  119. {
  120.     int i;
  121.     if (str)
  122.     {
  123.         while (*str)
  124.         {
  125.             i = put(*str++);
  126.             if (i)
  127.                 return i;
  128.         }
  129.     }
  130.     return 0;
  131. }
  132.  
  133.