1 /**
2  *  Yudit Unicode Editor Source File
3  *
4  *  GNU Copyright (C) 1997-2006  Gaspar Sinai <gaspar@yudit.org>
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License, version 2,
8  *  dated June 1991. See file COPYYING for details.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */
19 
20 #ifndef SIO_h
21 #define SIO_h
22 
23 #include "SString.h"
24 #include "SStringVector.h"
25 #include "SEvent.h"
26 #include "SBinVector.h"
27 
28 /**
29  * @author: Gaspar Sinai <gaspar@yudit.org>
30  * @version: 2000-04-23
31  * This library is not multi-threaded. Therefor we need event handlers.
32  */
33 class SIO
34 {
35 public:
36   SIO (SEventSource::Type type);
37   virtual ~SIO();
38 
39   const SInputStream& getInputStream();
40   const SOutputStream& getOutputStream();
41 protected:
42   SInputStream  in;
43   SOutputStream out;
44 };
45 
46 class SFileImage
47 {
48 public:
49   SFileImage (void);
50   SFileImage (long fd, long size, bool write);
51   SFileImage (const SFileImage& in);
52   SFileImage& operator = (const SFileImage& in);
53   long size() const;
54   // Writing is not supported yet.
55   char* array();
56   char operator[] (unsigned int pos)  { return array()[pos]; }
57   ~SFileImage();
58 private:
59   void* shared;
60 };
61 
62 /**
63  * A directory in a filesystem
64  */
65 class SDir
66 {
67 public:
68   enum SEntry { SE_DIR, SE_FILE };
69   SDir (void);
70   SDir (const SString& name);
71   SDir (const SDir& dir);
72   SDir& operator = (const SDir& dir);
73   virtual ~SDir ();
74   SStringVector list (SEntry e);
75   SStringVector list (const SStringVector &patterns, SEntry e=SE_FILE);
76   SString getName() const;
77   SString getUnixName() const;
78   bool cd (const SString& newDir);
79   bool exists() const;
80   bool readable() const;
81   bool create() const;
82 private:
83   SString           name;
84 };
85 
86 class SStdIO : public SIO
87 {
88 public:
89   SStdIO ();
90   virtual ~SStdIO ();
91   const SInputStream& getInputStream();
92   const SOutputStream& getOutputStream();
93   const SOutputStream& getErrorOutputStream();
94 protected:
95   SOutputStream err;
96 };
97 
98 class SPipe : public SIO
99 {
100 public:
101   SPipe (const SString& command);
102   SPipe (void);
103   virtual ~SPipe ();
104   const SInputStream& getInputStream();
105   const SOutputStream& getOutputStream();
106   int wait ();
107 protected:
108   long openPipe(bool output);
109   SString command;
110   SBinVector<long> waitHandles;
111 };
112 
113 
114 class SFile : public SIO
115 {
116 public:
117   SFile (const SString& filename);
118   SFile (const SString& filen, const SStringVector& pt, bool lookcwd=true);
119   SFile (const SFile&);
120   SFile& operator = (const SFile&);
121   virtual ~SFile ();
122 
123   long        size();
124   bool        truncate (long _size);
125 
126   const SString& getName() const;
127   // Writing is not supported for non-mmap paltforms.
128   const SFileImage&   getFileImage(bool write=false);
129   const SInputStream& getInputStream();
130   const SOutputStream& getOutputStream();
131 
132 private:
133   SString   name;
134   SFileImage map;
135 };
136 
137 SString getTemporaryFileName ();
138 
139 class SSocket  : public SIO
140 {
141 public:
142   SSocket (const SString host, int port);
143   SSocket (const SSocket&);
144   SSocket& operator = (const SSocket&);
145   virtual ~SSocket ();
146 
147   const SInputStream& getInputStream();
148   const SOutputStream& getOutputStream();
149 private:
150   void openSocket(SOutputStream* o);
151   unsigned int address[6]; // Four is used now.
152   SString host;
153   int port;
154 };
155 
156 class SServer  : public SIO
157 {
158 public:
159   SServer (int port);
160   virtual ~SServer ();
161 };
162 
163 
164 #endif /* SIO_h */
165