summaryrefslogtreecommitdiff
path: root/support/ltx2mathml/src/ltx2mathmlclasses.cpp
blob: 81dbc603ddf7604ce3e837939478c1520a092f1f (plain)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185

#include "ltx2mathmlclasses.h"
#include <stdio.h>

Buffer::Buffer()
{
	m_buf   = NULL;
	m_index = 0;
	m_size  = 0;
}

Buffer::~Buffer()
{
	destroy();
}

void Buffer::destroy()
{
	if( m_buf != NULL )
		free( m_buf );

	m_buf   = NULL;
	m_index = 0;
}

void Buffer::setlength( size_t len )
{
	char *tmp = (char *) realloc( m_buf, len + 1 );

	if( tmp != NULL )
	{
		m_buf = tmp;

		m_size = len;
		if( m_index > len )	// truncated
		{
			m_index = len;
		}
		else
		{			
			ZeroMemory( &m_buf[ m_index ], ( len - m_index ) );
		}
	}
	else
		throw ex_out_of_memory;
}

size_t  Buffer::length()
{
	return m_index;
}

void Buffer::format( const char *fmt, ... )
{
	va_list list;
	size_t len;
	char s[256];

	va_start( list, fmt );
	//len = vsprintf_s( s, sizeof( s ) - 1, fmt, list );
	len = vsprintf( s, fmt, list );
	va_end( list );
	_write( m_index, s, len );
}


void Buffer::_write( size_t index, const char *s, size_t len )
{
	if( !s || ( len <= 0 ) )
	{
		return;
	}

	if( ( m_size - m_index ) <= len	)
	{
		setlength( m_size + len + 1);
		
	}

	memcpy( &m_buf[ index ], s, len );

	
	m_index += len;
}


void Buffer::write( const char *s, size_t len )
{
	_write( m_index, s, len );
}

void Buffer::write( const char *s )
{
	_write( m_index, s, strlen( s ) );
}

char *Buffer::data( size_t *len )
{
	if( len != NULL )
	{
		*len = m_index;
	}

	return m_buf;
}


void Buffer::append( Buffer &buf, bool transfer )
{
	if( transfer && m_index == 0 )
	{
		BufferStruct temp;

		buf.releaseBuffer( temp );

		m_buf   = temp.m_buf;
		m_index = temp.m_index;
		m_size  = temp.m_size;
	}
	else
	{
		_write( m_index, buf.m_buf, buf.m_index );
	}
}

void Buffer::releaseBuffer( BufferStruct &buf )
{
	buf.m_buf   = m_buf;
	buf.m_index = m_index;
	buf.m_size  = m_size;
	
	m_size  = 0;
	m_index = 0;	
	m_buf   = NULL;
}

void Buffer::reset()
{
	ZeroMemory( m_buf, m_index );	
	m_index  = 0;
}

char *Buffer::release()
{
	char *temp;

	temp    = m_buf;
	m_size  = 0;
	m_index = 0;	
	m_buf   = NULL;

	return temp;
}

void Buffer::insertAt( size_t index, const char *s )
{
	size_t len, count;
	char *src, *dest;

	len = strlen( s );

	if( len == 0 )
	  return;

	if( index >= m_index )
	{
		_write( index, s, len );
		return;
	}

	if( ( m_size - m_index ) <= len	)
	{
		setlength( m_size + len + 1);		
	}
	src = m_buf + index;
	dest = src + len;
	
	count = m_index - index;
	
	//memmove_s( dest, m_size, src, count );

	memmove( dest, src, count );	
	memcpy( m_buf + index, s, len );
	m_index += len;
}