c_util.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
  2. * This file is part of clib library
  3. * Copyright (C) 2011 Avinash Dongre ( dongre.avinash@gmail.com )
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a copy
  6. * of this software and associated documentation files (the "Software"), to deal
  7. * in the Software without restriction, including without limitation the rights
  8. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. * copies of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be included in
  13. * all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. * THE SOFTWARE.
  22. ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **/
  23. #include "c_lib.h"
  24. #include <string.h>
  25. #include <stdlib.h>
  26. void
  27. clib_copy( void *destination, void *source, size_t size ) {
  28. memcpy ( (char*)destination, source, size);
  29. }
  30. void
  31. clib_get ( void *destination, void *source, size_t size) {
  32. memcpy ( destination, (char*)source, size);
  33. }
  34. struct clib_object*
  35. new_clib_object(void *inObject, size_t obj_size) {
  36. struct clib_object* tmp = (struct clib_object*)malloc(sizeof(struct clib_object));
  37. if ( ! tmp )
  38. return (struct clib_object*)0;
  39. tmp->size = obj_size;
  40. tmp->raw_data = (void*)malloc(obj_size);
  41. if ( !tmp->raw_data ) {
  42. free ( tmp );
  43. return (struct clib_object*)0;
  44. }
  45. memcpy ( tmp->raw_data, inObject, obj_size);
  46. return tmp;
  47. }
  48. clib_error
  49. get_raw_clib_object ( struct clib_object *inObject, void**elem) {
  50. *elem = (void*)malloc(inObject->size);
  51. if ( ! *elem )
  52. return CLIB_ELEMENT_RETURN_ERROR;
  53. memcpy ( *elem, inObject->raw_data, inObject->size );
  54. return CLIB_ERROR_SUCCESS;
  55. }
  56. void
  57. replace_raw_clib_object(struct clib_object *current_object,void *elem, size_t elem_size) {
  58. free (current_object->raw_data);
  59. current_object->raw_data = (void*)malloc(elem_size);
  60. memcpy ( current_object->raw_data, elem, elem_size);
  61. }
  62. void
  63. delete_clib_object ( struct clib_object *inObject ) {
  64. if (inObject) {
  65. free (inObject->raw_data);
  66. free (inObject);
  67. }
  68. }
  69. char*
  70. clib_strdup ( char *ptr ) {
  71. #ifdef WIN32
  72. return _strdup (ptr);
  73. #else
  74. return strdup (ptr);
  75. #endif
  76. }