File: stringbox.c

package info (click to toggle)
deal 3.1.9-12
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,552 kB
  • sloc: ansic: 5,224; cpp: 4,186; tcl: 3,125; makefile: 200; sh: 10
file content (411 lines) | stat: -rw-r--r-- 10,160 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
 * Copyright (C) 1996-2001, Thomas Andrews
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <stdio.h>
#include "stringbox.h"
#include <string.h>

typedef Tcl_UniChar Char;

typedef struct string_box_base {
  int rows, columns;
  Char *start;
  int refcount;
} *StringBoxBase;

typedef struct string_box {
  StringBoxBase parent;
  int rows,columns;
  Char **strings;
} *StringBox;

static void clearStringBox(box)
     StringBox box;
{
  int rows=box->rows, columns=box->columns;
  int r,c;
  for (r=0; r<rows; r++) {
    for (c=0; c<columns; c++) {
      box->strings[r][c] = ' ';
    }
  }
}

static int writeStringBox(interp,box,row,column,string)
     Tcl_Interp *interp; /* May be null.  For error messages */
     StringBox box;
     int row;
     int column;
     Char *string;
{
  int rowcount=box->rows, columncount=box->columns;
  Char *boxstring;
  int startcolumn=column;
  Char c;
  if (row<0 || row>=rowcount || column>=columncount || column<0) {
    if (interp != (Tcl_Interp *)NULL) {
      char s[20];
      sprintf(s,"%d %d",row,column);
      Tcl_AppendResult(interp,"illegal parameters \"",s,"\"",NULL);
    }
    return TCL_ERROR;
  }
  boxstring=box->strings[row]+column;
  while ((c=*(string++))!=0) {
    if (c=='\n') {
      row++;
      if (row>=rowcount) return TCL_OK;
      column=startcolumn;
      boxstring=box->strings[row]+column;
      continue;
    }
    if (column<columncount) {
      *boxstring=c;
      boxstring++;
      column++;
    }
  }
  return TCL_OK;
}

static Char *formatStringBox(box,lengthPtr)
     StringBox box;
     int *lengthPtr;
{
  int row=0,col=0;
  Char *result=(Char*)Tcl_Alloc(sizeof(Char)*(box->rows*(box->columns+1)+1));
  Char *rindex=result;
  for (row=0; row<box->rows; row++) {
    for (col=0; col<box->columns; col++) {
      *(rindex++)=box->strings[row][col];
    }
    *(rindex++)='\n';
  }
  *(--rindex)=0;
  *lengthPtr = rindex - result;
  return result;
}

static Char *compactStringBox(box,lengthPtr)
     StringBox box;
     int *lengthPtr;
{
  int row,col;
  Char *result=(Char *)Tcl_Alloc(sizeof(Char)*((box->rows)*(box->columns+1)+1));
  Char *rindex=result;
  Char c,*lastnonspace;
  for (row=0; row<box->rows; row++) {
    lastnonspace=rindex;
    for (col=0; col<box->columns; col++) {
      *(rindex++)=c=box->strings[row][col];
      if (c!=' ') {
	lastnonspace=rindex;
      }
    }
    rindex=lastnonspace;
    *(rindex++)='\n';
  }
  *(--rindex)=0;
  *lengthPtr = rindex-result;
  return result;
}


static StringBox newStringBox(rows,columns)
     int rows;
     int columns;
{
  StringBoxBase parent;
  StringBox result;
  int i;
  if (rows<=0 || columns<=0) {
    return 0;
  }
  parent=(StringBoxBase)Tcl_Alloc(sizeof(struct string_box_base));
  parent->rows=rows;
  parent->columns=columns;
  parent->start=(Char *)Tcl_Alloc(rows*columns*sizeof(Char));
  parent->refcount=1;

  result=(StringBox)Tcl_Alloc(sizeof(struct string_box));
  result->rows=rows;
  result->columns=columns;
  result->strings=(Char **)Tcl_Alloc(rows*sizeof(Char **));

  for (i=0; i<rows; i++) {
    result->strings[i]=parent->start+i*columns;
  }
      
  result->parent=parent;

  clearStringBox(result);
  return result;
}

static StringBox subStringBox(interp,parentbox,rowloc,columnloc,rows,columns)
     Tcl_Interp *interp; /* For error messages only */
     StringBox parentbox;
     int rowloc,columnloc;
     int rows;
     int columns;
{
  StringBoxBase parent;
  StringBox result;
  int i;
  
  if (rows<=0 || columns<=0 || rowloc< 0 || columnloc<0) {
    goto error;
  }
  
  if (rows+rowloc>parentbox->rows || rows+columnloc>parentbox->columns) {
    goto error;
  }
  
  parent=parentbox->parent;
  parent->refcount++;
  
  result=(StringBox)Tcl_Alloc(sizeof(struct string_box));
  
  result->rows=rows;
  result->columns=columns;
  result->strings=(Char **)Tcl_Alloc(rows*sizeof(Char **));
  
  for (i=0; i<rows; i++) {
    result->strings[i]=parentbox->strings[rowloc+i]+columnloc;
  }
  
  result->parent=parent;
  
  clearStringBox(result);
  return result;
 error:

  if (interp != (Tcl_Interp *)NULL) {
    Tcl_AppendResult(interp, "subbox: illegal argument",NULL);
  }
  return 0;
}


	
void deleteStringBox(ClientData boxdata)
{
  StringBox box=(StringBox)boxdata;
  Tcl_Free((char *)box->strings);
#ifdef DEBUG
  fprintf(stderr,"Deleting stringbox\n");
#endif
  if (--(box->parent->refcount) <=0) {
    Tcl_Free((char *)box->parent->start);
    Tcl_Free((char *)box->parent);
#ifdef DEBUG
    fprintf(stderr,"Deleting parent box\n");
#endif
  }
  Tcl_Free((char *)box);
}

static int tcl_string_box(TCLOBJ_PARAMS)
     TCLOBJ_DECL
{
  const char *command;
  StringBox box=(StringBox)cd;
  int len;
  if (objc==1) {
    int length;
    Char *result = formatStringBox(box,&length);
    Tcl_Obj *unicode = Tcl_NewUnicodeObj(result,length);
    Tcl_SetObjResult(interp,unicode);
    Tcl_Free((char *)result);
    return TCL_OK;
  }

  command=Tcl_GetStringFromObj(objv[1],&len);

  if (command==NULL) {
    return TCL_ERROR;
  }

  if ('w'==(*command) && strcmp(command,"write")==0) {
    int row, col;
    if (objc!=5) {
      Tcl_AppendResult(interp, "wrong # args: should be \"", 
		       Tcl_GetString(objv[0]),
		       " write row column string\"",NULL);
      return TCL_ERROR;
    }
    if  (Tcl_GetIntFromObj(interp,objv[2],&row)!=TCL_OK ||
	 Tcl_GetIntFromObj(interp,objv[3],&col)!=TCL_OK) {
      Tcl_AppendResult(interp,"\nbad row or column arguments: row=\"",
		       Tcl_GetString(objv[2]),
		       "\", column=\"",
		       Tcl_GetString(objv[3]),"\"", NULL);
      return TCL_ERROR;
    }

    return writeStringBox(interp,box,row,col,Tcl_GetUnicode(objv[4]));

  }
  
  if (*command=='c' && strcmp(command,"clear")==0) {
    clearStringBox(box);
    return TCL_OK;
  }

  if (*command=='r' && strcmp(command,"rows")==0) {
    if (objc!=2) {
      Tcl_AppendResult(interp, "wrong # args: should be \"", 
		       Tcl_GetString(objv[0]),
		       " ",command,"\"", NULL);
      return TCL_ERROR;
    }
    Tcl_SetObjResult(interp,Tcl_NewIntObj(box->rows));
    return TCL_OK;
  }

  if (*command=='c' && strcmp(command,"columns")==0) {
    if (objc!=2) {
      Tcl_AppendResult(interp, "wrong # args: should be \"", 
		       Tcl_GetString(objv[0]),
		       " ",command,"\"", NULL);
      return TCL_ERROR;
    }
    Tcl_SetObjResult(interp,Tcl_NewIntObj(box->columns));
    return TCL_OK;
  }

  if (*command=='s' && strcmp(command,"subbox")==0) {
    StringBox subbox;
    int rows=0,columns=0,rowloc=0,columnloc=0;
    
    if (objc!=7 && objc!=5) {
      Tcl_AppendResult(interp, "wrong # args: should be \"", 
		       Tcl_GetString(objv[0]),
		       " subbox name rowloc columnloc [ rows columns ]\"",
		       (Char *)NULL);
      return TCL_ERROR;
    }
    
    if (Tcl_GetIntFromObj(interp,objv[3],&rowloc) != TCL_OK) {
      if (rowloc<0) {
	Tcl_AppendResult(interp,"Illegal row location \"",
			 Tcl_GetString(objv[3]),"\" passed to subbox routine",NULL);
      }     
      return TCL_ERROR;
    }
    
    if (Tcl_GetIntFromObj(interp,objv[4],&columnloc) != TCL_OK) {
      if (columnloc<0) {
	Tcl_AppendResult(interp,"Illegal column location \"",
			 Tcl_GetString(objv[4]),"\" passed to subbox routine",NULL);
      }
      return TCL_ERROR;
    }
    if (objc==7) {
      if ((Tcl_GetIntFromObj(interp,objv[5],&rows) != TCL_OK)) {
	return TCL_ERROR;
      }
    
      if ((Tcl_GetIntFromObj(interp,objv[6],&columns) != TCL_OK)) {
	return TCL_ERROR;
      }
    }

    if (rowloc<0) {
      rowloc=(box->rows)+rowloc;
    }
    if (columnloc<0) {
      columnloc=(box->columns)+columnloc;
    }

    if (rows<=0) {
      rows=rows+(box->rows)-rowloc;
    }
    if (columns<=0) {
      columns=columns+(box->columns)-columnloc;
    }

    subbox=subStringBox(interp,box,rowloc,columnloc,rows,columns);

    if (subbox==(StringBox)0) {
      return TCL_ERROR;
    }

    Tcl_CreateObjCommand(interp,Tcl_GetString(objv[2]),tcl_string_box,subbox,deleteStringBox);

    return TCL_OK;
  }

  if (*command=='c' && strcmp(command,"compact")==0) {
    int length;
    Char *result = compactStringBox(box,&length);
    Tcl_Obj *unicode = Tcl_NewUnicodeObj(result,length);
    Tcl_SetObjResult(interp,unicode);
    Tcl_Free((char *)result);
    return TCL_OK;
  }

  Tcl_AppendResult(interp, "unknown stringbox operation: \"", 
		   Tcl_GetString(objv[0]), " ",
		   command," ...\"",NULL);
  return TCL_ERROR;
}

int tcl_create_string_box(TCLOBJ_PARAMS)
     TCLOBJ_DECL
{
  int rows,columns;
  StringBox box;
   
  if (objc!=4) {
    Tcl_AppendResult(interp, "wrong # args: should be \"", 
		     Tcl_GetString(objv[0]),
		     " name rows columns\"",(Char *)NULL);
    return TCL_ERROR;
  }
    
  if ((Tcl_GetIntFromObj(interp,objv[2],&rows) != TCL_OK) || (rows<=0)) {
    Tcl_AddErrorInfo(interp,
		     "\n (reading value of <rows>)");
    return TCL_ERROR;
  }

  if (Tcl_GetIntFromObj(interp,objv[3],&columns) != TCL_OK || (columns<=0)) {
    Tcl_AddErrorInfo(interp,
		     "\n (reading value of <columns>)");
    return TCL_ERROR;
  }

  box=newStringBox(rows,columns);
  Tcl_CreateObjCommand(interp,Tcl_GetString(objv[1]),tcl_string_box,box,deleteStringBox);
  return TCL_OK;
}

int Stringbox_Init(interp)
     Tcl_Interp *interp;
{
#if 0
  int code;
  code = Tcl_PkgProvide(interp,"stringbox","1.0");
  if (code != TCL_OK) {
    return code;
  }
#endif
  Tcl_CreateObjCommand(interp,"stringbox",tcl_create_string_box,NULL,NULL);
  return TCL_OK;
}