00001 /* 00002 * Copyright 2003, 2004 Ian Searle. All rights reserved. 00003 * 00004 * Redistribution and use in source and binary forms, with or without 00005 * modification, are permitted provided that the following 00006 * conditions are met: 00007 * 00008 * 1: Redistributions of source code must retain the above 00009 * copyright notice, this list of conditions and the following 00010 * disclaimer. 00011 * 00012 * 2: Redistributions in binary form must reproduce the above 00013 * copyright notice, this list of conditions and the following 00014 * disclaimer in the documentation and/or other materials 00015 * provided with the distribution. 00016 * 00017 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 00018 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 00019 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00020 * DISCLAIMED. IN NO EVENT SHALL IAN SEARLE OR CONTRIBUTORS 00021 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 00022 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 00023 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 00024 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 00025 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00026 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 00027 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 00028 * DAMAGE. 00029 * 00030 */ 00031 00032 /* 00033 * segment.h 00034 * Definition of Segment library. 00035 * 00036 * Introduction: The segment library is not much all by itself. It 00037 * is merely a container object that holds reference to 00038 * a character array. It does not, and cannot have much 00039 * intelligence. Since we want different Views to have 00040 * different representations of the same Segments, we 00041 * have to put the smarts for those operations in the View. 00042 * 00043 * Notes: 00044 * 00045 * 1: Since multiple Views can reference the same Segments, we use 00046 * reference counts to control the life-cycle of each Segment. 00047 * All segments are created with a reference count of 1. Every 00048 * time segmentDelete() is called, the reference count is 00049 * decremented. Once the reference count reaches zero, the Segment 00050 * is really deleted. 00051 * 00052 * 2: Segments contain string data that may (probably will) contain 00053 * NULL characters. Thus, we cannot use normal C-language string 00054 * operations on them as they will fail in the presence of NULL 00055 * characters embedded in the character array. 00056 * 00057 */ 00058 00059 #ifndef _SEGMENT_H 00060 #define _SEGMENT_H 00061 00062 #include <stdlib.h> 00063 00065 typedef struct _segment Segment; 00066 00068 struct _segment { 00069 char *data; 00070 unsigned int ref_count; 00071 unsigned int len; 00072 }; 00073 00074 /* 00075 * Initialize the segment memory allocation routines. 00076 * The defaults are the system malloc and free. If those 00077 * are acceptable, use of this function is not necessary. 00078 */ 00079 void segment_Init(void *(*seg_allocate)(size_t), void (*seg_deallocate)(void *)); 00080 00081 /* 00082 * Create a Segment with a borrowed reference to the data. 00083 */ 00084 Segment *segment_CreateBorrowed(char *str, unsigned int len); 00085 00086 /* 00087 * Create a Segment with a owned reference to the data. 00088 */ 00089 Segment *segment_CreateOwned(char *str, unsigned int len); 00090 00091 /* 00092 * Delete a Segment (with borrowed data). 00093 * Returns: > 0 if there is an error. 00094 * 0 on sucess. 00095 */ 00096 void segment_DeleteBorrowed(Segment *seg); 00097 00098 /* 00099 * Delete a Segment (with owned data). 00100 * Returns: > 0 if there is an error. 00101 * 0 on sucess. 00102 */ 00103 void segment_DeleteOwned(Segment *seg); 00104 00105 Segment *segment_Copy(Segment *seg); 00106 00107 /* 00108 * Return a character from a segment. 00109 * The character is returned through a character pointer. 00110 */ 00111 unsigned int segment_GetChar(Segment *seg, unsigned int pos, char *retval); 00112 00113 /* 00114 * Return a pointer to the segment's data. 00115 */ 00116 char *segment_DataPtr(Segment *seg, unsigned int index); 00117 00118 /* 00119 * Print a Segment to stdout. 00120 * Primarily for debugging. 00121 */ 00122 void segment_Print(Segment *seg); 00123 00124 /* 00125 * Print just part of a segment to stdout. 00126 */ 00127 unsigned int segment_PrintPiece(Segment *seg, unsigned int start, unsigned int end); 00128 00129 #endif /* _SEGMENT_H */