00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #define _STRNGRTR_SOURCE_
00024
00025 #include <stdio.h>
00026 #define _STDIO_INCLUDED_
00027 #include <stdlib.h>
00028 #include <string.h>
00029
00030 #include "setup.h"
00031
00032 #include "constant.h"
00033 #include "envrnmnt.h"
00034 #include "memalloc.h"
00035 #include "router.h"
00036 #include "sysdep.h"
00037
00038 #include "strngrtr.h"
00039
00040 #define READ_STRING 0
00041 #define WRITE_STRING 1
00042
00043
00044
00045
00046
00047 static int FindString(void *,char *);
00048 static int PrintString(void *,char *,char *);
00049 static int GetcString(void *,char *);
00050 static int UngetcString(void *,int,char *);
00051 static struct stringRouter *FindStringRouter(void *,char *);
00052 static int CreateReadStringSource(void *,char *,char *,size_t,size_t);
00053 static void DeallocateStringRouterData(void *);
00054
00055
00056
00057
00058 globle void InitializeStringRouter(
00059 void *theEnv)
00060 {
00061 AllocateEnvironmentData(theEnv,STRING_ROUTER_DATA,sizeof(struct stringRouterData),DeallocateStringRouterData);
00062
00063 EnvAddRouter(theEnv,"string",0,FindString,PrintString,GetcString,UngetcString,NULL);
00064 }
00065
00066
00067
00068
00069
00070 static void DeallocateStringRouterData(
00071 void *theEnv)
00072 {
00073 struct stringRouter *tmpPtr, *nextPtr;
00074
00075 tmpPtr = StringRouterData(theEnv)->ListOfStringRouters;
00076 while (tmpPtr != NULL)
00077 {
00078 nextPtr = tmpPtr->next;
00079 rm(theEnv,tmpPtr->name,strlen(tmpPtr->name) + 1);
00080 rtn_struct(theEnv,stringRouter,tmpPtr);
00081 tmpPtr = nextPtr;
00082 }
00083 }
00084
00085
00086
00087
00088 static int FindString(
00089 void *theEnv,
00090 char *fileid)
00091 {
00092 struct stringRouter *head;
00093
00094 head = StringRouterData(theEnv)->ListOfStringRouters;
00095 while (head != NULL)
00096 {
00097 if (strcmp(head->name,fileid) == 0)
00098 { return(TRUE); }
00099 head = head->next;
00100 }
00101
00102 return(FALSE);
00103 }
00104
00105
00106
00107
00108 static int PrintString(
00109 void *theEnv,
00110 char *logicalName,
00111 char *str)
00112 {
00113 struct stringRouter *head;
00114
00115 head = FindStringRouter(theEnv,logicalName);
00116 if (head == NULL)
00117 {
00118 SystemError(theEnv,"ROUTER",3);
00119 EnvExitRouter(theEnv,EXIT_FAILURE);
00120 }
00121
00122 if (head->readWriteType != WRITE_STRING) return(1);
00123
00124 if (head->maximumPosition == 0) return(1);
00125
00126 if ((head->currentPosition + 1) >= head->maximumPosition) return(1);
00127
00128 genstrncpy(&head->str[head->currentPosition],
00129 str,(STD_SIZE) (head->maximumPosition - head->currentPosition) - 1);
00130
00131 head->currentPosition += strlen(str);
00132
00133 return(1);
00134 }
00135
00136
00137
00138
00139 static int GetcString(
00140 void *theEnv,
00141 char *logicalName)
00142 {
00143 struct stringRouter *head;
00144 int rc;
00145
00146 head = FindStringRouter(theEnv,logicalName);
00147 if (head == NULL)
00148 {
00149 SystemError(theEnv,"ROUTER",1);
00150 EnvExitRouter(theEnv,EXIT_FAILURE);
00151 }
00152
00153 if (head->readWriteType != READ_STRING) return(EOF);
00154 if (head->currentPosition >= head->maximumPosition)
00155 {
00156 head->currentPosition++;
00157 return(EOF);
00158 }
00159
00160 rc = (unsigned char) head->str[head->currentPosition];
00161 head->currentPosition++;
00162
00163 return(rc);
00164 }
00165
00166
00167
00168
00169 #if WIN_BTC
00170 #pragma argsused
00171 #endif
00172 static int UngetcString(
00173 void *theEnv,
00174 int ch,
00175 char *logicalName)
00176 {
00177 struct stringRouter *head;
00178 #if MAC_MCW || WIN_MCW || MAC_XCD
00179 #pragma unused(ch)
00180 #endif
00181
00182 head = FindStringRouter(theEnv,logicalName);
00183
00184 if (head == NULL)
00185 {
00186 SystemError(theEnv,"ROUTER",2);
00187 EnvExitRouter(theEnv,EXIT_FAILURE);
00188 }
00189
00190 if (head->readWriteType != READ_STRING) return(0);
00191 if (head->currentPosition > 0)
00192 { head->currentPosition--; }
00193
00194 return(1);
00195 }
00196
00197
00198
00199
00200 globle int OpenStringSource(
00201 void *theEnv,
00202 char *name,
00203 char *str,
00204 size_t currentPosition)
00205 {
00206 size_t maximumPosition;
00207
00208 if (str == NULL)
00209 {
00210 currentPosition = 0;
00211 maximumPosition = 0;
00212 }
00213 else
00214 { maximumPosition = strlen(str); }
00215
00216 return(CreateReadStringSource(theEnv,name,str,currentPosition,maximumPosition));
00217 }
00218
00219
00220
00221
00222
00223 globle int OpenTextSource(
00224 void *theEnv,
00225 char *name,
00226 char *str,
00227 size_t currentPosition,
00228 size_t maximumPosition)
00229 {
00230 if (str == NULL)
00231 {
00232 currentPosition = 0;
00233 maximumPosition = 0;
00234 }
00235
00236 return(CreateReadStringSource(theEnv,name,str,currentPosition,maximumPosition));
00237 }
00238
00239
00240
00241
00242 static int CreateReadStringSource(
00243 void *theEnv,
00244 char *name,
00245 char *str,
00246 size_t currentPosition,
00247 size_t maximumPosition)
00248 {
00249 struct stringRouter *newStringRouter;
00250
00251 if (FindStringRouter(theEnv,name) != NULL) return(0);
00252
00253 newStringRouter = get_struct(theEnv,stringRouter);
00254 newStringRouter->name = (char *) gm1(theEnv,strlen(name) + 1);
00255 genstrcpy(newStringRouter->name,name);
00256 newStringRouter->str = str;
00257 newStringRouter->currentPosition = currentPosition;
00258 newStringRouter->readWriteType = READ_STRING;
00259 newStringRouter->maximumPosition = maximumPosition;
00260 newStringRouter->next = StringRouterData(theEnv)->ListOfStringRouters;
00261 StringRouterData(theEnv)->ListOfStringRouters = newStringRouter;
00262
00263 return(1);
00264 }
00265
00266
00267
00268
00269 globle int CloseStringSource(
00270 void *theEnv,
00271 char *name)
00272 {
00273 struct stringRouter *head, *last;
00274
00275 last = NULL;
00276 head = StringRouterData(theEnv)->ListOfStringRouters;
00277 while (head != NULL)
00278 {
00279 if (strcmp(head->name,name) == 0)
00280 {
00281 if (last == NULL)
00282 {
00283 StringRouterData(theEnv)->ListOfStringRouters = head->next;
00284 rm(theEnv,head->name,strlen(head->name) + 1);
00285 rtn_struct(theEnv,stringRouter,head);
00286 return(1);
00287 }
00288 else
00289 {
00290 last->next = head->next;
00291 rm(theEnv,head->name,strlen(head->name) + 1);
00292 rtn_struct(theEnv,stringRouter,head);
00293 return(1);
00294 }
00295 }
00296 last = head;
00297 head = head->next;
00298 }
00299
00300 return(0);
00301 }
00302
00303
00304
00305
00306 globle int OpenStringDestination(
00307 void *theEnv,
00308 char *name,
00309 char *str,
00310 size_t maximumPosition)
00311 {
00312 struct stringRouter *newStringRouter;
00313
00314 if (FindStringRouter(theEnv,name) != NULL) return(0);
00315
00316 newStringRouter = get_struct(theEnv,stringRouter);
00317 newStringRouter->name = (char *) gm1(theEnv,(int) strlen(name) + 1);
00318 genstrcpy(newStringRouter->name,name);
00319 newStringRouter->str = str;
00320 newStringRouter->currentPosition = 0;
00321 newStringRouter->readWriteType = WRITE_STRING;
00322 newStringRouter->maximumPosition = maximumPosition;
00323 newStringRouter->next = StringRouterData(theEnv)->ListOfStringRouters;
00324 StringRouterData(theEnv)->ListOfStringRouters = newStringRouter;
00325
00326 return(1);
00327 }
00328
00329
00330
00331
00332 globle int CloseStringDestination(
00333 void *theEnv,
00334 char *name)
00335 {
00336 return(CloseStringSource(theEnv,name));
00337 }
00338
00339
00340
00341
00342 static struct stringRouter *FindStringRouter(
00343 void *theEnv,
00344 char *name)
00345 {
00346 struct stringRouter *head;
00347
00348 head = StringRouterData(theEnv)->ListOfStringRouters;
00349 while (head != NULL)
00350 {
00351 if (strcmp(head->name,name) == 0)
00352 { return(head); }
00353 head = head->next;
00354 }
00355
00356 return(NULL);
00357 }
00358
00359
00360
00361