Annotation of win32/development/msvcee/cord/cordxtra.cpp, revision 1.2

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: # define I_HIDE_POINTERS       /* So we get access to allocation lock. */
                     27:                                /* We use this for lazy file reading,   */
                     28:                                /* so that we remain independent        */
                     29:                                /* of the threads primitives.           */
                     30: 
                     31: 
                     32: typedef unsigned int GC_word;
                     33: 
                     34: /* The standard says these are in stdio.h, but they aren't always: */
                     35: # ifndef SEEK_SET
                     36: #   define SEEK_SET 0
                     37: # endif
                     38: # ifndef SEEK_END
                     39: #   define SEEK_END 2
                     40: # endif
                     41: 
                     42: # define BUFSZ 2048    /* Size of stack allocated buffers when         */
                     43:                        /* we want large buffers.                       */
                     44: 
                     45: typedef void (* oom_fn)(void);
                     46: 
                     47: # define OUT_OF_MEMORY {  if (CORD_oom_fn != (oom_fn) 0) (*CORD_oom_fn)(); \
                     48:                          ABORT("Out of memory\n"); }
                     49: # define ABORT(msg) { fprintf(stderr, "%s\n", msg); abort(); }
                     50: 
                     51: 
                     52: typedef struct {
                     53:        size_t len;
                     54:        size_t count;
                     55:        char * buf;
                     56: } CORD_fill_data;
                     57: 
                     58: int CORD_fill_proc(char c, void * client_data)
                     59: {
                     60:     register CORD_fill_data * d = (CORD_fill_data *)client_data;
                     61:     register size_t count = d -> count;
                     62:     
                     63:     (d -> buf)[count] = c;
                     64:     d -> count = ++count;
                     65:     if (count >= d -> len) {
                     66:        return(1);
                     67:     } else {
                     68:        return(0);
                     69:     }
                     70: }
                     71: 
                     72: int CORD_batched_fill_proc(const char*  s, void * client_data)
                     73: {
                     74:     register CORD_fill_data * d = (CORD_fill_data *)client_data;
                     75:     register size_t count = d -> count;
                     76:     register size_t max = d -> len;
                     77:     register char * buf = d -> buf;
                     78:     register const char*  t = s;
                     79:     
                     80:     while((buf[count] = *t++) != '\0') {
                     81:         count++;
                     82:         if (count >= max) {
                     83:             d -> count = count;
                     84:             return(1);
                     85:         }
                     86:     }
                     87:     d -> count = count;
                     88:     return(0);
                     89: }
                     90: 
                     91: /* Fill buf with len characters starting at i.                         */
                     92: /* Assumes len characters are available.                               */ 
                     93: void CORD_fill_buf(DEBUGHELPER *pHelper, CORD x, size_t i, size_t len, char * buf)
                     94: {
                     95:     CORD_fill_data fd;
                     96:     
                     97:     fd.len = len;
                     98:     fd.buf = buf;
                     99:     fd.count = 0;
                    100:     (void)CORD_iter5(pHelper, x, i, CORD_fill_proc, CORD_batched_fill_proc, &fd);
                    101: }
                    102: 
                    103: char * CORD_to_char_star(DEBUGHELPER *pHelper, CORD x)
                    104: {
                    105:     register size_t len = CORD_len(x);
                    106:     char * result = (char*)malloc(len + 1);
                    107:     
                    108:     if (result == 0) OUT_OF_MEMORY;
                    109:     CORD_fill_buf(pHelper, x, 0, len, result);
                    110:     result[len] = '\0';
                    111:     return(result);
                    112: }
                    113: 
                    114: const char*  CORD_to_const_char_star(DEBUGHELPER *pHelper, CORD x)
                    115: {
                    116:     if (x == 0) return("");
                    117:     if (CORD_IS_STRING(x)) return((const char* )x);
                    118:     return(CORD_to_char_star(pHelper, x));
                    119: }

E-mail: