array.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #ifndef array_h_included
  12. #define array_h_included
  13. /**
  14. * @file
  15. * Functions to dynamically allocate arrays (header).
  16. */
  17. #include "portab.h"
  18. typedef struct {
  19. char * mem;
  20. size_t allocated;
  21. size_t used;
  22. } array;
  23. /* allocated: mem != NULL, used >= 0 && used <= allocated, allocated > 0
  24. unallocated: mem == NULL, allocated == 0, used == 0 */
  25. #define array_unallocated(x) (array_bytes(x)==0)
  26. #define INIT_ARRAY { NULL, 0, 0 }
  27. /* set all variables in a to 0 */
  28. extern void array_init PARAMS((array *a));
  29. /* allocates space for at least nmemb+1 elements of size bytes each.
  30. return pointer to elem at pos, or NULL if realloc() fails */
  31. extern void * array_alloc PARAMS((array *a, size_t size, size_t pos));
  32. /* returns the number of initialized BYTES in a. */
  33. #define array_bytes(array) ( (array)->used )
  34. /* returns the number of initialized ELEMS in a. */
  35. extern size_t array_length PARAMS((const array* const a, size_t elemsize));
  36. /* _copy functions: copy src to dest.
  37. return true if OK, else false (e. g. realloc failure, invalid src/dest
  38. array, ...). In that case dest is left unchanged. */
  39. /* copy array src to dest */
  40. extern bool array_copy PARAMS((array* dest, const array* const src));
  41. /* copy len bytes from src to array dest. */
  42. extern bool array_copyb PARAMS((array* dest, const char* src, size_t len));
  43. /* copy string to dest */
  44. extern bool array_copys PARAMS((array* dest, const char* src));
  45. /* _cat functions: append src to dest.
  46. return true if OK, else false (e. g. realloc failure, invalid src/dest
  47. array, ...). In that case dest is left unchanged. */
  48. /* append len bytes from src to array dest. */
  49. extern bool array_catb PARAMS((array* dest, const char* src, size_t len));
  50. /* append string to dest */
  51. extern bool array_cats PARAMS((array* dest, const char* src));
  52. /* append NUL byte to dest */
  53. extern bool array_cat0 PARAMS((array* dest));
  54. /* append NUL byte to dest, but do not count null byte */
  55. extern bool array_cat0_temporary PARAMS((array* dest));
  56. /* append contents of array src to array dest. */
  57. extern bool array_cat PARAMS((array* dest, const array* const src));
  58. /* return pointer to element at pos.
  59. return NULL if the array is unallocated or if pos is larger than the number
  60. of elements stored int the array. */
  61. extern void* array_get PARAMS((array* a, size_t membersize, size_t pos));
  62. /* free the contents of this array. */
  63. extern void array_free PARAMS((array* a));
  64. /* overwrite array with zeros before free */
  65. extern void array_free_wipe PARAMS((array* a));
  66. /* return pointer to first element in this array */
  67. extern void* array_start PARAMS((const array* const a));
  68. /* reset this array (the memory is not free'd */
  69. extern void array_trunc PARAMS((array* a));
  70. /* set number of used elements in this array to len */
  71. extern void array_truncate PARAMS((array* a, size_t membersize, size_t len));
  72. /* move elements starting at pos to beginning of array */
  73. extern void array_moveleft PARAMS((array* a, size_t membersize, size_t pos));
  74. #endif
  75. /* -eof- */