1 /*******************************************************/
2 /* "C" Language Integrated Production System */
4 /* CLIPS Version 6.30 08/16/14 */
6 /* STRING I/O ROUTER MODULE */
7 /*******************************************************/
9 /*************************************************************/
10 /* Purpose: I/O Router routines which allow strings to be */
11 /* used as input and output sources. */
13 /* Principal Programmer(s): */
16 /* Contributing Programmer(s): */
19 /* Revision History: */
21 /* 6.30: Used genstrcpy instead of strcpy. */
23 /* Removed conditional code for unsupported */
24 /* compilers/operating systems (IBM_MCW, */
25 /* MAC_MCW, and IBM_TBC). */
27 /* Changed integer type/precision. */
29 /* Added const qualifiers to remove C++ */
30 /* deprecation warnings. */
32 /*************************************************************/
34 #define _STRNGRTR_SOURCE_
37 #define _STDIO_INCLUDED_
52 #define WRITE_STRING 1
54 /***************************************/
55 /* LOCAL INTERNAL FUNCTION DEFINITIONS */
56 /***************************************/
58 static int FindString(void *,const char *);
59 static int PrintString(void *,const char *,const char *);
60 static int GetcString(void *,const char *);
61 static int UngetcString(void *,int,const char *);
62 static struct stringRouter *FindStringRouter(void *,const char *);
63 static int CreateReadStringSource(void *,const char *,const char *,size_t,size_t);
64 static void DeallocateStringRouterData(void *);
66 /**********************************************************/
67 /* InitializeStringRouter: Initializes string I/O router. */
68 /**********************************************************/
69 globle void InitializeStringRouter(
72 AllocateEnvironmentData(theEnv,STRING_ROUTER_DATA,sizeof(struct stringRouterData),DeallocateStringRouterData);
74 EnvAddRouter(theEnv,"string",0,FindString,PrintString,GetcString,UngetcString,NULL);
77 /*******************************************/
78 /* DeallocateStringRouterData: Deallocates */
79 /* environment data for string routers. */
80 /*******************************************/
81 static void DeallocateStringRouterData(
84 struct stringRouter *tmpPtr, *nextPtr;
86 tmpPtr = StringRouterData(theEnv)->ListOfStringRouters;
87 while (tmpPtr != NULL)
89 nextPtr = tmpPtr->next;
90 rm(theEnv,(void *) tmpPtr->name,strlen(tmpPtr->name) + 1);
91 rtn_struct(theEnv,stringRouter,tmpPtr);
96 /*************************************************************/
97 /* FindString: Find routine for string router logical names. */
98 /*************************************************************/
99 static int FindString(
103 struct stringRouter *head;
105 head = StringRouterData(theEnv)->ListOfStringRouters;
108 if (strcmp(head->name,fileid) == 0)
116 /**************************************************/
117 /* PrintString: Print routine for string routers. */
118 /**************************************************/
119 static int PrintString(
121 const char *logicalName,
124 struct stringRouter *head;
126 head = FindStringRouter(theEnv,logicalName);
129 SystemError(theEnv,"ROUTER",3);
130 EnvExitRouter(theEnv,EXIT_FAILURE);
133 if (head->readWriteType != WRITE_STRING) return(1);
135 if (head->maximumPosition == 0) return(1);
137 if ((head->currentPosition + 1) >= head->maximumPosition) return(1);
139 genstrncpy(&head->writeString[head->currentPosition],
140 str,(STD_SIZE) (head->maximumPosition - head->currentPosition) - 1);
142 head->currentPosition += strlen(str);
147 /************************************************/
148 /* GetcString: Getc routine for string routers. */
149 /************************************************/
150 static int GetcString(
152 const char *logicalName)
154 struct stringRouter *head;
157 head = FindStringRouter(theEnv,logicalName);
160 SystemError(theEnv,"ROUTER",1);
161 EnvExitRouter(theEnv,EXIT_FAILURE);
164 if (head->readWriteType != READ_STRING) return(EOF);
165 if (head->currentPosition >= head->maximumPosition)
167 head->currentPosition++;
171 rc = (unsigned char) head->readString[head->currentPosition];
172 head->currentPosition++;
177 /****************************************************/
178 /* UngetcString: Ungetc routine for string routers. */
179 /****************************************************/
180 static int UngetcString(
183 const char *logicalName)
185 struct stringRouter *head;
190 head = FindStringRouter(theEnv,logicalName);
194 SystemError(theEnv,"ROUTER",2);
195 EnvExitRouter(theEnv,EXIT_FAILURE);
198 if (head->readWriteType != READ_STRING) return(0);
199 if (head->currentPosition > 0)
200 { head->currentPosition--; }
205 /************************************************/
206 /* OpenStringSource: Opens a new string router. */
207 /************************************************/
208 globle int OpenStringSource(
212 size_t currentPosition)
214 size_t maximumPosition;
222 { maximumPosition = strlen(str); }
224 return(CreateReadStringSource(theEnv,name,str,currentPosition,maximumPosition));
227 /******************************************************/
228 /* OpenTextSource: Opens a new string router for text */
229 /* (which is not NULL terminated). */
230 /******************************************************/
231 globle int OpenTextSource(
235 size_t currentPosition,
236 size_t maximumPosition)
244 return(CreateReadStringSource(theEnv,name,str,currentPosition,maximumPosition));
247 /******************************************************************/
248 /* CreateReadStringSource: Creates a new string router for input. */
249 /******************************************************************/
250 static int CreateReadStringSource(
254 size_t currentPosition,
255 size_t maximumPosition)
257 struct stringRouter *newStringRouter;
260 if (FindStringRouter(theEnv,name) != NULL) return(0);
262 newStringRouter = get_struct(theEnv,stringRouter);
263 theName = (char *) gm1(theEnv,strlen(name) + 1);
264 genstrcpy(theName,name);
265 newStringRouter->name = theName;
266 newStringRouter->writeString = NULL;
267 newStringRouter->readString = str;
268 newStringRouter->currentPosition = currentPosition;
269 newStringRouter->readWriteType = READ_STRING;
270 newStringRouter->maximumPosition = maximumPosition;
271 newStringRouter->next = StringRouterData(theEnv)->ListOfStringRouters;
272 StringRouterData(theEnv)->ListOfStringRouters = newStringRouter;
277 /**********************************************/
278 /* CloseStringSource: Closes a string router. */
279 /**********************************************/
280 globle int CloseStringSource(
284 struct stringRouter *head, *last;
287 head = StringRouterData(theEnv)->ListOfStringRouters;
290 if (strcmp(head->name,name) == 0)
294 StringRouterData(theEnv)->ListOfStringRouters = head->next;
295 rm(theEnv,(void *) head->name,strlen(head->name) + 1);
296 rtn_struct(theEnv,stringRouter,head);
301 last->next = head->next;
302 rm(theEnv,(void *) head->name,strlen(head->name) + 1);
303 rtn_struct(theEnv,stringRouter,head);
314 /******************************************************************/
315 /* OpenStringDestination: Opens a new string router for printing. */
316 /******************************************************************/
317 globle int OpenStringDestination(
321 size_t maximumPosition)
323 struct stringRouter *newStringRouter;
326 if (FindStringRouter(theEnv,name) != NULL) return(0);
328 newStringRouter = get_struct(theEnv,stringRouter);
329 theName = (char *) gm1(theEnv,(int) strlen(name) + 1);
330 genstrcpy(theName,name);
331 newStringRouter->name = theName;
332 newStringRouter->readString = NULL;
333 newStringRouter->writeString = str;
334 newStringRouter->currentPosition = 0;
335 newStringRouter->readWriteType = WRITE_STRING;
336 newStringRouter->maximumPosition = maximumPosition;
337 newStringRouter->next = StringRouterData(theEnv)->ListOfStringRouters;
338 StringRouterData(theEnv)->ListOfStringRouters = newStringRouter;
343 /***************************************************/
344 /* CloseStringDestination: Closes a string router. */
345 /***************************************************/
346 globle int CloseStringDestination(
350 return(CloseStringSource(theEnv,name));
353 /*******************************************************************/
354 /* FindStringRouter: Returns a pointer to the named string router. */
355 /*******************************************************************/
356 static struct stringRouter *FindStringRouter(
360 struct stringRouter *head;
362 head = StringRouterData(theEnv)->ListOfStringRouters;
365 if (strcmp(head->name,name) == 0)