foreach.c 929 B

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