string.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* Copyright (C) 2002-2015 Free Software Foundation, Inc.
  2. Contributed by Paul Brook
  3. This file is part of the GNU Fortran 95 runtime library (libgfortran).
  4. Libgfortran is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. Libgfortran is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. Under Section 7 of GPL version 3, you are granted additional
  13. permissions described in the GCC Runtime Library Exception, version
  14. 3.1, as published by the Free Software Foundation.
  15. You should have received a copy of the GNU General Public License and
  16. a copy of the GCC Runtime Library Exception along with this program;
  17. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  18. <http://www.gnu.org/licenses/>. */
  19. #include "libgfortran.h"
  20. #include <string.h>
  21. #include <stdlib.h>
  22. /* Given a fortran string, return its length exclusive of the trailing
  23. spaces. */
  24. gfc_charlen_type
  25. fstrlen (const char *string, gfc_charlen_type len)
  26. {
  27. for (; len > 0; len--)
  28. if (string[len-1] != ' ')
  29. break;
  30. return len;
  31. }
  32. /* Copy a Fortran string (not null-terminated, hence length arguments
  33. for both source and destination strings. Returns the non-padded
  34. length of the destination. */
  35. gfc_charlen_type
  36. fstrcpy (char *dest, gfc_charlen_type destlen,
  37. const char *src, gfc_charlen_type srclen)
  38. {
  39. if (srclen >= destlen)
  40. {
  41. /* This will truncate if too long. */
  42. memcpy (dest, src, destlen);
  43. return destlen;
  44. }
  45. else
  46. {
  47. memcpy (dest, src, srclen);
  48. /* Pad with spaces. */
  49. memset (&dest[srclen], ' ', destlen - srclen);
  50. return srclen;
  51. }
  52. }
  53. /* Copy a null-terminated C string to a non-null-terminated Fortran
  54. string. Returns the non-padded length of the destination string. */
  55. gfc_charlen_type
  56. cf_strcpy (char *dest, gfc_charlen_type dest_len, const char *src)
  57. {
  58. size_t src_len;
  59. src_len = strlen (src);
  60. if (src_len >= (size_t) dest_len)
  61. {
  62. /* This will truncate if too long. */
  63. memcpy (dest, src, dest_len);
  64. return dest_len;
  65. }
  66. else
  67. {
  68. memcpy (dest, src, src_len);
  69. /* Pad with spaces. */
  70. memset (&dest[src_len], ' ', dest_len - src_len);
  71. return src_len;
  72. }
  73. }
  74. #ifndef HAVE_STRNLEN
  75. static size_t
  76. strnlen (const char *s, size_t maxlen)
  77. {
  78. for (size_t ii = 0; ii < maxlen; ii++)
  79. {
  80. if (s[ii] == '\0')
  81. return ii;
  82. }
  83. return maxlen;
  84. }
  85. #endif
  86. #ifndef HAVE_STRNDUP
  87. static char *
  88. strndup (const char *s, size_t n)
  89. {
  90. size_t len = strnlen (s, n);
  91. char *p = malloc (len + 1);
  92. if (!p)
  93. return NULL;
  94. memcpy (p, s, len);
  95. p[len] = '\0';
  96. return p;
  97. }
  98. #endif
  99. /* Duplicate a non-null-terminated Fortran string to a malloced
  100. null-terminated C string. */
  101. char *
  102. fc_strdup (const char *src, gfc_charlen_type src_len)
  103. {
  104. gfc_charlen_type n = fstrlen (src, src_len);
  105. char *p = strndup (src, n);
  106. if (!p)
  107. os_error ("Memory allocation failed in fc_strdup");
  108. return p;
  109. }
  110. /* Duplicate a non-null-terminated Fortran string to a malloced
  111. null-terminated C string, without getting rid of trailing
  112. blanks. */
  113. char *
  114. fc_strdup_notrim (const char *src, gfc_charlen_type src_len)
  115. {
  116. char *p = strndup (src, src_len);
  117. if (!p)
  118. os_error ("Memory allocation failed in fc_strdup");
  119. return p;
  120. }
  121. /* Given a fortran string and an array of st_option structures, search through
  122. the array to find a match. If the option is not found, we generate an error
  123. if no default is provided. */
  124. int
  125. find_option (st_parameter_common *cmp, const char *s1, gfc_charlen_type s1_len,
  126. const st_option * opts, const char *error_message)
  127. {
  128. /* Strip trailing blanks from the Fortran string. */
  129. size_t len = (size_t) fstrlen (s1, s1_len);
  130. for (; opts->name; opts++)
  131. if (len == strlen(opts->name) && strncasecmp (s1, opts->name, len) == 0)
  132. return opts->value;
  133. generate_error (cmp, LIBERROR_BAD_OPTION, error_message);
  134. return -1;
  135. }