legal_dbname.c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <grass/gis.h>
  2. #include <grass/dbmi.h>
  3. #include <grass/glocale.h>
  4. /* TODO: are we as restrictive here as for vector names? */
  5. /*!
  6. \fn int db_legal_tablename (char *s)
  7. \brief Check if output is legal table name
  8. \return 1 OK
  9. \return -1 if name does not start with letter A..Za..z
  10. or if name does not continue with A..Za..z0..9_@
  11. Rule: [A-Za-z][A-Za-z0-9_@]*
  12. \param name table name to be checked
  13. */
  14. int db_legal_tablename(const char *s)
  15. {
  16. char buf[GNAME_MAX];
  17. sprintf(buf, "%s", s);
  18. if (*s == '.' || *s == 0) {
  19. fprintf(stderr,
  20. _("Illegal table map name <%s>. May not contain '.' or 'NULL'.\n"),
  21. buf);
  22. return DB_FAILED;
  23. }
  24. /* file name must start with letter */
  25. if (!((*s >= 'A' && *s <= 'Z') || (*s >= 'a' && *s <= 'z'))) {
  26. fprintf(stderr,
  27. _("Illegal table map name <%s>. Must start with a letter.\n"),
  28. buf);
  29. return DB_FAILED;
  30. }
  31. for (s++; *s; s++)
  32. if (!
  33. ((*s >= 'A' && *s <= 'Z') || (*s >= 'a' && *s <= 'z') ||
  34. (*s >= '0' && *s <= '9') || *s == '_' || *s == '@')) {
  35. fprintf(stderr,
  36. _("Illegal table map name <%s>. Character <%c> not allowed.\n"),
  37. buf, *s);
  38. return DB_FAILED;
  39. }
  40. return DB_OK;
  41. }