@@ -236,6 +236,65 @@ BOOLEAN_T esock_get_bool_from_map(ErlNifEnv* env,
236236}
237237
238238
239+ /* *** esock_get_string_from_map ***
240+ *
241+ * Simple utility function used to extract a string value from a map.
242+ * If the map does not contain the map value, the string is set to NULL
243+ * and TRUE is returned (its acceptible to not provide a value).
244+ * If it fails to extract the value (for whatever reason) the string
245+ * is set to NULL and FALSE is returned.
246+ */
247+
248+ extern
249+ BOOLEAN_T esock_get_string_from_map (ErlNifEnv * env ,
250+ ERL_NIF_TERM map ,
251+ ERL_NIF_TERM key ,
252+ ErlNifCharEncoding encoding ,
253+ char * * str )
254+ {
255+ ERL_NIF_TERM eval ;
256+ unsigned int len ;
257+ char * buf ;
258+ int written ;
259+
260+ /* Try extract the string in erlang form from the map */
261+ if (!GET_MAP_VAL (env , map , key , & eval )) {
262+ * str = NULL ;
263+ return TRUE;
264+ }
265+
266+ /* Check if its a list */
267+ if (!enif_is_list (env , eval )) {
268+ * str = NULL ;
269+ return FALSE;
270+ }
271+
272+ /* Get the string length */
273+ if (!enif_get_string_length (env , eval , & len , encoding )) {
274+ * str = NULL ;
275+ return FALSE;
276+ }
277+
278+ /* Allocate the string */
279+ if ((buf = MALLOC (len + 1 )) == NULL ) {
280+ * str = NULL ;
281+ return FALSE;
282+ }
283+
284+ /* And finally copy it out */
285+ written = enif_get_string (env , eval , buf , len + 1 , encoding );
286+ if (written == (len + 1 )) {
287+ * str = buf ;
288+ return TRUE;
289+ } else {
290+ FREE ( buf );
291+ * str = NULL ;
292+ return FALSE;
293+ }
294+
295+ }
296+
297+
239298/* +++ esock_encode_iov +++
240299 *
241300 * Encode an IO Vector. In erlang we represented this as a list of binaries.
0 commit comments