1 /*
2  * PROPRIETARY INFORMATION.  This software is proprietary to POWDER
3  * Development, and is not to be reproduced, transmitted, or disclosed
4  * in any way without written permission.
5  *
6  * Produced by:	Jeff Lait
7  *
8  *      	POWDER Development
9  *
10  * NAME:        buf.h ( POWDER Library, C++ )
11  *
12  * COMMENTS:
13  *	Implements a simple character buffer.  Underlying data
14  *	is reference counted and can be passed by value.
15  *	Uses copy on write semantic.
16  */
17 
18 #ifndef __buf__
19 #define __buf__
20 
21 #include <stdarg.h>
22 
23 // Returns number of active buffers.
24 // This should be zero most of the time.  Or at least bounded.
25 int
26 buf_numbufs();
27 
28 // Internal buffer representation
29 class BUF_int;
30 
31 class BUF
32 {
33 public:
34     BUF();
35     BUF(int len);
36     ~BUF();
37 
38     // Copy constructors
39     BUF(const BUF &buf);
40     BUF &operator=(const BUF &buf);
41 
42     // Allocates a blank buffer of the given size to ourself.
43     void	allocate(int len);
44 
45     // Sets us to an empty string, but not a shared empty string!
46     void	clear();
47 
48     // Determines if we are a non-null and non-empty string
49     bool	isstring() const;
50 
51     // Read only access.  Only valid so long as we are scoped and not
52     // editted.
53     const char *buffer() const;
54 
55     // Acquires ownership of text, will delete [] it.
56     void	steal(char *text);
57 
58     // Points to the text without gaining ownership.  If you try to write
59     // to this, you will not alter text!
60     void	reference(const char *text);
61 
62     // Copies the text into our own buffer, so the source may do as it wishes.
63     void	strcpy(const char *text);
strcpy(BUF buf)64     void	strcpy(BUF buf)
65 		{ *this = buf; }
66 
67     int		strcmp(const char *cmp) const;
strcmp(BUF buf)68     inline int	strcmp(BUF buf) const
69 		{ return strcmp(buf.buffer()); }
70 
71     // Matches strlen.
72     int		strlen() const;
73 
74     // Returns a malloc() copy of self, just like strdup(this->buffer())
75     // THis is safe to call on a temporary, unlike the above construction.
76     char	*strdup() const;
77 
78     // Like strcat, but no concerns of overrun
79     void	strcat(const char *text);
strcat(BUF buf)80     void	strcat(BUF buf)
81 		{ strcat(buf.buffer()); }
82 
83     char	lastchar(int nthlast = 0) const;
84 
85     // Adds a single character.
86     void	append(char c);
87 
88     // Our good friends.  Now with no buffer overruns.
89     int		vsprintf(const char *fmt, va_list ap);
90     int		sprintf(const char *fmt, ...);
91     int		appendSprintf(const char *fmt, ...);
92 
93     // Makes this a writeable buffer.
94     void	uniquify();
95     // Access underlying data.  Never use this :>
96     char	*evildata();
97 
98     // Fun string manipulation functions
99 
100     // Converts almost everything into _ so people are happier.
101     void	 makeFileSafe();
102 
103 protected:
104     BUF_int	*myBuffer;
105 };
106 
107 #endif
108