tizen 2.4 release
[external/clips.git] / src / cstrncmp.c
1    /*******************************************************/
2    /*      "C" Language Integrated Production System      */
3    /*                                                     */
4    /*             CLIPS Version 6.30  08/16/14            */
5    /*                                                     */
6    /*          CONSTRAINT CONSTRUCTS-TO-C MODULE          */
7    /*******************************************************/
8
9 /*************************************************************/
10 /* Purpose: Implements the constructs-to-c feature for       */
11 /*    constraint records.                                    */
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.24: Added allowed-classes slot facet.              */
22 /*                                                           */
23 /*            Added environment parameter to GenClose.       */
24 /*                                                           */
25 /*      6.30: Added support for path name argument to        */
26 /*            constructs-to-c.                               */
27 /*                                                           */
28 /*            Added const qualifiers to remove C++           */
29 /*            deprecation warnings.                          */
30 /*                                                           */
31 /*************************************************************/
32
33 #define _CSTRNCMP_SOURCE_
34
35 #include "setup.h"
36
37 #if CONSTRUCT_COMPILER && (! RUN_TIME)
38
39 #include "constant.h"
40
41 #include "conscomp.h"
42 #include "envrnmnt.h"
43 #include "memalloc.h"
44 #include "router.h"
45 #include "sysdep.h"
46
47 #include "cstrncmp.h"
48
49 /***********************************************/
50 /* ConstraintsToCode: Produces the constraint  */
51 /*   record code for a run-time module created */
52 /*   using the constructs-to-c function.       */
53 /***********************************************/
54 globle int ConstraintsToCode(
55   void *theEnv,
56   const char *fileName,
57   const char *pathName,
58   char *fileNameBuffer,
59   int fileID,
60   FILE *headerFP,
61   int imageID,
62   int maxIndices)
63   {
64    int i, j, count;
65    int newHeader = TRUE;
66    FILE *fp;
67    int version = 1;
68    int arrayVersion = 1;
69    unsigned short numberOfConstraints = 0;
70    CONSTRAINT_RECORD *tmpPtr;
71
72    /*===============================================*/
73    /* Count the total number of constraint records. */
74    /*===============================================*/
75
76    for (i = 0 ; i < SIZE_CONSTRAINT_HASH; i++)
77      {
78       for (tmpPtr = ConstraintData(theEnv)->ConstraintHashtable[i];
79            tmpPtr != NULL;
80            tmpPtr = tmpPtr->next)
81         { tmpPtr->bsaveIndex = numberOfConstraints++; }
82      }
83
84    /*=====================================================*/
85    /* If dynamic constraint checking is disabled, then    */
86    /* contraints won't be saved. If there are constraints */
87    /* which could be saved, then issue a warning message. */
88    /*=====================================================*/
89
90    if ((! EnvGetDynamicConstraintChecking(theEnv)) && (numberOfConstraints != 0))
91      {
92       numberOfConstraints = 0;
93       PrintWarningID(theEnv,"CSTRNCMP",1,FALSE);
94       EnvPrintRouter(theEnv,WWARNING,"Constraints are not saved with a constructs-to-c image\n");
95       EnvPrintRouter(theEnv,WWARNING,"  when dynamic constraint checking is disabled.\n");
96      }
97
98    if (numberOfConstraints == 0) return(-1);
99
100    /*=================================================*/
101    /* Print the extern definition in the header file. */
102    /*=================================================*/
103
104    for (i = 1; i <= (numberOfConstraints / maxIndices) + 1 ; i++)
105      { fprintf(headerFP,"extern CONSTRAINT_RECORD C%d_%d[];\n",imageID,i); }
106
107    /*==================*/
108    /* Create the file. */
109    /*==================*/
110
111    if ((fp = NewCFile(theEnv,fileName,pathName,fileNameBuffer,fileID,version,FALSE)) == NULL) return(-1);
112
113    /*===================*/
114    /* List the entries. */
115    /*===================*/
116
117    j = 0;
118    count = 0;
119
120    for (i = 0; i < SIZE_CONSTRAINT_HASH; i++)
121      {
122       for (tmpPtr = ConstraintData(theEnv)->ConstraintHashtable[i];
123            tmpPtr != NULL;
124            tmpPtr = tmpPtr->next)
125         {
126          if (newHeader)
127            {
128             fprintf(fp,"CONSTRAINT_RECORD C%d_%d[] = {\n",imageID,arrayVersion);
129             newHeader = FALSE;
130            }
131
132          fprintf(fp,"{%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
133                  tmpPtr->anyAllowed,
134                  tmpPtr->symbolsAllowed,
135                  tmpPtr->stringsAllowed,
136                  tmpPtr->floatsAllowed,
137                  tmpPtr->integersAllowed,
138                  tmpPtr->instanceNamesAllowed,
139                  tmpPtr->instanceAddressesAllowed,
140                  tmpPtr->externalAddressesAllowed,
141                  tmpPtr->factAddressesAllowed,
142                  0, /* void allowed */
143                  tmpPtr->anyRestriction,
144                  tmpPtr->symbolRestriction,
145                  tmpPtr->stringRestriction,
146                  tmpPtr->floatRestriction,
147                  tmpPtr->integerRestriction,
148                  tmpPtr->classRestriction,
149                  tmpPtr->instanceNameRestriction,
150                  tmpPtr->multifieldsAllowed,
151                  tmpPtr->singlefieldsAllowed);
152
153          fprintf(fp,",0,"); /* bsaveIndex */
154
155          PrintHashedExpressionReference(theEnv,fp,tmpPtr->classList,imageID,maxIndices);
156          fprintf(fp,",");
157          PrintHashedExpressionReference(theEnv,fp,tmpPtr->restrictionList,imageID,maxIndices);
158          fprintf(fp,",");
159          PrintHashedExpressionReference(theEnv,fp,tmpPtr->minValue,imageID,maxIndices);
160          fprintf(fp,",");
161          PrintHashedExpressionReference(theEnv,fp,tmpPtr->maxValue,imageID,maxIndices);
162          fprintf(fp,",");
163          PrintHashedExpressionReference(theEnv,fp,tmpPtr->minFields,imageID,maxIndices);
164          fprintf(fp,",");
165          PrintHashedExpressionReference(theEnv,fp,tmpPtr->maxFields,imageID,maxIndices);
166
167          /* multifield slot */
168
169          fprintf(fp,",NULL");
170
171          /* next slot */
172
173          if (tmpPtr->next == NULL)
174            { fprintf(fp,",NULL,"); }
175          else
176            {
177             if ((j + 1) >= maxIndices)
178               { fprintf(fp,",&C%d_%d[%d],",imageID,arrayVersion + 1,0); }
179             else
180               { fprintf(fp,",&C%d_%d[%d],",imageID,arrayVersion,j + 1); }
181            }
182
183          fprintf(fp,"%d,%d",tmpPtr->bucket,tmpPtr->count + 1);
184
185          count++;
186          j++;
187
188          if ((count == numberOfConstraints) || (j >= maxIndices))
189            {
190             fprintf(fp,"}};\n");
191             GenClose(theEnv,fp);
192             j = 0;
193             version++;
194             arrayVersion++;
195             if (count < numberOfConstraints)
196               {
197                if ((fp = NewCFile(theEnv,fileName,pathName,fileNameBuffer,1,version,FALSE)) == NULL) return(0);
198                newHeader = TRUE;
199               }
200            }
201          else
202            { fprintf(fp,"},\n"); }
203         }
204      }
205
206    return(version);
207   }
208
209 /**********************************************************/
210 /* PrintConstraintReference: Prints C code representation */
211 /*   of a constraint record data structure reference.     */
212 /**********************************************************/
213 globle void PrintConstraintReference(
214   void *theEnv,
215   FILE *fp,
216   CONSTRAINT_RECORD *cPtr,
217   int imageID,
218   int maxIndices)
219   {
220    if ((cPtr == NULL) || (! EnvGetDynamicConstraintChecking(theEnv)))
221      { fprintf(fp,"NULL"); }
222    else fprintf(fp,"&C%d_%d[%d]",imageID,
223                                  (int) (cPtr->bsaveIndex / maxIndices) + 1,
224                                  (int) cPtr->bsaveIndex % maxIndices);
225   }
226
227 #endif /* CONSTRUCT_COMPILER && (! RUN_TIME) */
228
229
230