1 /*******************************************************/
2 /* "C" Language Integrated Production System */
4 /* CLIPS Version 6.30 08/16/14 */
6 /* PRETTY PRINT MODULE */
7 /*******************************************************/
9 /*************************************************************/
10 /* Purpose: Routines for processing the pretty print */
11 /* representation of constructs. */
13 /* Principal Programmer(s): */
16 /* Contributing Programmer(s): */
20 /* Revision History: */
22 /* 6.24: Corrected code generating compilation */
25 /* 6.30: Changed integer type/precision. */
27 /* Used genstrcpy instead of strcpy. */
29 /* Added const qualifiers to remove C++ */
30 /* deprecation warnings. */
32 /*************************************************************/
34 #define _PPRINT_SOURCE_
37 #define _STDIO_INCLUDED_
51 /***************************************/
52 /* LOCAL INTERNAL FUNCTION DEFINITIONS */
53 /***************************************/
55 static void DeallocatePrettyPrintData(void *);
57 /****************************************************/
58 /* InitializePrettyPrintData: Allocates environment */
59 /* data for pretty print routines. */
60 /****************************************************/
61 globle void InitializePrettyPrintData(
64 AllocateEnvironmentData(theEnv,PRETTY_PRINT_DATA,sizeof(struct prettyPrintData),DeallocatePrettyPrintData);
66 PrettyPrintData(theEnv)->PPBufferEnabled = TRUE;
69 /******************************************************/
70 /* DeallocatePrettyPrintData: Deallocates environment */
71 /* data for the pretty print routines. */
72 /******************************************************/
73 static void DeallocatePrettyPrintData(
76 if (PrettyPrintData(theEnv)->PrettyPrintBuffer != NULL)
77 { rm(theEnv,PrettyPrintData(theEnv)->PrettyPrintBuffer,PrettyPrintData(theEnv)->PPBufferMax); }
80 /*******************************************************/
81 /* FlushPPBuffer: Resets the pretty print save buffer. */
82 /*******************************************************/
83 globle void FlushPPBuffer(
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;
94 /*********************************************************************/
95 /* DestroyPPBuffer: Resets and removes the pretty print save buffer. */
96 /*********************************************************************/
97 globle void DestroyPPBuffer(void *theEnv)
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;
107 /*********************************************/
108 /* SavePPBuffer: Appends a string to the end */
109 /* of the pretty print save buffer. */
110 /*********************************************/
111 globle void SavePPBuffer(
117 /*==========================================*/
118 /* If the pretty print buffer isn't needed, */
119 /* then don't bother writing to it. */
120 /*==========================================*/
122 if ((PrettyPrintData(theEnv)->PPBufferStatus == OFF) || (! PrettyPrintData(theEnv)->PPBufferEnabled))
125 /*===============================*/
126 /* Determine the increment size. */
127 /*===============================*/
130 if (PrettyPrintData(theEnv)->PPBufferPos > increment)
131 { increment = PrettyPrintData(theEnv)->PPBufferPos * 3; }
133 /*================================================*/
134 /* If the pretty print buffer isn't big enough to */
135 /* contain the string, then increase its size. */
136 /*================================================*/
138 if (strlen(str) + PrettyPrintData(theEnv)->PPBufferPos + 1 >= PrettyPrintData(theEnv)->PPBufferMax)
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;
147 /*==================================================*/
148 /* Remember the previous tokens saved to the pretty */
149 /* print buffer in case it is necessary to back up. */
150 /*==================================================*/
152 PrettyPrintData(theEnv)->PPBackupTwice = PrettyPrintData(theEnv)->PPBackupOnce;
153 PrettyPrintData(theEnv)->PPBackupOnce = PrettyPrintData(theEnv)->PPBufferPos;
155 /*=============================================*/
156 /* Save the string to the pretty print buffer. */
157 /*=============================================*/
159 PrettyPrintData(theEnv)->PrettyPrintBuffer = AppendToString(theEnv,str,PrettyPrintData(theEnv)->PrettyPrintBuffer,&PrettyPrintData(theEnv)->PPBufferPos,&PrettyPrintData(theEnv)->PPBufferMax);
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(
170 if ((PrettyPrintData(theEnv)->PPBufferStatus == OFF) ||
171 (PrettyPrintData(theEnv)->PrettyPrintBuffer == NULL) ||
172 (! PrettyPrintData(theEnv)->PPBufferEnabled))
175 PrettyPrintData(theEnv)->PPBufferPos = PrettyPrintData(theEnv)->PPBackupOnce;
176 PrettyPrintData(theEnv)->PPBackupOnce = PrettyPrintData(theEnv)->PPBackupTwice;
177 PrettyPrintData(theEnv)->PrettyPrintBuffer[PrettyPrintData(theEnv)->PPBufferPos] = EOS;
180 /**************************************************/
181 /* CopyPPBuffer: Makes a copy of the pretty print */
183 /**************************************************/
184 globle char *CopyPPBuffer(
190 length = (1 + strlen(PrettyPrintData(theEnv)->PrettyPrintBuffer)) * (int) sizeof (char);
191 newString = (char *) gm2(theEnv,length);
193 genstrcpy(newString,PrettyPrintData(theEnv)->PrettyPrintBuffer);
197 /************************************************************/
198 /* GetPPBuffer: Returns a pointer to the PrettyPrintBuffer. */
199 /************************************************************/
200 globle char *GetPPBuffer(
203 return(PrettyPrintData(theEnv)->PrettyPrintBuffer);
206 /*******************************************/
207 /* PPCRAndIndent: Prints white spaces into */
208 /* the pretty print buffer. */
209 /*******************************************/
210 globle void PPCRAndIndent(
216 if ((PrettyPrintData(theEnv)->PPBufferStatus == OFF) ||
217 (! PrettyPrintData(theEnv)->PPBufferEnabled))
222 for (i = 1 ; i <= PrettyPrintData(theEnv)->IndentationDepth ; i++)
226 SavePPBuffer(theEnv,buffer);
229 /************************************************/
230 /* IncrementIndentDepth: Increments indentation */
231 /* depth for pretty printing. */
232 /************************************************/
233 globle void IncrementIndentDepth(
237 PrettyPrintData(theEnv)->IndentationDepth += value;
240 /************************************************/
241 /* DecrementIndentDepth: Decrements indentation */
242 /* depth for pretty printing. */
243 /************************************************/
244 globle void DecrementIndentDepth(
248 PrettyPrintData(theEnv)->IndentationDepth -= value;
251 /************************************/
252 /* SetIndentDepth: Sets indentation */
253 /* depth for pretty printing. */
254 /************************************/
255 globle void SetIndentDepth(
259 PrettyPrintData(theEnv)->IndentationDepth = value;
262 /******************************************/
263 /* SetPPBufferStatus: Sets PPBufferStatus */
264 /* flag to boolean value of ON or OFF. */
265 /******************************************/
266 globle void SetPPBufferStatus(
270 PrettyPrintData(theEnv)->PPBufferStatus = value;
273 /************************************/
274 /* GetPPBufferStatus: Returns value */
275 /* of the PPBufferStatus flag. */
276 /************************************/
277 globle int GetPPBufferStatus(
280 return(PrettyPrintData(theEnv)->PPBufferStatus);
283 /******************************************/
284 /* SetPPBufferEnabled: */
285 /******************************************/
286 globle int SetPPBufferEnabled(
292 oldValue = PrettyPrintData(theEnv)->PPBufferEnabled;
293 PrettyPrintData(theEnv)->PPBufferEnabled = value;
297 /************************************/
298 /* GetPPBufferEnabled: */
299 /************************************/
300 globle int GetPPBufferEnabled(
303 return(PrettyPrintData(theEnv)->PPBufferEnabled);