flstring.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * "$Id: flstring.c 9573 2012-06-06 03:38:02Z fabien $"
  3. *
  4. * BSD string functions for the Fast Light Tool Kit (FLTK).
  5. *
  6. * Copyright 1998-2010 by Bill Spitzak and others.
  7. *
  8. * This library is free software. Distribution and use rights are outlined in
  9. * the file "COPYING" which should have been included with this file. If this
  10. * file is missing or damaged, see the license at:
  11. *
  12. * http://www.fltk.org/COPYING.php
  13. *
  14. * Please report all bugs and problems on the following page:
  15. *
  16. * http://www.fltk.org/str.php
  17. */
  18. #include "flstring.h"
  19. /*
  20. * 'fl_strlcat()' - Safely concatenate two strings.
  21. */
  22. size_t /* O - Length of string */
  23. fl_strlcat(char *dst, /* O - Destination string */
  24. const char *src, /* I - Source string */
  25. size_t size) { /* I - Size of destination string buffer */
  26. size_t srclen; /* Length of source string */
  27. size_t dstlen; /* Length of destination string */
  28. /*
  29. * Figure out how much room is left...
  30. */
  31. dstlen = strlen(dst);
  32. size -= dstlen + 1;
  33. if (!size) return (dstlen); /* No room, return immediately... */
  34. /*
  35. * Figure out how much room is needed...
  36. */
  37. srclen = strlen(src);
  38. /*
  39. * Copy the appropriate amount...
  40. */
  41. if (srclen > size) srclen = size;
  42. memcpy(dst + dstlen, src, srclen);
  43. dst[dstlen + srclen] = '\0';
  44. return (dstlen + srclen);
  45. }
  46. /*
  47. * 'fl_strlcpy()' - Safely copy two strings.
  48. */
  49. size_t /* O - Length of string */
  50. fl_strlcpy(char *dst, /* O - Destination string */
  51. const char *src, /* I - Source string */
  52. size_t size) { /* I - Size of destination string buffer */
  53. size_t srclen; /* Length of source string */
  54. /*
  55. * Figure out how much room is needed...
  56. */
  57. size --;
  58. srclen = strlen(src);
  59. /*
  60. * Copy the appropriate amount...
  61. */
  62. if (srclen > size) srclen = size;
  63. memcpy(dst, src, srclen);
  64. dst[srclen] = '\0';
  65. return (srclen);
  66. }
  67. #define C_RANGE(c,l,r) ( (c) >= (l) && (c) <= (r) )
  68. /**
  69. * locale independent ascii oriented case cmp
  70. * returns 0 if string successfully compare, -1 if s<t, +1 if s>t
  71. */
  72. int fl_ascii_strcasecmp(const char *s, const char *t) {
  73. if (!s || !t) return (s==t ? 0 : (!s ? -1 : +1));
  74. for(;*s && *t; s++,t++) {
  75. if (*s == *t) continue;
  76. if (*s < *t) {
  77. if ( (*s+0x20)!=*t || !C_RANGE(*s,'A','Z') ) return -1;
  78. } else { /* (*s > *t) */
  79. if ( (*s-0x20)!=*t || !C_RANGE(*s,'a','z') ) return +1;
  80. }
  81. }
  82. return (*s==*t) ? 0 : (*t ? -1 : +1);
  83. }
  84. /*
  85. * End of "$Id: flstring.c 9573 2012-06-06 03:38:02Z fabien $".
  86. */