1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
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 // -------------------------------------------------------------------------*/
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
)
21 sjme_list_sjme_jint
* result
;
23 if (inPool
== NULL
|| outList
== NULL
|| inputStream
== NULL
)
24 return SJME_ERROR_NULL_ARGUMENTS
;
28 if (sjme_error_is(error
= sjme_stream_inputReadValueJI(
29 inputStream
, &length
)) ||
31 return sjme_error_default(error
);
35 return SJME_ERROR_INDEX_OUT_OF_BOUNDS
;
37 /* Setup target list. */
39 if (sjme_error_is(error
= sjme_list_alloc(inPool
,
40 length
, &result
, sjme_jint
, 0)) || result
== NULL
)
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
])))
51 return SJME_ERROR_NONE
;
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
)
67 sjme_jint length
, i
, actual
;
71 if (inPool
== NULL
|| outList
== NULL
|| inputStream
== NULL
)
72 return SJME_ERROR_NULL_ARGUMENTS
;
76 if (sjme_error_is(error
= sjme_stream_inputReadValueJI(
77 inputStream
, &length
)) ||
79 return sjme_error_default(error
);
83 return SJME_ERROR_INDEX_OUT_OF_BOUNDS
;
85 /* Allocate target values for later list creation. */
86 values
= sjme_alloca(sizeof(*values
) * length
);
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. */
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);
108 if (sjme_error_is(error
= sjme_stream_inputReadFully(
109 inputStream
, &actual
,
111 return sjme_error_default(error
);
114 if (actual
!= utfLen
)
115 return SJME_ERROR_END_OF_FILE
;
119 return sjme_list_flattenArgCV(inPool
, outList
,
120 length
, (sjme_lpcstr
*)values
);