You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.h 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* Copyright 2011 Pierre Ossman <ossman@cendio.se> for Cendio AB
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining
  4. * a copy of this software and associated documentation files (the
  5. * "Software"), to deal in the Software without restriction, including
  6. * without limitation the rights to use, copy, modify, merge, publish,
  7. * distribute, sublicense, and/or sell copies of the Software, and to
  8. * permit persons to whom the Software is furnished to do so, subject to
  9. * the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be
  12. * included in all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. */
  23. #ifndef __FLTK_UTIL_H__
  24. #define __FLTK_UTIL_H__
  25. #include <FL/Fl_Menu_.H>
  26. /* Escapes all @ in text as those have special meaning in labels */
  27. static inline size_t fltk_escape(const char *in, char *out, size_t maxlen)
  28. {
  29. size_t len;
  30. len = 0;
  31. while (*in != '\0') {
  32. if (*in == '@') {
  33. if (maxlen >= 3) {
  34. *out++ = '@';
  35. *out++ = '@';
  36. maxlen -= 2;
  37. }
  38. len += 2;
  39. } else {
  40. if (maxlen >= 2) {
  41. *out++ = *in;
  42. maxlen--;
  43. }
  44. len += 1;
  45. }
  46. in++;
  47. }
  48. if (maxlen)
  49. *out = '\0';
  50. return len;
  51. }
  52. /* Filter out unsafe characters for menu entries */
  53. static inline size_t fltk_menu_escape(const char *in, char *out, size_t maxlen)
  54. {
  55. size_t len;
  56. len = 0;
  57. while (*in != '\0') {
  58. if (*in == '/') {
  59. if (maxlen >= 3) {
  60. *out++ = '\\';
  61. *out++ = '/';
  62. maxlen -= 2;
  63. }
  64. len += 2;
  65. } else {
  66. if (maxlen >= 2) {
  67. *out++ = *in;
  68. maxlen--;
  69. }
  70. len += 1;
  71. }
  72. in++;
  73. }
  74. if (maxlen)
  75. *out = '\0';
  76. return len;
  77. }
  78. /* Helper to add menu entries safely */
  79. static inline void fltk_menu_add(Fl_Menu_ *menu, const char *text,
  80. int shortcut, Fl_Callback *cb,
  81. void *data = 0, int flags = 0)
  82. {
  83. char buffer[1024];
  84. if (fltk_menu_escape(text, buffer, sizeof(buffer)) >= sizeof(buffer))
  85. return;
  86. menu->add(buffer, shortcut, cb, data, flags);
  87. }
  88. #endif