Annotation of win32/development/msvcee/cord/cordxtra.cpp, revision 1.1
1.1 ! paf 1: /*
! 2: * Copyright (c) 1993-1994 by Xerox Corporation. All rights reserved.
! 3: *
! 4: * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
! 5: * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
! 6: *
! 7: * Permission is hereby granted to use or copy this program
! 8: * for any purpose, provided the above notices are retained on all copies.
! 9: * Permission to modify the code and to distribute modified code is granted,
! 10: * provided the above notices are retained, and a notice that the code was
! 11: * modified is included with the above copyright notice.
! 12: *
! 13: * Author: Hans-J. Boehm (boehm@parc.xerox.com)
! 14: */
! 15: /*
! 16: * These are functions on cords that do not need to understand their
! 17: * implementation. They serve also serve as example client code for
! 18: * cord_basics.
! 19: */
! 20: /* Boehm, December 8, 1995 1:53 pm PST */
! 21: # include <stdio.h>
! 22: # include <string.h>
! 23: # include <stdlib.h>
! 24: # include <stdarg.h>
! 25: # include "cord.h"
! 26: # include "ec.h"
! 27: # define I_HIDE_POINTERS /* So we get access to allocation lock. */
! 28: /* We use this for lazy file reading, */
! 29: /* so that we remain independent */
! 30: /* of the threads primitives. */
! 31:
! 32:
! 33: typedef unsigned int GC_word;
! 34:
! 35: /* The standard says these are in stdio.h, but they aren't always: */
! 36: # ifndef SEEK_SET
! 37: # define SEEK_SET 0
! 38: # endif
! 39: # ifndef SEEK_END
! 40: # define SEEK_END 2
! 41: # endif
! 42:
! 43: # define BUFSZ 2048 /* Size of stack allocated buffers when */
! 44: /* we want large buffers. */
! 45:
! 46: typedef void (* oom_fn)(void);
! 47:
! 48: # define OUT_OF_MEMORY { if (CORD_oom_fn != (oom_fn) 0) (*CORD_oom_fn)(); \
! 49: ABORT("Out of memory\n"); }
! 50: # define ABORT(msg) { fprintf(stderr, "%s\n", msg); abort(); }
! 51:
! 52:
! 53: typedef struct {
! 54: size_t len;
! 55: size_t count;
! 56: char * buf;
! 57: } CORD_fill_data;
! 58:
! 59: int CORD_fill_proc(char c, void * client_data)
! 60: {
! 61: register CORD_fill_data * d = (CORD_fill_data *)client_data;
! 62: register size_t count = d -> count;
! 63:
! 64: (d -> buf)[count] = c;
! 65: d -> count = ++count;
! 66: if (count >= d -> len) {
! 67: return(1);
! 68: } else {
! 69: return(0);
! 70: }
! 71: }
! 72:
! 73: int CORD_batched_fill_proc(const char* s, void * client_data)
! 74: {
! 75: register CORD_fill_data * d = (CORD_fill_data *)client_data;
! 76: register size_t count = d -> count;
! 77: register size_t max = d -> len;
! 78: register char * buf = d -> buf;
! 79: register const char* t = s;
! 80:
! 81: while((buf[count] = *t++) != '\0') {
! 82: count++;
! 83: if (count >= max) {
! 84: d -> count = count;
! 85: return(1);
! 86: }
! 87: }
! 88: d -> count = count;
! 89: return(0);
! 90: }
! 91:
! 92: /* Fill buf with len characters starting at i. */
! 93: /* Assumes len characters are available. */
! 94: void CORD_fill_buf(DEBUGHELPER *pHelper, CORD x, size_t i, size_t len, char * buf)
! 95: {
! 96: CORD_fill_data fd;
! 97:
! 98: fd.len = len;
! 99: fd.buf = buf;
! 100: fd.count = 0;
! 101: (void)CORD_iter5(pHelper, x, i, CORD_fill_proc, CORD_batched_fill_proc, &fd);
! 102: }
! 103:
! 104: char * CORD_to_char_star(DEBUGHELPER *pHelper, CORD x)
! 105: {
! 106: register size_t len = CORD_len(x);
! 107: char * result = (char*)malloc(len + 1);
! 108:
! 109: if (result == 0) OUT_OF_MEMORY;
! 110: CORD_fill_buf(pHelper, x, 0, len, result);
! 111: result[len] = '\0';
! 112: return(result);
! 113: }
! 114:
! 115: const char* CORD_to_const_char_star(DEBUGHELPER *pHelper, CORD x)
! 116: {
! 117: if (x == 0) return("");
! 118: if (CORD_IS_STRING(x)) return((const char* )x);
! 119: return(CORD_to_char_star(pHelper, x));
! 120: }
E-mail: