1 /*----------------------------------------------------------------------------
2                       filepoint.cc (objectlist interfaces)
3                        This file is a part of topaz systems
4                   Copyright: Hisao Kawaura, All rights reserved
5                                    1997 - 98
6 
7 
8     This program is free software; you can redistribute it and/or modify
9     it under the terms of the GNU General Public License as published by
10     the Free Software Foundation; either version 2 of the License, or
11     (at your option) any later version.
12 
13     This program is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17 
18     You should have received a copy of the GNU General Public License
19     along with this program; if not, write to the Free Software
20     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21 
22 ----------------------------------------------------------------------------*/
23 
24 #include <stdio.h>
25 #include <string>
26 #include "filepoint.h"
27 #include "pipecall.h"
28 
HandleElement()29 HandleElement::HandleElement()
30 {label = std::string(""); handle = 0;}
31 
operator =(const HandleElement & ele)32 HandleElement& HandleElement::operator = (const HandleElement&  ele)
33 {
34   label         = ele.label;
35   handle        = ele.handle;
36   handletype    = ele.handletype;
37   filename      = ele.filename;
38   return *this;
39 }
40 
FileHandleArray()41 FileHandleArray::FileHandleArray()
42 {
43   int i;
44   arraylength = 0;
45   increment = 100;
46 
47   bufflength = (arraylength / increment + 1) * increment;
48   element =   new HandleElement * [bufflength];
49 
50   for (i = 0; i < bufflength; i++)
51     element[i] = 0;
52 
53   for (i = 0; i < arraylength; i++)
54     element[i] = new HandleElement();
55 }
56 
~FileHandleArray()57 FileHandleArray::~FileHandleArray()
58 {
59   int i;
60   for (i = 0; i < arraylength; i++)
61     {
62       switch(element[i]->handletype)
63 	{
64 	case READFROMFILE:
65 	case WRITETOFILE:
66 	case APPENDTOFILE:
67 	  fclose(element[i]->handle);
68 	  break;
69 	case READFROMPIPE:
70 	case WRITETOPIPE:
71 	  pclose(element[i]->handle);
72 	  break;
73 	default:
74 	  return;
75 	}
76       delete element[i];
77     }
78   delete [] element;
79 
80 }
81 
search(std::string * label,int * SeachedIndex)82 bool FileHandleArray::search(std::string *label, int *SeachedIndex)
83 {
84   int i;
85 
86   for (i = 0; i < arraylength; i++)
87     {
88       if (*label == element[i]->label)
89 	{
90 	  *SeachedIndex = i;
91 	  return true;
92 	}
93     }
94   return false;
95 
96 }
97 
set(std::string * label,FILE ** h,int * handletype,std::string * filename)98 bool FileHandleArray::set(std::string *label, FILE **h, int *handletype, std::string *filename)
99 {
100   int itemp;
101 
102   if (search(label, &itemp) == true)    //overwrite
103     {
104       element[itemp]->handle = *h;
105       element[itemp]->handletype = *handletype;
106       element[itemp]->filename = *filename;
107       return true;
108     }
109   else                                  //append to last
110     {
111       add(label, h, handletype, filename);
112       return true;
113     }
114 }
115 
get(std::string * label,FILE ** h,int * handletype,std::string * filename)116 bool FileHandleArray::get(std::string *label, FILE **h, int *handletype, std::string *filename)
117 {
118   int itemp;
119 
120   if (search(label, &itemp) == true)
121     {
122       *h          = element[itemp]->handle;
123       *handletype = element[itemp]->handletype;
124       *filename   = element[itemp]->filename;
125       return true;
126     }
127   else if (*label == std::string("STDIN"))
128     {
129       *h          = stdin;
130       *handletype = READFROMFILE;
131       *filename   = std::string("");
132       return true;
133     }
134   else if (*label == std::string("STDOUT"))
135     {
136       *h          = stdout;
137       *handletype = WRITETOFILE;
138       *filename   = std::string("");
139       return true;
140     }
141   else if (*label == std::string("STDERR"))
142     {
143       *h          = stderr;
144       *handletype = WRITETOFILE;
145       *filename   = std::string("");
146       return true;
147     }
148   else
149      return false;
150 }
151 
getinfo(int i,std::string * label,int * handletype,std::string * filename)152 bool FileHandleArray::getinfo(int i, std::string *label, int *handletype, std::string *filename)
153 {
154   if (0 <= i && i < getarraylength())
155     {
156       *label      = element[i]->label;
157       *handletype = element[i]->handletype;
158       *filename   = element[i]->filename;
159       return true;
160     }
161   else
162      return false;
163 }
164 
165 
getarraylength()166 int FileHandleArray::getarraylength()
167 {
168   return arraylength;
169 }
170 
setbuff(int length)171 bool FileHandleArray::setbuff(int length)
172 {
173   int i;
174 
175   for (i = 0; i < arraylength; i++)
176     delete element[i];
177   delete [] element;
178 
179   arraylength = 0;
180   bufflength = (length / increment + 1) * increment;
181   element =  new HandleElement * [bufflength];
182   for (i = 0; i < bufflength; i++)
183     element[i] = 0;
184   return true;
185 }
186 
add(std::string * label,FILE ** h,int * handletype,std::string * filename)187 bool FileHandleArray::add(std::string *label, FILE **h, int *handletype, std::string *filename)
188 {
189   int itemp;
190   if (search(label, &itemp))
191     return false;
192   if (arraylength + 1 >= bufflength)
193       changebufflength(arraylength + 1);
194 
195   arraylength++;
196   element[arraylength - 1] = new HandleElement();
197   element[arraylength - 1]->label        = *label;
198   element[arraylength - 1]->handle       = *h;
199   element[arraylength - 1]->handletype   = *handletype;
200   element[arraylength - 1]->filename     = *filename;
201 
202   return true;
203 }
204 
remove(int pos)205 bool FileHandleArray::remove(int pos)
206 {
207   int i;
208 
209   for (i = pos; i < arraylength - 1; i++)
210     *element[i] = *element[i + 1];
211 
212   delete element[arraylength - 1];
213   arraylength--;
214   return true;
215 }
216 
changebufflength(int newlength)217 bool FileHandleArray::changebufflength(int newlength)
218 {
219 
220   int i;
221   HandleElement **tempstrarray;
222   int temparraylength;
223 
224   if (newlength <= bufflength)
225     return true;
226 
227   tempstrarray =   new HandleElement * [bufflength];
228   temparraylength = arraylength;
229 
230   for (i = 0; i < arraylength; i++)
231     tempstrarray[i] = element[i];
232 
233   for (i = arraylength; i < bufflength; i++)
234     tempstrarray[i] = 0;
235 
236   delete[] element;
237 
238   bufflength = (newlength / increment + 1) * increment;
239   element =  new HandleElement * [bufflength];
240   for (i = 0; i < bufflength; i++)
241     element[i] = 0;
242 
243   if (newlength > temparraylength)
244     {
245       for (i = 0; i < temparraylength; i++)
246 	element[i] = tempstrarray[i];
247       for (i = temparraylength; i < newlength; i++)
248 	element[i] = 0;
249 
250     }
251   else
252     {
253       for (i = 0; i < newlength; i++)
254 	element[i] = tempstrarray[i];
255     }
256 
257   delete [] tempstrarray;
258 
259   return true;
260 }
261 
262 
263 
264 
265