00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #define _FILERTR_SOURCE_
00029
00030 #include <stdio.h>
00031 #define _STDIO_INCLUDED_
00032 #include <string.h>
00033
00034 #include "setup.h"
00035
00036 #include "constant.h"
00037 #include "envrnmnt.h"
00038 #include "memalloc.h"
00039 #include "router.h"
00040 #include "sysdep.h"
00041
00042 #include "filertr.h"
00043
00044
00045
00046
00047
00048 static int ExitFile(void *,int);
00049 static int PrintFile(void *,char *,char *);
00050 static int GetcFile(void *,char *);
00051 static int UngetcFile(void *,int,char *);
00052 static void DeallocateFileRouterData(void *);
00053
00054
00055
00056
00057 globle void InitializeFileRouter(
00058 void *theEnv)
00059 {
00060 AllocateEnvironmentData(theEnv,FILE_ROUTER_DATA,sizeof(struct fileRouterData),DeallocateFileRouterData);
00061
00062 EnvAddRouter(theEnv,"fileio",0,FindFile,
00063 PrintFile,GetcFile,
00064 UngetcFile,ExitFile);
00065 }
00066
00067
00068
00069
00070
00071 static void DeallocateFileRouterData(
00072 void *theEnv)
00073 {
00074 struct fileRouter *tmpPtr, *nextPtr;
00075
00076 tmpPtr = FileRouterData(theEnv)->ListOfFileRouters;
00077 while (tmpPtr != NULL)
00078 {
00079 nextPtr = tmpPtr->next;
00080 GenClose(theEnv,tmpPtr->stream);
00081 rm(theEnv,tmpPtr->logicalName,strlen(tmpPtr->logicalName) + 1);
00082 rtn_struct(theEnv,fileRouter,tmpPtr);
00083 tmpPtr = nextPtr;
00084 }
00085 }
00086
00087
00088
00089
00090
00091 globle FILE *FindFptr(
00092 void *theEnv,
00093 char *logicalName)
00094 {
00095 struct fileRouter *fptr;
00096
00097
00098
00099
00100
00101 if (strcmp(logicalName,"stdout") == 0)
00102 { return(stdout); }
00103 else if (strcmp(logicalName,"stdin") == 0)
00104 { return(stdin); }
00105 else if (strcmp(logicalName,WTRACE) == 0)
00106 { return(stdout); }
00107 else if (strcmp(logicalName,WDIALOG) == 0)
00108 { return(stdout); }
00109 else if (strcmp(logicalName,WPROMPT) == 0)
00110 { return(stdout); }
00111 else if (strcmp(logicalName,WDISPLAY) == 0)
00112 { return(stdout); }
00113 else if (strcmp(logicalName,WERROR) == 0)
00114 { return(stdout); }
00115 else if (strcmp(logicalName,WWARNING) == 0)
00116 { return(stdout); }
00117
00118
00119
00120
00121
00122 fptr = FileRouterData(theEnv)->ListOfFileRouters;
00123 while ((fptr != NULL) ? (strcmp(logicalName,fptr->logicalName) != 0) : FALSE)
00124 { fptr = fptr->next; }
00125
00126 if (fptr != NULL) return(fptr->stream);
00127
00128 return(NULL);
00129 }
00130
00131
00132
00133
00134
00135
00136
00137
00138 globle int FindFile(
00139 void *theEnv,
00140 char *logicalName)
00141 {
00142 if (FindFptr(theEnv,logicalName) != NULL) return(TRUE);
00143
00144 return(FALSE);
00145 }
00146
00147
00148
00149
00150 #if WIN_BTC
00151 #pragma argsused
00152 #endif
00153 static int ExitFile(
00154 void *theEnv,
00155 int num)
00156 {
00157 #if MAC_MCW || WIN_MCW || MAC_XCD
00158 #pragma unused(num)
00159 #endif
00160 #if IO_FUNCTIONS
00161 CloseAllFiles(theEnv);
00162 #else
00163 #if MAC_MCW || WIN_MCW || MAC_XCD
00164 #pragma unused(theEnv)
00165 #endif
00166 #endif
00167 return(1);
00168 }
00169
00170
00171
00172
00173 static int PrintFile(
00174 void *theEnv,
00175 char *logicalName,
00176 char *str)
00177 {
00178 FILE *fptr;
00179
00180 fptr = FindFptr(theEnv,logicalName);
00181
00182 genprintfile(theEnv,fptr,str);
00183
00184 return(1);
00185 }
00186
00187
00188
00189
00190 static int GetcFile(
00191 void *theEnv,
00192 char *logicalName)
00193 {
00194 FILE *fptr;
00195 int theChar;
00196
00197 fptr = FindFptr(theEnv,logicalName);
00198
00199 if (fptr == stdin)
00200 { theChar = gengetchar(theEnv); }
00201 else
00202 { theChar = getc(fptr); }
00203
00204
00205
00206
00207
00208
00209 if ((fptr == stdin) && (theChar == EOF)) clearerr(stdin);
00210
00211 return(theChar);
00212 }
00213
00214
00215
00216
00217 static int UngetcFile(
00218 void *theEnv,
00219 int ch,
00220 char *logicalName)
00221 {
00222 FILE *fptr;
00223
00224 fptr = FindFptr(theEnv,logicalName);
00225
00226 if (fptr == stdin)
00227 { return(genungetchar(theEnv,ch)); }
00228 else
00229 { return(ungetc(ch,fptr)); }
00230 }
00231
00232
00233
00234
00235
00236
00237
00238 globle int OpenAFile(
00239 void *theEnv,
00240 char *fileName,
00241 char *accessMode,
00242 char *logicalName)
00243 {
00244 FILE *newstream;
00245 struct fileRouter *newRouter;
00246
00247
00248
00249
00250
00251
00252 if ((newstream = GenOpen(theEnv,fileName,accessMode)) == NULL)
00253 { return(FALSE); }
00254
00255
00256
00257
00258
00259 newRouter = get_struct(theEnv,fileRouter);
00260 newRouter->logicalName = (char *) gm2(theEnv,strlen(logicalName) + 1);
00261 genstrcpy(newRouter->logicalName,logicalName);
00262 newRouter->stream = newstream;
00263
00264
00265
00266
00267
00268
00269 newRouter->next = FileRouterData(theEnv)->ListOfFileRouters;
00270 FileRouterData(theEnv)->ListOfFileRouters = newRouter;
00271
00272
00273
00274
00275
00276
00277 return(TRUE);
00278 }
00279
00280
00281
00282
00283
00284
00285 globle int CloseFile(
00286 void *theEnv,
00287 char *fid)
00288 {
00289 struct fileRouter *fptr, *prev;
00290
00291 for (fptr = FileRouterData(theEnv)->ListOfFileRouters, prev = NULL;
00292 fptr != NULL;
00293 fptr = fptr->next)
00294 {
00295 if (strcmp(fptr->logicalName,fid) == 0)
00296 {
00297 GenClose(theEnv,fptr->stream);
00298 rm(theEnv,fptr->logicalName,strlen(fptr->logicalName) + 1);
00299 if (prev == NULL)
00300 { FileRouterData(theEnv)->ListOfFileRouters = fptr->next; }
00301 else
00302 { prev->next = fptr->next; }
00303 rm(theEnv,fptr,(int) sizeof(struct fileRouter));
00304
00305 return(TRUE);
00306 }
00307
00308 prev = fptr;
00309 }
00310
00311 return(FALSE);
00312 }
00313
00314
00315
00316
00317
00318
00319 globle int CloseAllFiles(
00320 void *theEnv)
00321 {
00322 struct fileRouter *fptr, *prev;
00323
00324 if (FileRouterData(theEnv)->ListOfFileRouters == NULL) return(FALSE);
00325
00326 fptr = FileRouterData(theEnv)->ListOfFileRouters;
00327
00328 while (fptr != NULL)
00329 {
00330 GenClose(theEnv,fptr->stream);
00331 prev = fptr;
00332 rm(theEnv,fptr->logicalName,strlen(fptr->logicalName) + 1);
00333 fptr = fptr->next;
00334 rm(theEnv,prev,(int) sizeof(struct fileRouter));
00335 }
00336
00337 FileRouterData(theEnv)->ListOfFileRouters = NULL;
00338
00339 return(TRUE);
00340 }
00341
00342
00343