File: buf.c

package info (click to toggle)
memchan 2.3%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,356 kB
  • sloc: ansic: 3,320; sh: 990; tcl: 687; makefile: 48
file content (648 lines) | stat: -rw-r--r-- 12,752 bytes parent folder | download | duplicates (3)
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
/*
 * buf.c --
 *
 *	Implementations of functions in the platform independent public Buf API.
 *
 * Copyright (c) 2000 by Andreas Kupries <a.kupries@westend.com>
 *
 * See the file "license.terms" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 * RCS: @(#) $Id: buf.c,v 1.2 2010/12/08 18:17:36 andreas_kupries Exp $
 */

#include "buf.h"
#include "memchanInt.h"

/*
 * Codestring used to associate the
 * initialization flag with an interpreter.
 */

#define ASSOC "memchan:buf"

/* Internal declaration of buffers.
 */

typedef struct Buffer_ {
  Buf_BufferType* type;       /* Reference to the type of the buffer */
  ClientData      clientData; /* The information pertinent to the used type. */
  int             refCount;   /* Number of references hold by other parts
			       * of the application to this buffer. Initialized
			       * to 0 by the creator procedures implemented here.
			       */
} Buffer;

/* Internal declaration of a buffer position.
 */

typedef struct BufferPosition_ {
  Buf_Buffer buf;    /* The buffer this position belongs to */
  int        offset; /* The offset into the data area */
} BufferPosition;

/*
 *------------------------------------------------------*
 *
 *	Buf_IsInitialized --
 *
 *	Check wether the buffer system is initialized for
 *	the specified interpreter
 *
 *	Sideeffects:
 *		None.
 *
 *	Result:
 *		A boolean value. 0 indicates no
 *		initialization, 1 the opposite.
 *
 *------------------------------------------------------*
 */

int
Buf_IsInitialized (interp)
     Tcl_Interp* interp;
{
  Tcl_InterpDeleteProc* proc = (Tcl_InterpDeleteProc*) NULL;
  return PTR2INT (Tcl_GetAssocData (interp, ASSOC, &proc));
}

/*
 *------------------------------------------------------*
 *
 *	Buf_Init --
 *
 *	Initializes the buffer system in the specified
 *	interpreter.
 *
 *	Sideeffects:
 *		Associates data with the specified
 *		interpreter to remember the initialization
 *
 *	Result:
 *		A standard TCL error code.
 *
 *------------------------------------------------------*
 */


int
Buf_Init (interp)
     Tcl_Interp* interp;
{
  /* There is nothing to initialize here. But we have an operation
   * querying the state of initialization, so we have to remember
   * at least this.
   */

  if (Buf_IsInitialized (interp)) {
    /* catch multiple initialization of an interpreter
     */
    return TCL_OK;
  }

  Tcl_SetAssocData (interp, ASSOC,
		    (Tcl_InterpDeleteProc*) NULL,
		    INT2PTR (1));

  return TCL_OK;
}

/*
 *------------------------------------------------------*
 *
 *	Buf_RegisterType --
 *
 *	Registers a new buffer type.
 *
 *	Sideeffects:
 *		None.
 *
 *	Result:
 *		None.
 *
 *------------------------------------------------------*
 */

void
Buf_RegisterType (bufType)
     Buf_BufferType* bufType;
{
  /* There are currently no operations in the interface which require
   * an internal list of registered buffer types, etc. So this function
   * can and will be a no-op for now.
   */
}

/*
 *------------------------------------------------------*
 *
 *	Buf_IncrRefcount --
 *
 *	Tells the specified buffer that a new reference
 *	to it is hold by someone else.
 *
 *	Sideeffects:
 *		See above.
 *
 *	Result:
 *		None.
 *
 *------------------------------------------------------*
 */

void
Buf_IncrRefcount (buf)
     Buf_Buffer buf;
{
  ((Buffer*) buf)->refCount ++;
}

/*
 *------------------------------------------------------*
 *
 *	Buf_DecrRefcount --
 *
 *	Tells the specified buffer that a reference to it
 *	is no longer in existence.
 *
 *	Sideeffects:
 *		Deletes the buffer if the last reference
 *		to it is released.
 *
 *	Result:
 *		None.
 *
 *------------------------------------------------------*
 */

void
Buf_DecrRefcount (buf)
     Buf_Buffer buf;
{
  Buffer* iBuf = (Buffer*) buf;

  iBuf->refCount --;

  if (iBuf->refCount <= 0) {
    /* No references are hold by anyone else anymore.
     * So remove the buffer, we won't need it again.
     */

    iBuf->type->freeProc (buf, iBuf->clientData);
    Tcl_Free ((char*) iBuf);
  }
}

/*
 *------------------------------------------------------*
 *
 *	Buf_IsShared --
 *
 *	Checks wether the buffer is shared among
 *	different parts of the application.
 *
 *	Sideeffects:
 *		None.
 *
 *	Result:
 *		A boolean value. 1 indicates a shared
 *		buffer, 0 the opposite.
 *
 *------------------------------------------------------*
 */

int
Buf_IsShared (buf)
     Buf_Buffer buf;
{
  return (((Buffer*) buf)->refCount > 1);
}

/*
 *------------------------------------------------------*
 *
 *	Buf_GetType --
 *
 *	Retrieves the type structure of a buffer.
 *
 *	Sideeffects:
 *		None.
 *
 *	Result:
 *		A reference to the type of the buffer.
 *
 *------------------------------------------------------*
 */

Buf_BufferType*
Buf_GetType (buf)
     Buf_Buffer buf;
{
  return ((Buffer*) buf)->type;
}

/*
 *------------------------------------------------------*
 *
 *	Buf_GetTypeName --
 *
 *	Retrieves the name of the type of a buffer.
 *
 *	Sideeffects:
 *		None.
 *
 *	Result:
 *		A reference to the string containing the
 *		name of the type of the buffer.
 *
 *------------------------------------------------------*
 */

CONST char*
Buf_GetTypeName (buf)
     Buf_Buffer buf;
{
  return ((Buffer*) buf)->type->typeName;
}

/*
 *------------------------------------------------------*
 *
 *	Buf_Size --
 *
 *	Returns the number of bytes currently stored in
 *	the buffer.
 *
 *	Sideeffects:
 *		None.
 *
 *	Result:
 *		See above.
 *
 *------------------------------------------------------*
 */

int
Buf_Size (buf)
     Buf_Buffer buf;
{
  Buffer* iBuf = (Buffer*) buf;
  return iBuf->type->sizeProc (buf, iBuf->clientData);
}

/*
 *------------------------------------------------------*
 *
 *	Buf_GetClientData --
 *
 *	Retrieves the type specific client information
 *	of a buffer.
 *
 *	Sideeffects:
 *		None.
 *
 *	Result:
 *		The client information of the buffer.
 *
 *------------------------------------------------------*
 */

ClientData
Buf_GetClientData (buf)
     Buf_Buffer buf;
{
  return ((Buffer*) buf)->clientData;
}

/*
 *------------------------------------------------------*
 *
 *	Buf_Create --
 *
 *	Create a new buffer (refcount 0) from its type
 *	and the asssociated type specific information
 *
 *	Sideeffects:
 *		Allocates memory.
 *
 *	Result:
 *		A new buffer token.
 *
 *------------------------------------------------------*
 */


Buf_Buffer
Buf_Create (bufType, clientData)
     Buf_BufferType* bufType;
     ClientData      clientData;
{
  Buffer* iBuf     = (Buffer*) Tcl_Alloc (sizeof (Buffer));

  iBuf->type       = bufType;
  iBuf->clientData = clientData;
  iBuf->refCount   = 0;

  return (Buf_Buffer) iBuf;
}

/*
 *------------------------------------------------------*
 *
 *	Buf_Dup --
 *
 *	Duplicates a buffer and its contents.
 *
 *	Sideeffects:
 *		As of the used buffer driver.
 *
 *	Result:
 *		A new buffer token.
 *
 *------------------------------------------------------*
 */

Buf_Buffer
Buf_Dup (buf)
     Buf_Buffer buf;
{
  Buffer* iBuf = (Buffer*) buf;
  return iBuf->type->dupProc (buf, iBuf->clientData);
}

/*
 *------------------------------------------------------*
 *
 *	Buf_Read --
 *
 *	Reads at most size bytes from the current location
 *	in the buffer and stores it into outbuf.
 *
 *	Sideeffects:
 *		Moves the read pointer behind the bytes
 *		just read from the buffer.
 *
 *	Result:
 *		The number of bytes actually read from
 *		the buffer.
 *
 *------------------------------------------------------*
 */

int
Buf_Read (buf, outbuf, size)
     Buf_Buffer  buf;
     VOID*       outbuf;
     int         size;
{
  Buffer* iBuf = (Buffer*) buf;
  return iBuf->type->readProc (buf, iBuf->clientData, outbuf, size);
}

/*
 *------------------------------------------------------*
 *
 *	Buf_Write --
 *
 *	Writes at most size bytes from inbuf and appends
 *	it the buffer
 *
 *	Sideeffects:
 *		Moves the write pointer behind the bytes
 *		just written into the buffer.
 *
 *	Result:
 *		The number of bytes actually written
 *		into the buffer.
 *
 *------------------------------------------------------*
 */

int
Buf_Write (buf, inbuf, size)
     Buf_Buffer  buf;
     CONST VOID* inbuf;
     int         size;
{
  Buffer* iBuf = (Buffer*) buf;

  if (iBuf->type->writeProc == NULL) {
    return 0;
  } else {
    return iBuf->type->writeProc (buf, iBuf->clientData, inbuf, size);
  }
}

/*
 *------------------------------------------------------*
 *
 *	Buf_PositionPtr --
 *
 *	Converts the logical location in the buffer into
 *	a reference to the data area.
 *
 *	Sideeffects:
 *		None.
 *
 *	Result:
 *		A character pointer
 *
 *------------------------------------------------------*
 */

char*
Buf_PositionPtr (loc)
     Buf_BufferPosition loc;
{
  BufferPosition* bPos = (BufferPosition*) loc;
  Buffer*         iBuf = (Buffer*) bPos->buf;
  char*           data = iBuf->type->dataProc (bPos->buf, iBuf->clientData);

  return data + bPos->offset;
}

/*
 *------------------------------------------------------*
 *
 *	Buf_PositionOffset --
 *
 *	Converts the logical location in the buffer into
 *	an offset relative to the start of the data area
 *	of the underlying buffer.
 *
 *	Sideeffects:
 *		None.
 *
 *	Result:
 *		An integer value.
 *
 *------------------------------------------------------*
 */

int
Buf_PositionOffset (loc)
     Buf_BufferPosition loc;
{
  BufferPosition* bPos = (BufferPosition*) loc;
  return bPos->offset;
}

/*
 *------------------------------------------------------*
 *
 *	Buf_Tell --
 *
 *	Creates a logical position in the buffer pointing
 *	to the current location of the read pointer.
 *
 *	Sideeffects:
 *		Allocates memory, adds a reference to
 *		the buffer.
 *
 *	Result:
 *		A buffer position.
 *
 *------------------------------------------------------*
 */

Buf_BufferPosition
Buf_Tell (buf)
     Buf_Buffer buf;
{
  Buffer*         iBuf = (Buffer*) buf;
  BufferPosition* bPos = (BufferPosition*) Tcl_Alloc (sizeof (BufferPosition));

  bPos->buf    = buf;
  bPos->offset = iBuf->type->tellProc (buf, iBuf->clientData);

  /*
   * The position holds a reference to the buffer, remember that.
   */

  Buf_IncrRefcount (buf);

  return (Buf_BufferPosition) bPos;
}

/*
 *------------------------------------------------------*
 *
 *	Buf_FreePosition --
 *
 *	Deletes a logical position in a buffer.
 *
 *	Sideeffects:
 *		Deallocates memory, removes a reference to
 *		the buffer, may free the referenced buffer.
 *
 *	Result:
 *		None.
 *
 *------------------------------------------------------*
 */

void
Buf_FreePosition (loc)
     Buf_BufferPosition loc;
{
  BufferPosition* bPos = (BufferPosition*) loc;

  Buf_DecrRefcount (bPos->buf);
  Tcl_Free ((char*) bPos);
}

/*
 *------------------------------------------------------*
 *
 *	Buf_MovePosition --
 *
 *	Move a logical position in a buffer.
 *
 *	Sideeffects:
 *		See above.
 *
 *	Result:
 *		None.
 *
 *------------------------------------------------------*
 */

void
Buf_MovePosition (loc, offset)
     Buf_BufferPosition loc;
     int                offset;
{
  BufferPosition* bPos = (BufferPosition*) loc;

  if ((bPos->offset + offset) < 0) {
    Tcl_Panic ("Moved buffer location out of range");
  }

  bPos->offset += offset;
}

/*
 *------------------------------------------------------*
 *
 *	Buf_DupPosition --
 *
 *	Duplicates a logical position in a buffer.
 *
 *	Sideeffects:
 *		Allocates memory, adds another reference
 *		to the underlying buffer.
 *
 *	Result:
 *		A token for the new buffer position.
 *
 *------------------------------------------------------*
 */

Buf_BufferPosition
Buf_DupPosition (loc)
     Buf_BufferPosition loc;
{
  BufferPosition* bPos   = (BufferPosition*) loc;
  BufferPosition* newPos = (BufferPosition*) Tcl_Alloc (sizeof (BufferPosition));

  newPos->buf    = bPos->buf;
  newPos->offset = bPos->offset;

  Buf_IncrRefcount (bPos->buf);

  return (Buf_BufferPosition) newPos;
}

/*
 *------------------------------------------------------*
 *
 *	Buf_PositionFromOffset --
 *
 *	Creates a logical position from an offset into
 *	the data area and a buffer.
 *
 *	Sideeffects:
 *		Allocates memory, adds another reference
 *		to the buffer.
 *
 *	Result:
 *		A token for the new buffer position.
 *
 *------------------------------------------------------*
 */

Buf_BufferPosition
Buf_PositionFromOffset (buf, offset)
     Buf_Buffer buf;
     int        offset;
{
  BufferPosition* newPos = (BufferPosition*) Tcl_Alloc (sizeof (BufferPosition));

  newPos->buf    = buf;
  newPos->offset = offset;

  Buf_IncrRefcount (buf);

  return (Buf_BufferPosition) newPos;
}