qapi-util.c 686 B

12345678910111213141516171819202122232425262728293031323334
  1. /*
  2. * QAPI util functions
  3. *
  4. * Authors:
  5. * Hu Tao <hutao@cn.fujitsu.com>
  6. * Peter Lieven <pl@kamp.de>
  7. *
  8. * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
  9. * See the COPYING.LIB file in the top-level directory.
  10. *
  11. */
  12. #include "qemu-common.h"
  13. #include "qapi/error.h"
  14. #include "qapi/util.h"
  15. int qapi_enum_parse(const char *lookup[], const char *buf,
  16. int max, int def, Error **errp)
  17. {
  18. int i;
  19. if (!buf) {
  20. return def;
  21. }
  22. for (i = 0; i < max; i++) {
  23. if (!strcmp(buf, lookup[i])) {
  24. return i;
  25. }
  26. }
  27. error_setg(errp, "invalid parameter value: %s", buf);
  28. return def;
  29. }