tizen 2.4 release
[external/clips.git] / src / strngrtr.c
1    /*******************************************************/
2    /*      "C" Language Integrated Production System      */
3    /*                                                     */
4    /*             CLIPS Version 6.30  08/16/14            */
5    /*                                                     */
6    /*              STRING I/O ROUTER MODULE               */
7    /*******************************************************/
8
9 /*************************************************************/
10 /* Purpose: I/O Router routines which allow strings to be    */
11 /*   used as input and output sources.                       */
12 /*                                                           */
13 /* Principal Programmer(s):                                  */
14 /*      Gary D. Riley                                        */
15 /*                                                           */
16 /* Contributing Programmer(s):                               */
17 /*      Brian L. Dantes                                      */
18 /*                                                           */
19 /* Revision History:                                         */
20 /*                                                           */
21 /*      6.30: Used genstrcpy instead of strcpy.              */
22 /*                                                           */
23 /*            Removed conditional code for unsupported       */
24 /*            compilers/operating systems (IBM_MCW,          */
25 /*            MAC_MCW, and IBM_TBC).                         */
26 /*                                                           */
27 /*            Changed integer type/precision.                */
28 /*                                                           */
29 /*            Added const qualifiers to remove C++           */
30 /*            deprecation warnings.                          */
31 /*                                                           */
32 /*************************************************************/
33
34 #define _STRNGRTR_SOURCE_
35
36 #include <stdio.h>
37 #define _STDIO_INCLUDED_
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include "setup.h"
42
43 #include "constant.h"
44 #include "envrnmnt.h"
45 #include "memalloc.h"
46 #include "router.h"
47 #include "sysdep.h"
48
49 #include "strngrtr.h"
50
51 #define READ_STRING 0
52 #define WRITE_STRING 1
53
54 /***************************************/
55 /* LOCAL INTERNAL FUNCTION DEFINITIONS */
56 /***************************************/
57
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 *);
65
66 /**********************************************************/
67 /* InitializeStringRouter: Initializes string I/O router. */
68 /**********************************************************/
69 globle void InitializeStringRouter(
70   void *theEnv)
71   {
72    AllocateEnvironmentData(theEnv,STRING_ROUTER_DATA,sizeof(struct stringRouterData),DeallocateStringRouterData);
73
74    EnvAddRouter(theEnv,"string",0,FindString,PrintString,GetcString,UngetcString,NULL);
75   }
76
77 /*******************************************/
78 /* DeallocateStringRouterData: Deallocates */
79 /*    environment data for string routers. */
80 /*******************************************/
81 static void DeallocateStringRouterData(
82   void *theEnv)
83   {
84    struct stringRouter *tmpPtr, *nextPtr;
85
86    tmpPtr = StringRouterData(theEnv)->ListOfStringRouters;
87    while (tmpPtr != NULL)
88      {
89       nextPtr = tmpPtr->next;
90       rm(theEnv,(void *) tmpPtr->name,strlen(tmpPtr->name) + 1);
91       rtn_struct(theEnv,stringRouter,tmpPtr);
92       tmpPtr = nextPtr;
93      }
94   }
95
96 /*************************************************************/
97 /* FindString: Find routine for string router logical names. */
98 /*************************************************************/
99 static int FindString(
100   void *theEnv,
101   const char *fileid)
102   {
103    struct stringRouter *head;
104
105    head = StringRouterData(theEnv)->ListOfStringRouters;
106    while (head != NULL)
107      {
108       if (strcmp(head->name,fileid) == 0)
109         { return(TRUE); }
110       head = head->next;
111      }
112
113    return(FALSE);
114   }
115
116 /**************************************************/
117 /* PrintString: Print routine for string routers. */
118 /**************************************************/
119 static int PrintString(
120   void *theEnv,
121   const char *logicalName,
122   const char *str)
123   {
124    struct stringRouter *head;
125
126    head = FindStringRouter(theEnv,logicalName);
127    if (head == NULL)
128      {
129       SystemError(theEnv,"ROUTER",3);
130       EnvExitRouter(theEnv,EXIT_FAILURE);
131      }
132
133    if (head->readWriteType != WRITE_STRING) return(1);
134
135    if (head->maximumPosition == 0) return(1);
136
137    if ((head->currentPosition + 1) >= head->maximumPosition) return(1);
138
139    genstrncpy(&head->writeString[head->currentPosition],
140               str,(STD_SIZE) (head->maximumPosition - head->currentPosition) - 1);
141
142    head->currentPosition += strlen(str);
143
144    return(1);
145   }
146
147 /************************************************/
148 /* GetcString: Getc routine for string routers. */
149 /************************************************/
150 static int GetcString(
151   void *theEnv,
152   const char *logicalName)
153   {
154    struct stringRouter *head;
155    int rc;
156
157    head = FindStringRouter(theEnv,logicalName);
158    if (head == NULL)
159      {
160       SystemError(theEnv,"ROUTER",1);
161       EnvExitRouter(theEnv,EXIT_FAILURE);
162      }
163
164    if (head->readWriteType != READ_STRING) return(EOF);
165    if (head->currentPosition >= head->maximumPosition)
166      {
167       head->currentPosition++;
168       return(EOF);
169      }
170
171    rc = (unsigned char) head->readString[head->currentPosition];
172    head->currentPosition++;
173
174    return(rc);
175   }
176
177 /****************************************************/
178 /* UngetcString: Ungetc routine for string routers. */
179 /****************************************************/
180 static int UngetcString(
181   void *theEnv,
182   int ch,
183   const char *logicalName)
184   {
185    struct stringRouter *head;
186 #if MAC_XCD
187 #pragma unused(ch)
188 #endif
189
190    head = FindStringRouter(theEnv,logicalName);
191
192    if (head == NULL)
193      {
194       SystemError(theEnv,"ROUTER",2);
195       EnvExitRouter(theEnv,EXIT_FAILURE);
196      }
197
198    if (head->readWriteType != READ_STRING) return(0);
199    if (head->currentPosition > 0)
200      { head->currentPosition--; }
201
202    return(1);
203   }
204
205 /************************************************/
206 /* OpenStringSource: Opens a new string router. */
207 /************************************************/
208 globle int OpenStringSource(
209   void *theEnv,
210   const char *name,
211   const char *str,
212   size_t currentPosition)
213   {
214    size_t maximumPosition;
215
216    if (str == NULL)
217      {
218       currentPosition = 0;
219       maximumPosition = 0;
220      }
221    else
222      { maximumPosition = strlen(str); }
223
224    return(CreateReadStringSource(theEnv,name,str,currentPosition,maximumPosition));
225   }
226
227 /******************************************************/
228 /* OpenTextSource: Opens a new string router for text */
229 /*   (which is not NULL terminated).                  */
230 /******************************************************/
231 globle int OpenTextSource(
232   void *theEnv,
233   const char *name,
234   const char *str,
235   size_t currentPosition,
236   size_t maximumPosition)
237   {
238    if (str == NULL)
239      {
240       currentPosition = 0;
241       maximumPosition = 0;
242      }
243
244    return(CreateReadStringSource(theEnv,name,str,currentPosition,maximumPosition));
245   }
246
247 /******************************************************************/
248 /* CreateReadStringSource: Creates a new string router for input. */
249 /******************************************************************/
250 static int CreateReadStringSource(
251   void *theEnv,
252   const char *name,
253   const char *str,
254   size_t currentPosition,
255   size_t maximumPosition)
256   {
257    struct stringRouter *newStringRouter;
258    char *theName;
259
260    if (FindStringRouter(theEnv,name) != NULL) return(0);
261
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;
273
274    return(1);
275   }
276
277 /**********************************************/
278 /* CloseStringSource: Closes a string router. */
279 /**********************************************/
280 globle int CloseStringSource(
281   void *theEnv,
282   const char *name)
283   {
284    struct stringRouter *head, *last;
285
286    last = NULL;
287    head = StringRouterData(theEnv)->ListOfStringRouters;
288    while (head != NULL)
289      {
290       if (strcmp(head->name,name) == 0)
291         {
292          if (last == NULL)
293            {
294             StringRouterData(theEnv)->ListOfStringRouters = head->next;
295             rm(theEnv,(void *) head->name,strlen(head->name) + 1);
296             rtn_struct(theEnv,stringRouter,head);
297             return(1);
298            }
299          else
300            {
301             last->next = head->next;
302             rm(theEnv,(void *) head->name,strlen(head->name) + 1);
303             rtn_struct(theEnv,stringRouter,head);
304             return(1);
305            }
306         }
307       last = head;
308       head = head->next;
309      }
310
311    return(0);
312   }
313
314 /******************************************************************/
315 /* OpenStringDestination: Opens a new string router for printing. */
316 /******************************************************************/
317 globle int OpenStringDestination(
318   void *theEnv,
319   const char *name,
320   char *str,
321   size_t maximumPosition)
322   {
323    struct stringRouter *newStringRouter;
324    char *theName;
325
326    if (FindStringRouter(theEnv,name) != NULL) return(0);
327
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;
339
340    return(1);
341   }
342
343 /***************************************************/
344 /* CloseStringDestination: Closes a string router. */
345 /***************************************************/
346 globle int CloseStringDestination(
347   void *theEnv,
348   const char *name)
349   {
350    return(CloseStringSource(theEnv,name));
351   }
352
353 /*******************************************************************/
354 /* FindStringRouter: Returns a pointer to the named string router. */
355 /*******************************************************************/
356 static struct stringRouter *FindStringRouter(
357   void *theEnv,
358   const char *name)
359   {
360    struct stringRouter *head;
361
362    head = StringRouterData(theEnv)->ListOfStringRouters;
363    while (head != NULL)
364      {
365       if (strcmp(head->name,name) == 0)
366         { return(head); }
367       head = head->next;
368      }
369
370    return(NULL);
371   }
372
373
374
375