tizen 2.4 release
[external/clips.git] / src / pprint.c
1    /*******************************************************/
2    /*      "C" Language Integrated Production System      */
3    /*                                                     */
4    /*             CLIPS Version 6.30  08/16/14            */
5    /*                                                     */
6    /*                 PRETTY PRINT MODULE                 */
7    /*******************************************************/
8
9 /*************************************************************/
10 /* Purpose: Routines for processing the pretty print         */
11 /*   representation of constructs.                           */
12 /*                                                           */
13 /* Principal Programmer(s):                                  */
14 /*      Gary D. Riley                                        */
15 /*                                                           */
16 /* Contributing Programmer(s):                               */
17 /*      Chris Culbert                                        */
18 /*      Brian Dantes                                         */
19 /*                                                           */
20 /* Revision History:                                         */
21 /*                                                           */
22 /*      6.24: Corrected code generating compilation          */
23 /*            warnings.                                      */
24 /*                                                           */
25 /*      6.30: Changed integer type/precision.                */
26 /*                                                           */
27 /*            Used genstrcpy instead of strcpy.              */
28 /*                                                           */
29 /*            Added const qualifiers to remove C++           */
30 /*            deprecation warnings.                          */
31 /*                                                           */
32 /*************************************************************/
33
34 #define _PPRINT_SOURCE_
35
36 #include <stdio.h>
37 #define _STDIO_INCLUDED_
38 #include <string.h>
39 #include <ctype.h>
40
41 #include "setup.h"
42
43 #include "constant.h"
44 #include "envrnmnt.h"
45 #include "memalloc.h"
46 #include "sysdep.h"
47 #include "utility.h"
48
49 #include "pprint.h"
50
51 /***************************************/
52 /* LOCAL INTERNAL FUNCTION DEFINITIONS */
53 /***************************************/
54
55    static void                    DeallocatePrettyPrintData(void *);
56
57 /****************************************************/
58 /* InitializePrettyPrintData: Allocates environment */
59 /*    data for pretty print routines.               */
60 /****************************************************/
61 globle void InitializePrettyPrintData(
62   void *theEnv)
63   {
64    AllocateEnvironmentData(theEnv,PRETTY_PRINT_DATA,sizeof(struct prettyPrintData),DeallocatePrettyPrintData);
65
66    PrettyPrintData(theEnv)->PPBufferEnabled = TRUE;
67   }
68
69 /******************************************************/
70 /* DeallocatePrettyPrintData: Deallocates environment */
71 /*    data for the pretty print routines.             */
72 /******************************************************/
73 static void DeallocatePrettyPrintData(
74   void *theEnv)
75   {
76    if (PrettyPrintData(theEnv)->PrettyPrintBuffer != NULL)
77      { rm(theEnv,PrettyPrintData(theEnv)->PrettyPrintBuffer,PrettyPrintData(theEnv)->PPBufferMax); }
78   }
79
80 /*******************************************************/
81 /* FlushPPBuffer: Resets the pretty print save buffer. */
82 /*******************************************************/
83 globle void FlushPPBuffer(
84   void *theEnv)
85   {
86    if (PrettyPrintData(theEnv)->PrettyPrintBuffer == NULL) return;
87    PrettyPrintData(theEnv)->PPBackupOnce = 0;
88    PrettyPrintData(theEnv)->PPBackupTwice = 0;
89    PrettyPrintData(theEnv)->PPBufferPos = 0;
90    PrettyPrintData(theEnv)->PrettyPrintBuffer[0] = EOS;
91    return;
92   }
93
94 /*********************************************************************/
95 /* DestroyPPBuffer: Resets and removes the pretty print save buffer. */
96 /*********************************************************************/
97 globle void DestroyPPBuffer(void *theEnv)
98   {
99    PrettyPrintData(theEnv)->PPBackupOnce = 0;
100    PrettyPrintData(theEnv)->PPBackupTwice = 0;
101    PrettyPrintData(theEnv)->PPBufferPos = 0;
102    if (PrettyPrintData(theEnv)->PrettyPrintBuffer != NULL) rm(theEnv,PrettyPrintData(theEnv)->PrettyPrintBuffer,PrettyPrintData(theEnv)->PPBufferMax);
103    PrettyPrintData(theEnv)->PrettyPrintBuffer = NULL;
104    PrettyPrintData(theEnv)->PPBufferMax = 0;
105   }
106
107 /*********************************************/
108 /* SavePPBuffer: Appends a string to the end */
109 /*   of the pretty print save buffer.        */
110 /*********************************************/
111 globle void SavePPBuffer(
112   void *theEnv,
113   const char *str)
114   {
115    size_t increment;
116
117    /*==========================================*/
118    /* If the pretty print buffer isn't needed, */
119    /* then don't bother writing to it.         */
120    /*==========================================*/
121
122    if ((PrettyPrintData(theEnv)->PPBufferStatus == OFF) || (! PrettyPrintData(theEnv)->PPBufferEnabled))
123      { return; }
124
125    /*===============================*/
126    /* Determine the increment size. */
127    /*===============================*/
128
129    increment = 512;
130    if (PrettyPrintData(theEnv)->PPBufferPos > increment)
131      { increment = PrettyPrintData(theEnv)->PPBufferPos * 3; }
132
133    /*================================================*/
134    /* If the pretty print buffer isn't big enough to */
135    /* contain the string, then increase its size.    */
136    /*================================================*/
137
138    if (strlen(str) + PrettyPrintData(theEnv)->PPBufferPos + 1 >= PrettyPrintData(theEnv)->PPBufferMax)
139      {
140       PrettyPrintData(theEnv)->PrettyPrintBuffer =
141          (char *) genrealloc(theEnv,PrettyPrintData(theEnv)->PrettyPrintBuffer,
142                                     PrettyPrintData(theEnv)->PPBufferMax,
143                                     PrettyPrintData(theEnv)->PPBufferMax + increment);
144       PrettyPrintData(theEnv)->PPBufferMax += increment;
145      }
146
147    /*==================================================*/
148    /* Remember the previous tokens saved to the pretty */
149    /* print buffer in case it is necessary to back up. */
150    /*==================================================*/
151
152    PrettyPrintData(theEnv)->PPBackupTwice = PrettyPrintData(theEnv)->PPBackupOnce;
153    PrettyPrintData(theEnv)->PPBackupOnce = PrettyPrintData(theEnv)->PPBufferPos;
154
155    /*=============================================*/
156    /* Save the string to the pretty print buffer. */
157    /*=============================================*/
158
159    PrettyPrintData(theEnv)->PrettyPrintBuffer = AppendToString(theEnv,str,PrettyPrintData(theEnv)->PrettyPrintBuffer,&PrettyPrintData(theEnv)->PPBufferPos,&PrettyPrintData(theEnv)->PPBufferMax);
160   }
161
162 /***************************************************/
163 /* PPBackup:  Removes the last string added to the */
164 /*   pretty print save buffer. Only capable of     */
165 /*   backing up for the two most recent additions. */
166 /***************************************************/
167 globle void PPBackup(
168   void *theEnv)
169   {
170    if ((PrettyPrintData(theEnv)->PPBufferStatus == OFF) ||
171        (PrettyPrintData(theEnv)->PrettyPrintBuffer == NULL) ||
172        (! PrettyPrintData(theEnv)->PPBufferEnabled))
173      { return; }
174
175    PrettyPrintData(theEnv)->PPBufferPos = PrettyPrintData(theEnv)->PPBackupOnce;
176    PrettyPrintData(theEnv)->PPBackupOnce = PrettyPrintData(theEnv)->PPBackupTwice;
177    PrettyPrintData(theEnv)->PrettyPrintBuffer[PrettyPrintData(theEnv)->PPBufferPos] = EOS;
178   }
179
180 /**************************************************/
181 /* CopyPPBuffer: Makes a copy of the pretty print */
182 /*   save buffer.                                 */
183 /**************************************************/
184 globle char *CopyPPBuffer(
185   void *theEnv)
186   {
187    size_t length;
188    char *newString;
189
190    length = (1 + strlen(PrettyPrintData(theEnv)->PrettyPrintBuffer)) * (int) sizeof (char);
191    newString = (char *) gm2(theEnv,length);
192
193    genstrcpy(newString,PrettyPrintData(theEnv)->PrettyPrintBuffer);
194    return(newString);
195   }
196
197 /************************************************************/
198 /* GetPPBuffer: Returns a pointer to the PrettyPrintBuffer. */
199 /************************************************************/
200 globle char *GetPPBuffer(
201   void *theEnv)
202   {
203    return(PrettyPrintData(theEnv)->PrettyPrintBuffer);
204   }
205
206 /*******************************************/
207 /* PPCRAndIndent: Prints white spaces into */
208 /*   the pretty print buffer.              */
209 /*******************************************/
210 globle void PPCRAndIndent(
211   void *theEnv)
212   {
213    int i;
214    char buffer[120];
215
216    if ((PrettyPrintData(theEnv)->PPBufferStatus == OFF) ||
217        (! PrettyPrintData(theEnv)->PPBufferEnabled))
218      { return; }
219
220    buffer[0] = '\n';
221
222    for (i = 1 ; i <= PrettyPrintData(theEnv)->IndentationDepth ; i++)
223      { buffer[i] = ' '; }
224    buffer[i] = EOS;
225
226    SavePPBuffer(theEnv,buffer);
227   }
228
229 /************************************************/
230 /* IncrementIndentDepth: Increments indentation */
231 /*   depth for pretty printing.                 */
232 /************************************************/
233 globle void IncrementIndentDepth(
234   void *theEnv,
235   int value)
236   {
237    PrettyPrintData(theEnv)->IndentationDepth += value;
238   }
239
240 /************************************************/
241 /* DecrementIndentDepth: Decrements indentation */
242 /*   depth for pretty printing.                 */
243 /************************************************/
244 globle void DecrementIndentDepth(
245   void *theEnv,
246   int value)
247   {
248    PrettyPrintData(theEnv)->IndentationDepth -= value;
249   }
250
251 /************************************/
252 /* SetIndentDepth: Sets indentation */
253 /*   depth for pretty printing.     */
254 /************************************/
255 globle void SetIndentDepth(
256   void *theEnv,
257   int value)
258   {
259    PrettyPrintData(theEnv)->IndentationDepth = value;
260   }
261
262 /******************************************/
263 /* SetPPBufferStatus: Sets PPBufferStatus */
264 /*   flag to boolean value of ON or OFF.  */
265 /******************************************/
266 globle void SetPPBufferStatus(
267   void *theEnv,
268   int value)
269   {
270    PrettyPrintData(theEnv)->PPBufferStatus = value;
271   }
272
273 /************************************/
274 /* GetPPBufferStatus: Returns value */
275 /*   of the PPBufferStatus flag.    */
276 /************************************/
277 globle int GetPPBufferStatus(
278   void *theEnv)
279   {
280    return(PrettyPrintData(theEnv)->PPBufferStatus);
281   }
282
283 /******************************************/
284 /* SetPPBufferEnabled: */
285 /******************************************/
286 globle int SetPPBufferEnabled(
287   void *theEnv,
288   int value)
289   {
290    int oldValue;
291
292    oldValue = PrettyPrintData(theEnv)->PPBufferEnabled;
293    PrettyPrintData(theEnv)->PPBufferEnabled = value;
294    return(oldValue);
295   }
296
297 /************************************/
298 /* GetPPBufferEnabled: */
299 /************************************/
300 globle int GetPPBufferEnabled(
301   void *theEnv)
302   {
303    return(PrettyPrintData(theEnv)->PPBufferEnabled);
304   }
305