aboutsummaryrefslogtreecommitdiff
path: root/Src/nswasabi/AutoCharNX.h
blob: 54a80786ba987b674c5d18aa2cd1262e8ef53967 (plain) (blame)
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
#pragma once
#include "nx/nxstring.h"
#include "nx/nxuri.h"
#include "foundation/error.h"

template <nx_charset_t charset>
class AutoCharNX
{
public:
	AutoCharNX()
	{
		Init();
	}

	AutoCharNX(size_t bytes)
	{
		Init();
		ptr = (char *)malloc(bytes);
		malloc_size = bytes;
	}

	AutoCharNX(nx_string_t string)
	{
		Init();

		Set(string);
	}

	AutoCharNX(nx_uri_t filename)
	{
		Init();

		Set(filename);
	}

	~AutoCharNX()
	{
		if (owned)
			free(ptr);
		if (reference_string)
			NXStringRelease(reference_string);
	}

	int Set(nx_string_t string)
	{
		if (reference_string == string)
			return NErr_Success;

		if (reference_string)
			NXStringRelease(reference_string);
		reference_string=0;

		size_t byte_count=0;
		int ret = NXStringGetBytesSize(&byte_count, string, charset, nx_string_get_bytes_size_null_terminate);
		if(ret == NErr_DirectPointer)
		{
			if (owned)
			{
				free(ptr);
				ptr=0;
				length=0;
				malloc_size=0;
			}
			ret = NXStringGetBytesDirect((const void **)&ptr, &length, string, charset, nx_string_get_bytes_size_null_terminate);
			reference_string = NXStringRetain(string);
			owned=false;
		}
		else if (ret == NErr_Success)
		{
			if (owned)
			{
				if (byte_count > malloc_size)
				{
					ptr = (char *)realloc(ptr, byte_count);
					malloc_size = byte_count;
				}
			}
			else
			{
				/* not owned. need to allocate */
				ptr = (char *)malloc(byte_count);
				malloc_size = byte_count;
				owned=true;
			}

			if (ptr)
			{
				ret = NXStringGetBytes(&length, string, ptr, byte_count, charset, nx_string_get_bytes_size_null_terminate);
			}
			else
			{
				return NErr_OutOfMemory;
			}
		}
		else
		{
			Clear();
		}
		return ret;
	}

	int Set(nx_uri_t filename)
	{
		int ret;
		nx_string_t string;
		ret = NXURIGetNXString(&string, filename);
		if (ret == NErr_Success)
		{
			ret = Set(string);
			NXStringRelease(string);
		}
		else
		{
			Clear();
			// failed! we need to clean up
		}
		return ret;
	}

	operator const char *() const
	{
		if (length)
			return ptr;
		else
			return 0;
	}

	/* this one will never return a NULL, always a valid string */
	const char *GetValidString() const
	{
		if (length)
			return ptr;
		else
			return "";
	}

	/* the Clear function clears the string but doesn't deallocate memory */
	void Clear()
	{
		if (!owned)
			ptr=0;
		length=0;
	
		if (reference_string)
			NXStringRelease(reference_string);
		reference_string=0;
	}

private:
	void Init()
	{
		ptr=0;
		length=0;
		owned=false;
		reference_string=0;
		malloc_size=0;
	}
	char *ptr;
	size_t length;
	size_t malloc_size;
	bool owned;
	nx_string_t reference_string;
};

typedef AutoCharNX<nx_charset_utf8> AutoCharUTF8;
#define AutoCharPrintfUTF8(x) (AutoCharUTF8(x).GetValidString())
class AutoCharNative
{
public:
};

class AutoFilename
{
public:
};