1 /* ccapi/lib/ccapi_string.c */ 2 /* 3 * Copyright 2006 Massachusetts Institute of Technology. 4 * All Rights Reserved. 5 * 6 * Export of this software from the United States of America may 7 * require a specific license from the United States Government. 8 * It is the responsibility of any person or organization contemplating 9 * export to obtain such a license before exporting. 10 * 11 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and 12 * distribute this software and its documentation for any purpose and 13 * without fee is hereby granted, provided that the above copyright 14 * notice appear in all copies and that both that copyright notice and 15 * this permission notice appear in supporting documentation, and that 16 * the name of M.I.T. not be used in advertising or publicity pertaining 17 * to distribution of the software without specific, written prior 18 * permission. Furthermore if you modify this software you must label 19 * your software as modified software and not distribute it in such a 20 * fashion that it might be confused with the original M.I.T. software. 21 * M.I.T. makes no representations about the suitability of 22 * this software for any purpose. It is provided "as is" without express 23 * or implied warranty. 24 */ 25 26 #include "ccapi_string.h" 27 28 /* ------------------------------------------------------------------------ */ 29 30 cc_string_d cci_string_d_initializer = { 31 NULL, 32 NULL 33 VECTOR_FUNCTIONS_INITIALIZER }; 34 35 cc_string_f cci_string_f_initializer = { 36 ccapi_string_release 37 }; 38 39 /* ------------------------------------------------------------------------ */ 40 41 cc_int32 cci_string_new (cc_string_t *out_string, 42 char *in_cstring) 43 { 44 cc_int32 err = ccNoError; 45 cc_string_t string = NULL; 46 47 if (!out_string) { err = cci_check_error (ccErrBadParam); } 48 if (!in_cstring) { err = cci_check_error (ccErrBadParam); } 49 50 if (!err) { 51 string = malloc (sizeof (*string)); 52 if (string) { 53 *string = cci_string_d_initializer; 54 } else { 55 err = cci_check_error (ccErrNoMem); 56 } 57 } 58 59 if (!err) { 60 string->functions = malloc (sizeof (*string->functions)); 61 if (string->functions) { 62 *((cc_string_f *) string->functions) = cci_string_f_initializer; 63 } else { 64 err = cci_check_error (ccErrNoMem); 65 } 66 } 67 68 if (!err) { 69 string->data = strdup (in_cstring); 70 if (!string->data) { 71 err = cci_check_error (ccErrNoMem); 72 } 73 74 } 75 76 if (!err) { 77 *out_string = string; 78 string = NULL; /* take ownership */ 79 } 80 81 if (string) { ccapi_string_release (string); } 82 83 return cci_check_error (err); 84 } 85 86 /* ------------------------------------------------------------------------ */ 87 88 cc_int32 ccapi_string_release (cc_string_t in_string) 89 { 90 cc_int32 err = ccNoError; 91 92 if (!in_string) { err = ccErrBadParam; } 93 94 if (!err) { 95 free ((char *) in_string->data); 96 free ((char *) in_string->functions); 97 free (in_string); 98 } 99 100 return err; 101 } 102