procfile.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_PROCFILE_H
  3. #define NETDATA_PROCFILE_H 1
  4. #include "../libnetdata.h"
  5. // ----------------------------------------------------------------------------
  6. // An array of words
  7. typedef struct {
  8. size_t len; // used entries
  9. size_t size; // capacity
  10. char *words[]; // array of pointers
  11. } pfwords;
  12. // ----------------------------------------------------------------------------
  13. // An array of lines
  14. typedef struct {
  15. size_t words; // how many words this line has
  16. size_t first; // the id of the first word of this line
  17. // in the words array
  18. } ffline;
  19. typedef struct {
  20. size_t len; // used entries
  21. size_t size; // capacity
  22. ffline lines[]; // array of lines
  23. } pflines;
  24. // ----------------------------------------------------------------------------
  25. // The procfile
  26. #define PROCFILE_FLAG_DEFAULT 0x00000000
  27. #define PROCFILE_FLAG_NO_ERROR_ON_FILE_IO 0x00000001
  28. typedef enum procfile_separator {
  29. PF_CHAR_IS_SEPARATOR,
  30. PF_CHAR_IS_NEWLINE,
  31. PF_CHAR_IS_WORD,
  32. PF_CHAR_IS_QUOTE,
  33. PF_CHAR_IS_OPEN,
  34. PF_CHAR_IS_CLOSE
  35. } PF_CHAR_TYPE;
  36. typedef struct {
  37. char filename[FILENAME_MAX + 1]; // not populated until profile_filename() is called
  38. uint32_t flags;
  39. int fd; // the file descriptor
  40. size_t len; // the bytes we have placed into data
  41. size_t size; // the bytes we have allocated for data
  42. pflines *lines;
  43. pfwords *words;
  44. PF_CHAR_TYPE separators[256];
  45. char data[]; // allocated buffer to keep file contents
  46. } procfile;
  47. // close the proc file and free all related memory
  48. extern void procfile_close(procfile *ff);
  49. // (re)read and parse the proc file
  50. extern procfile *procfile_readall(procfile *ff);
  51. // open a /proc or /sys file
  52. extern procfile *procfile_open(const char *filename, const char *separators, uint32_t flags);
  53. // re-open a file
  54. // if separators == NULL, the last separators are used
  55. extern procfile *procfile_reopen(procfile *ff, const char *filename, const char *separators, uint32_t flags);
  56. // example walk-through a procfile parsed file
  57. extern void procfile_print(procfile *ff);
  58. extern void procfile_set_quotes(procfile *ff, const char *quotes);
  59. extern void procfile_set_open_close(procfile *ff, const char *open, const char *close);
  60. extern char *procfile_filename(procfile *ff);
  61. // ----------------------------------------------------------------------------
  62. // set to the O_XXXX flags, to have procfile_open and procfile_reopen use them when opening proc files
  63. extern int procfile_open_flags;
  64. // set this to 1, to have procfile adapt its initial buffer allocation to the max allocation used so far
  65. extern int procfile_adaptive_initial_allocation;
  66. // return the number of lines present
  67. #define procfile_lines(ff) ((ff)->lines->len)
  68. // return the number of words of the Nth line
  69. #define procfile_linewords(ff, line) (((line) < procfile_lines(ff)) ? (ff)->lines->lines[(line)].words : 0)
  70. // return the Nth word of the file, or empty string
  71. #define procfile_word(ff, word) (((word) < (ff)->words->len) ? (ff)->words->words[(word)] : "")
  72. // return the first word of the Nth line, or empty string
  73. #define procfile_line(ff, line) (((line) < procfile_lines(ff)) ? procfile_word((ff), (ff)->lines->lines[(line)].first) : "")
  74. // return the Nth word of the current line
  75. #define procfile_lineword(ff, line, word) (((line) < procfile_lines(ff) && (word) < procfile_linewords((ff), (line))) ? procfile_word((ff), (ff)->lines->lines[(line)].first + (word)) : "")
  76. // Open file without logging file IO error if any
  77. #define procfile_open_no_log(filename, separators, flags) procfile_open(filename, separators, flags | PROCFILE_FLAG_NO_ERROR_ON_FILE_IO)
  78. #endif /* NETDATA_PROCFILE_H */