array.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. * Please read the file COPYING, README and AUTHORS for more information.
  7. *
  8. * libarray - dynamically allocate arrays.
  9. * Copyright (c) 2005 Florian Westphal (westphal@foo.fh-furtwangen.de)
  10. *
  11. * $Id: array.h,v 1.4 2005/08/30 13:36:32 fw Exp $
  12. */
  13. #ifndef array_h_included
  14. #define array_h_included
  15. #include "portab.h"
  16. typedef struct {
  17. char * mem;
  18. size_t allocated;
  19. size_t used;
  20. } array;
  21. /* allocated: mem != NULL, used >= 0 && used <= allocated, allocated > 0
  22. unallocated: mem == NULL, allocated == 0, used == 0 */
  23. #define array_unallocated(x) (array_bytes(x)==0)
  24. #define INIT_ARRAY { NULL, 0, 0 }
  25. /* set all variables in a to 0 */
  26. extern void array_init PARAMS((array *a));
  27. /* allocates space for at least nmemb+1 elements of size bytes each.
  28. return pointer to elem at pos, or NULL if realloc() fails */
  29. extern void * array_alloc PARAMS((array *a, size_t size, size_t pos));
  30. /* returns the number of initialized BYTES in a. */
  31. #define array_bytes(array) ( (array)->used )
  32. /* returns the number of initialized ELEMS in a. */
  33. extern size_t array_length PARAMS((const array* const a, size_t elemsize));
  34. /* _copy functions: copy src to dest.
  35. return true if OK, else false (e. g. realloc failure, invalid src/dest
  36. array, ...). In that case dest is left unchanged. */
  37. /* copy array src to dest */
  38. extern bool array_copy PARAMS((array* dest, const array* const src));
  39. /* copy len bytes from src to array dest. */
  40. extern bool array_copyb PARAMS((array* dest, const char* src, size_t len));
  41. /* copy string to dest */
  42. extern bool array_copys PARAMS((array* dest, const char* src));
  43. /* _cat functions: append src to dest.
  44. return true if OK, else false (e. g. realloc failure, invalid src/dest
  45. array, ...). In that case dest is left unchanged. */
  46. /* append len bytes from src to array dest. */
  47. extern bool array_catb PARAMS((array* dest, const char* src, size_t len));
  48. /* append string to dest */
  49. extern bool array_cats PARAMS((array* dest, const char* src));
  50. /* append NUL byte to dest */
  51. extern bool array_cat0 PARAMS((array* dest));
  52. /* append NUL byte to dest, but do not count null byte */
  53. extern bool array_cat0_temporary PARAMS((array* dest));
  54. /* append contents of array src to array dest. */
  55. extern bool array_cat PARAMS((array* dest, const array* const src));
  56. /* return pointer to element at pos.
  57. return NULL if the array is unallocated or if pos is larger than the number
  58. of elements stored int the array. */
  59. extern void* array_get PARAMS((array* a, size_t membersize, size_t pos));
  60. /* free the contents of this array. */
  61. extern void array_free PARAMS((array* a));
  62. /* overwrite array with zeroes before free */
  63. extern void array_free_wipe PARAMS((array* a));
  64. /* return pointer to first element in this array */
  65. extern void* array_start PARAMS((const array* const a));
  66. /* reset this array (the memory is not free'd */
  67. extern void array_trunc PARAMS((array* a));
  68. /* set number of used elements in this array to len */
  69. extern void array_truncate PARAMS((array* a, size_t membersize, size_t len));
  70. /* move elements starting at pos to beginning of array */
  71. extern void array_moveleft PARAMS((array* a, size_t membersize, size_t pos));
  72. #endif
  73. /* -eof- */