Compile fixes.
[SquirrelJME.git] / nanocoat / lib / base / listUtil.c
blob35ff941e0b87c69c8a52d108c91759f0514c29cd
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
10 #include <string.h>
12 #include "sjme/listUtil.h"
14 sjme_errorCode sjme_listUtil_binListInt(
15 sjme_attrInNotNull sjme_alloc_pool* inPool,
16 sjme_attrOutNotNull sjme_list_sjme_jint** outList,
17 sjme_attrInNotNull sjme_stream_input inputStream)
19 sjme_errorCode error;
20 sjme_jint length, i;
21 sjme_list_sjme_jint* result;
23 if (inPool == NULL || outList == NULL || inputStream == NULL)
24 return SJME_ERROR_NULL_ARGUMENTS;
26 /* Read in length. */
27 length = INT32_MAX;
28 if (sjme_error_is(error = sjme_stream_inputReadValueJI(
29 inputStream, &length)) ||
30 length == INT32_MAX)
31 return sjme_error_default(error);
33 /* Not valid? */
34 if (length < 0)
35 return SJME_ERROR_INDEX_OUT_OF_BOUNDS;
37 /* Setup target list. */
38 result = NULL;
39 if (sjme_error_is(error = sjme_list_alloc(inPool,
40 length, &result, sjme_jint, 0)) || result == NULL)
41 goto fail_allocList;
43 /* Read in all values. */
44 for (i = 0; i < length; i++)
45 if (sjme_error_is(error = sjme_stream_inputReadValueJI(
46 inputStream, &result->elements[i])))
47 goto fail_readValue;
49 /* Success! */
50 *outList = result;
51 return SJME_ERROR_NONE;
53 fail_readValue:
54 fail_allocList:
55 if (result != NULL)
56 sjme_alloc_free(result);
58 return sjme_error_default(error);
61 sjme_errorCode sjme_listUtil_binListUtf(
62 sjme_attrInNotNull sjme_alloc_pool* inPool,
63 sjme_attrOutNotNull sjme_list_sjme_lpstr** outList,
64 sjme_attrInNotNull sjme_stream_input inputStream)
66 sjme_errorCode error;
67 sjme_jint length, i, actual;
68 sjme_jchar utfLen;
69 sjme_lpstr* values;
71 if (inPool == NULL || outList == NULL || inputStream == NULL)
72 return SJME_ERROR_NULL_ARGUMENTS;
74 /* Read in length. */
75 length = INT32_MAX;
76 if (sjme_error_is(error = sjme_stream_inputReadValueJI(
77 inputStream, &length)) ||
78 length == INT32_MAX)
79 return sjme_error_default(error);
81 /* Not valid? */
82 if (length < 0)
83 return SJME_ERROR_INDEX_OUT_OF_BOUNDS;
85 /* Allocate target values for later list creation. */
86 values = sjme_alloca(sizeof(*values) * length);
87 if (values == NULL)
88 return SJME_ERROR_OUT_OF_MEMORY;
89 memset(values, 0, sizeof(*values) * length);
91 /* Read in all UTF values. */
92 for (i = 0; i < length; i++)
94 /* Read in UTF sequence length. */
95 utfLen = 0;
96 if (sjme_error_is(error = sjme_stream_inputReadValueJS(
97 inputStream, (sjme_jshort*)&utfLen)))
98 return sjme_error_default(error);
100 /* Allocate buffer for string. */
101 values[i] = sjme_alloca(utfLen + 1);
102 if (values[i] == NULL)
103 return SJME_ERROR_OUT_OF_MEMORY;
104 memset(values[i], 0, utfLen + 1);
106 /* Read in. */
107 actual = INT32_MAX;
108 if (sjme_error_is(error = sjme_stream_inputReadFully(
109 inputStream, &actual,
110 values[i], utfLen)))
111 return sjme_error_default(error);
113 /* Wrong count? */
114 if (actual != utfLen)
115 return SJME_ERROR_END_OF_FILE;
118 /* Setup list. */
119 return sjme_list_flattenArgCV(inPool, outList,
120 length, (sjme_lpcstr*)values);