The SWORD Project  1.9.0.svnversion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
swbuf.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * swbuf.cpp - used as a transport and utility for data buffers
4  *
5  * $Id: swbuf.cpp 3714 2020-04-10 23:43:12Z scribe $
6  *
7  * Copyright 2003-2013 CrossWire Bible Society (http://www.crosswire.org)
8  * CrossWire Bible Society
9  * P. O. Box 2528
10  * Tempe, AZ 85280-2528
11  *
12  * This program is free software; you can redistribute it and/or modify it
13  * under the terms of the GNU General Public License as published by the
14  * Free Software Foundation version 2.
15  *
16  * This program is distributed in the hope that it will be useful, but
17  * WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * General Public License for more details.
20  *
21  */
22 
23 #include <swbuf.h>
24 
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 
29 #include <stringmgr.h>
30 
32 
33 
34 char *SWBuf::nullStr = (char *)"";
35 
36 
37 /*
38 SWBuf::SWBuf(unsigned long initSize) {
39  init(initSize);
40  set((const char *)0);
41 }
42 */
43 
44 
45 /******************************************************************************
46 * SWBuf::setFormatted - sets this buf to a formatted string
47 * WARNING: This function can only write at most
48 * JUNKBUFSIZE to the string per call.
49 */
50 SWBuf &SWBuf::setFormatted(const char *format, ...) {
51  va_list argptr;
52 
53  va_start(argptr, format);
54 
55  setFormattedVA(format, argptr);
56 
57  va_end(argptr);
58  return *this;
59 }
60 
61 SWBuf &SWBuf::setFormattedVA(const char *format, va_list argptr) {
62  va_list argptr2;
63  va_copy(argptr2, argptr);
64 #ifdef NO_VSNPRINTF
65  static char junkBuf[JUNKBUFSIZE];
66  int len = vsprintf(junkBuf, format, argptr)+1;
67 #else
68  int len = vsnprintf(0, 0, format, argptr)+1;
69 #endif
70  assureSize(len);
71  end = vsprintf(buf, format, argptr2) + buf;
72  va_end(argptr2);
73  return *this;
74 }
75 
76 /******************************************************************************
77 * SWBuf::appendFormatted - appends formatted strings to the current value of this SWBuf
78 * WARNING: This function can only write at most
79 * JUNKBUFSIZE to the string per call.
80 */
81 SWBuf &SWBuf::appendFormatted(const char *format, ...) {
82  va_list argptr;
83 
84  va_start(argptr, format);
85 #ifdef NO_VSNPRINTF
86  static char junkBuf[JUNKBUFSIZE];
87  int len = vsprintf(junkBuf, format, argptr)+1;
88 #else
89  int len = vsnprintf(0, 0, format, argptr)+1;
90 #endif
91  va_end(argptr);
92  assureMore(len);
93  va_start(argptr, format);
94  end += vsprintf(end, format, argptr);
95  va_end(argptr);
96  return *this;
97 }
98 
99 void SWBuf::insert(unsigned long pos, const char* str, unsigned long start, signed long max) {
100 // if (!str) //A null string was passed
101 // return;
102 
103  str += start;
104  int len = (int)((max > -1) ? max : strlen(str));
105 
106  if (!len || (pos > length())) //nothing to do, return
107  return;
108 
109  // pos==length(), so we can call append in this case
110  if (pos == length()) { //append is more efficient
111  append(str, max);
112  return;
113  }
114 
115  assureMore( len );
116 
117  memmove(buf + pos + len, buf + pos, (end - buf) - pos); //make a gap of "len" bytes
118  memcpy(buf+pos, str, len);
119 
120  end += len;
121  *end = 0;
122 }
123 
124 
133  char *utf8 = 0;
134  stdstr(&utf8, c_str(), 3);
135  sword::toupperstr(utf8, (unsigned int)size()*3-1);
136  *this = utf8;
137  delete [] utf8;
138 
139  return *this;
140 }
142  char *utf8 = 0;
143  stdstr(&utf8, c_str(), 3);
144  sword::tolowerstr(utf8, (unsigned int)size()*3-1);
145  *this = utf8;
146  delete [] utf8;
147 
148  return *this;
149 }
150 
char * buf
Definition: swbuf.h:50
#define SWORD_NAMESPACE_START
Definition: defs.h:39
SWBuf & appendFormatted(const char *format,...)
Definition: swbuf.cpp:81
Definition: swbuf.h:47
unsigned long length() const
Definition: swbuf.h:197
SWBuf & setFormattedVA(const char *format, va_list argptr)
Definition: swbuf.cpp:61
int pos
Definition: regex.c:5534
char * end
Definition: swbuf.h:51
void assureMore(size_t pastEnd)
Definition: swbuf.h:56
#define JUNKBUFSIZE
Definition: swbuf.h:38
SWORD_NAMESPACE_START char * stdstr(char **ipstr, const char *istr, unsigned int memPadFactor=1)
Definition: utilstr.h:44
void insert(unsigned long pos, const char *str, unsigned long start=0, signed long max=-1)
Definition: swbuf.cpp:99
SWBuf & toLower()
Definition: swbuf.cpp:141
const char * c_str() const
Definition: swbuf.h:158
SWBuf & append(const char *str, long max=-1)
Definition: swbuf.h:274
void assureSize(size_t checkSize)
Definition: swbuf.h:62
char * tolowerstr(char *t, unsigned int max=0)
Definition: stringmgr.h:111
regoff_t * start
Definition: regex.h:407
unsigned long size() const
Definition: swbuf.h:185
SWBuf & toUpper()
Definition: swbuf.cpp:132
char * toupperstr(char *t, unsigned int max=0)
Definition: stringmgr.h:107
#define SWORD_NAMESPACE_END
Definition: defs.h:40
SWBuf & setFormatted(const char *format,...)
Definition: swbuf.cpp:50
static char * nullStr
Definition: swbuf.h:87