-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtadshtml.cpp
154 lines (124 loc) · 3.6 KB
/
tadshtml.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#ifdef RCSID
static char RCSid[] =
"$Header: d:/cvsroot/tads/html/tadshtml.cpp,v 1.3 1999/07/11 00:46:41 MJRoberts Exp $";
#endif
/*
* Copyright (c) 1997 by Michael J. Roberts. All Rights Reserved.
*
* Please see the accompanying license file, LICENSE.TXT, for information
* on using and copying this software.
*/
/*
Name
tadshtml.cpp - utility functions for TADS HTML engine
Function
Notes
Modified
08/26/97 MJRoberts - Creation
*/
#include <stdio.h>
#include <memory.h>
#ifndef TADSHTML_H
#include "tadshtml.h"
#endif
#ifndef HTMLSYS_H
#include "htmlsys.h"
#endif
#ifndef HTML_OS_H
#include "html_os.h"
#endif
/* ------------------------------------------------------------------------ */
/*
* Text stream buffer. This buffer accumulates output for eventual
* submission to the parser.
*/
/*
* Append text to the end of the buffer
*/
void CHtmlTextBuffer::append(const textchar_t *txt, size_t len)
{
/* make sure we have enough space allocated */
if (buf_ == 0)
{
/* nothing allocated yet - allocate the initial buffer space */
bufalo_ = init_alloc_unit;
buf_ = (textchar_t *)th_malloc(bufalo_ * sizeof(textchar_t));
/* nothing in the buffer yet - the end is the beginning */
bufend_ = buf_;
}
else if (getlen() + len > bufalo_)
{
size_t curlen;
/* remember the current length, so we can reposition bufend_ later */
curlen = getlen();
/* allocate the additional space */
bufalo_ += extra_alloc_unit;
buf_ = (textchar_t *)th_realloc(buf_, bufalo_ * sizeof(textchar_t));
/* update the end pointer, in case the buffer moved */
bufend_ = buf_ + curlen;
}
/* copy the text into the buffer */
memcpy(bufend_, txt, len * sizeof(*txt));
/* remember the new end pointer */
bufend_ += len;
}
/* ------------------------------------------------------------------------ */
/*
* Counted-length string pointer
*/
textchar_t CCntlenStrPtr::nextchar() const
{
/*
* if we have another character after the current character, return
* it, otherwise return a null character
*/
return (textchar_t)(len_ > 1 ? *(ptr_ + 1) : 0);
}
/* ------------------------------------------------------------------------ */
/*
* Safe strcpy
*/
void safe_strcpy(textchar_t *dst, size_t dstsize,
const textchar_t *src, size_t srclen)
{
size_t copylen;
/* if the destination buffer is zero-size, we can't do anything */
if (dstsize == 0)
return;
/* assume we'll copy the whole source */
copylen = srclen;
/* limit to the available destination space less null termination */
if (copylen > dstsize - 1)
copylen = dstsize - 1;
/* copy the characters */
memcpy(dst, src, copylen * sizeof(src[0]));
/* null-terminate */
dst[copylen] = '\0';
}
/* ------------------------------------------------------------------------ */
/*
* Get the rightmost instance of a substring in a string
*/
textchar_t *get_strrstr(const textchar_t *str, const textchar_t *sub)
{
const char *p;
size_t slen = get_strlen(str);
size_t sublen = get_strlen(sub);
/*
* if the substring is longer than the string, we're obviously not
* going to find it
*/
if (sublen > slen)
return 0;
/* scan from the end of the string backwards */
for (p = str + slen - sublen + 1 ; p != str ; )
{
/* back one character */
--p;
/* check for a match */
if (memcmp(p, sub, sublen) == 0)
return (char *)p;
}
/* not found */
return 0;
}