misc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * Misc functions
  3. *
  4. * Copyright 2000 Jon Griffiths
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "config.h"
  21. #include "winedump.h"
  22. /*******************************************************************
  23. * str_substring
  24. *
  25. * Create a new substring from a string
  26. */
  27. char *str_substring(const char *start, const char *end)
  28. {
  29. char *newstr;
  30. assert (start && end && end > start);
  31. newstr = xmalloc (end - start + 1);
  32. memcpy (newstr, start, end - start);
  33. newstr [end - start] = '\0';
  34. return newstr;
  35. }
  36. /*******************************************************************
  37. * str_replace
  38. *
  39. * Swap two strings in another string, in place
  40. * Modified PD code from 'snippets'
  41. */
  42. char *str_replace (char *str, const char *oldstr, const char *newstr)
  43. {
  44. int oldlen, newlen;
  45. char *p, *q;
  46. if (!(p = strstr(str, oldstr)))
  47. return p;
  48. oldlen = strlen (oldstr);
  49. newlen = strlen (newstr);
  50. memmove (q = p + newlen, p + oldlen, strlen (p + oldlen) + 1);
  51. memcpy (p, newstr, newlen);
  52. return q;
  53. }
  54. /*******************************************************************
  55. * str_match
  56. *
  57. * Locate one string in another, ignoring spaces
  58. */
  59. const char *str_match (const char *str, const char *match, BOOL *found)
  60. {
  61. assert(str && match && found);
  62. while (*str == ' ') str++;
  63. if (!strncmp (str, match, strlen (match)))
  64. {
  65. *found = TRUE;
  66. str += strlen (match);
  67. while (*str == ' ') str++;
  68. }
  69. else
  70. *found = FALSE;
  71. return str;
  72. }
  73. /*******************************************************************
  74. * str_find_set
  75. *
  76. * Locate the first occurrence of a set of characters in a string
  77. */
  78. const char *str_find_set (const char *str, const char *findset)
  79. {
  80. assert(str && findset);
  81. while (*str)
  82. {
  83. const char *p = findset;
  84. while (*p)
  85. if (*p++ == *str)
  86. return str;
  87. str++;
  88. }
  89. return NULL;
  90. }
  91. /*******************************************************************
  92. * str_toupper
  93. *
  94. * Uppercase a string
  95. */
  96. char *str_toupper (char *str)
  97. {
  98. char *save = str;
  99. while (*str)
  100. {
  101. *str = toupper (*str);
  102. str++;
  103. }
  104. return save;
  105. }
  106. /*******************************************************************
  107. * open_file
  108. *
  109. * Open a file returning only on success
  110. */
  111. FILE *open_file (const char *name, const char *ext, const char *mode)
  112. {
  113. char *fname;
  114. FILE *fp;
  115. fname = strmake( "%s%s%s", *mode == 'w' ? "./" : "", name, ext);
  116. if (VERBOSE)
  117. printf ("Open file %s\n", fname);
  118. fp = fopen (fname, mode);
  119. if (!fp)
  120. fatal ("Can't open file");
  121. return fp;
  122. }
  123. /*******************************************************************
  124. * fatal
  125. *
  126. * Fatal error handling
  127. */
  128. void fatal (const char *message)
  129. {
  130. if (errno)
  131. perror (message);
  132. else
  133. puts (message);
  134. exit(1);
  135. }