foreach.c 960 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #define NOBUILD_IMPLEMENTATION
  2. #include "../nobuild.h"
  3. void foreach_vargs(int ignore, ...)
  4. {
  5. va_list args;
  6. FOREACH_VARGS(ignore, arg, args, {
  7. printf(" %s\n", arg);
  8. });
  9. }
  10. void foreach_array(void)
  11. {
  12. static const char *array[] = {
  13. "foo", "bar", "baz"
  14. };
  15. FOREACH_ARRAY(const char *, item, array, {
  16. printf(" %s\n", item);
  17. });
  18. }
  19. void foreach_file_in_dir(const char *dir_path)
  20. {
  21. FOREACH_FILE_IN_DIR(file, dir_path, {
  22. printf(" %s\n", file);
  23. });
  24. }
  25. #define DEMO(expr) \
  26. do { \
  27. printf(#expr"\n"); \
  28. expr; \
  29. } while(0)
  30. int main(int argc, char *argv[])
  31. {
  32. printf("=== Foreach Macro Example ===\n");
  33. DEMO(foreach_vargs(69, "foo", "bar", "baz", NULL));
  34. DEMO(foreach_array());
  35. DEMO(foreach_file_in_dir("."));
  36. return 0;
  37. }