DynamicColumn.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. htop - DynamicColumn.c
  3. (C) 2021 Sohaib Mohammed
  4. (C) 2021 htop dev team
  5. (C) 2021 Red Hat, Inc. All Rights Reserved.
  6. Released under the GNU GPLv2+, see the COPYING file
  7. in the source distribution for its full text.
  8. */
  9. #include "config.h" // IWYU pragma: keep
  10. #include "DynamicColumn.h"
  11. #include <stddef.h>
  12. #include <stdlib.h>
  13. #include "Platform.h"
  14. #include "RichString.h"
  15. #include "XUtils.h"
  16. Hashtable* DynamicColumns_new(void) {
  17. Hashtable* dynamics = Platform_dynamicColumns();
  18. if (!dynamics)
  19. dynamics = Hashtable_new(0, true);
  20. return dynamics;
  21. }
  22. void DynamicColumns_delete(Hashtable* dynamics) {
  23. if (dynamics) {
  24. Platform_dynamicColumnsDone(dynamics);
  25. Hashtable_delete(dynamics);
  26. }
  27. }
  28. const char* DynamicColumn_name(unsigned int key) {
  29. return Platform_dynamicColumnName(key);
  30. }
  31. void DynamicColumn_done(DynamicColumn* this) {
  32. free(this->heading);
  33. free(this->caption);
  34. free(this->description);
  35. }
  36. typedef struct {
  37. const char* name;
  38. const DynamicColumn* data;
  39. unsigned int key;
  40. } DynamicIterator;
  41. static void DynamicColumn_compare(ht_key_t key, void* value, void* data) {
  42. const DynamicColumn* column = (const DynamicColumn*)value;
  43. DynamicIterator* iter = (DynamicIterator*)data;
  44. if (String_eq(iter->name, column->name)) {
  45. iter->data = column;
  46. iter->key = key;
  47. }
  48. }
  49. const DynamicColumn* DynamicColumn_search(Hashtable* dynamics, const char* name, unsigned int* key) {
  50. DynamicIterator iter = { .key = 0, .data = NULL, .name = name };
  51. if (dynamics)
  52. Hashtable_foreach(dynamics, DynamicColumn_compare, &iter);
  53. if (key)
  54. *key = iter.key;
  55. return iter.data;
  56. }
  57. const DynamicColumn* DynamicColumn_lookup(Hashtable* dynamics, unsigned int key) {
  58. return (const DynamicColumn*) Hashtable_get(dynamics, key);
  59. }
  60. bool DynamicColumn_writeField(const Process* proc, RichString* str, unsigned int key) {
  61. return Platform_dynamicColumnWriteField(proc, str, key);
  62. }