misc.c 4.87 KB
Newer Older
1 2 3 4
/*
 *  Misc functions
 *
 *  Copyright 2000 Jon Griffiths
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 21 22 23

#include "config.h"
#include "wine/port.h"

24
#include "winedump.h"
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43


/*******************************************************************
 *         str_create
 *
 * Create a single string from many substrings
 */
char *str_create(size_t num_str, ...)
{
  va_list args;
  size_t len = 1, i = 0;
  char *tmp, *t;

  va_start (args, num_str);
  for (i = 0; i < num_str; i++)
    if ((t = va_arg(args, char *)))
      len += strlen (t);
  va_end (args);

44
  if (!(tmp = malloc (len)))
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    fatal ("Out of memory");

  tmp[0] = '\0';

  va_start (args, num_str);
  for (i = 0; i < num_str; i++)
    if ((t = va_arg(args, char *)))
      strcat (tmp, t);
  va_end (args);
  return tmp;
}


/*******************************************************************
 *         str_create_num
 *
 * Create a single string from many substrings, terminating in a number
 */
char *str_create_num(size_t num_str, int num, ...)
{
  va_list args;
  size_t len = 8, i = 0;
  char *tmp, *t;

  va_start (args, num);
  for (i = 0; i < num_str; i++)
    if ((t = va_arg(args, char *)))
      len += strlen (t);
  va_end (args);

75
  if (!(tmp = malloc (len)))
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    fatal ("Out of memory");

  tmp[0] = '\0';

  va_start (args, num);
  for (i = 0; i < num_str; i++)
    if ((t = va_arg(args, char *)))
      strcat (tmp, t);
  va_end (args);
  sprintf (tmp + len - 8, "%d", num);
  return tmp;
}


/*******************************************************************
 *         str_substring
 *
 * Create a new substring from a string
 */
char *str_substring(const char *start, const char *end)
{
  char *newstr;

  assert (start && end && end > start);

101
  if (!(newstr = malloc (end - start + 1)))
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    fatal ("Out of memory");

  memcpy (newstr, start, end - start);
  newstr [end - start] = '\0';

  return newstr;
}


/*******************************************************************
 *         str_replace
 *
 * Swap two strings in another string, in place
 * Modified PD code from 'snippets'
 */
char *str_replace (char *str, const char *oldstr, const char *newstr)
{
  int oldlen, newlen;
  char *p, *q;

  if (!(p = strstr(str, oldstr)))
    return p;
  oldlen = strlen (oldstr);
  newlen = strlen (newstr);
  memmove (q = p + newlen, p + oldlen, strlen (p + oldlen) + 1);
  memcpy (p, newstr, newlen);
  return q;
}


/*******************************************************************
 *         str_match
 *
 * Locate one string in another, ignoring spaces
 */
const char *str_match (const char *str, const char *match, int *found)
{
  assert(str && match && found);

141
  while (*str == ' ') str++;
142 143 144 145
  if (!strncmp (str, match, strlen (match)))
  {
    *found = 1;
    str += strlen (match);
146
    while (*str == ' ') str++;
147 148 149 150 151 152 153 154 155 156
  }
  else
    *found = 0;
  return str;
}


/*******************************************************************
 *         str_find_set
 *
Andreas Mohr's avatar
Andreas Mohr committed
157
 * Locate the first occurrence of a set of characters in a string
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
 */
const char *str_find_set (const char *str, const char *findset)
{
  assert(str && findset);

  while (*str)
  {
    const char *p = findset;
    while (*p)
      if (*p++ == *str)
        return str;
    str++;
  }
  return NULL;
}


/*******************************************************************
 *         str_toupper
 *
 * Uppercase a string
 */
char *str_toupper (char *str)
{
  char *save = str;
  while (*str)
  {
    *str = toupper (*str);
    str++;
  }
  return save;
}


/*******************************************************************
 *         open_file
 *
 * Open a file returning only on success
 */
FILE *open_file (const char *name, const char *ext, const char *mode)
{
  char  fname[128];
  FILE *fp;

  if (((unsigned)snprintf (fname, sizeof (fname), "%s%s%s",
                 *mode == 'w' ? "./" : "", name, ext) > sizeof (fname)))
    fatal ("File name too long");

  if (VERBOSE)
    printf ("Open file %s\n", fname);

  fp = fopen (fname, mode);
  if (!fp)
211
    fatal ("Can't open file");
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
  return fp;
}


/*******************************************************************
 *         fatal
 *
 * Fatal error handling
 */
void  fatal (const char *message)
{
  if (errno)
    perror (message);
  else
    puts (message);
227
  exit(1);
228
}