string.c 4.31 KB
Newer Older
1 2 3 4
/*
 * NTDLL string functions
 *
 * Copyright 2000 Alexandre Julliard
5
 * Copyright 2000 Jon Griffiths
6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * 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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20 21 22 23 24 25 26 27 28 29
 */

#include "config.h"

#include <ctype.h>
#include <string.h>

#include "windef.h"

/*********************************************************************
30
 *                  _memicmp   (NTDLL.@)
31
 */
32
INT __cdecl NTDLL__memicmp( LPCSTR s1, LPCSTR s2, DWORD len )
33 34 35 36 37 38 39 40 41 42 43 44
{
    int ret = 0;
    while (len--)
    {
        if ((ret = tolower(*s1) - tolower(*s2))) break;
        s1++;
        s2++;
    }
    return ret;
}

/*********************************************************************
45
 *                  _strupr   (NTDLL.@)
46 47 48 49 50 51 52 53 54
 */
LPSTR __cdecl _strupr( LPSTR str )
{
    LPSTR ret = str;
    for ( ; *str; str++) *str = toupper(*str);
    return ret;
}

/*********************************************************************
55
 *                  _strlwr   (NTDLL.@)
56
 *
57
 * convert a string in place to lowercase
58 59 60 61 62 63 64 65 66 67
 */
LPSTR __cdecl _strlwr( LPSTR str )
{
    LPSTR ret = str;
    for ( ; *str; str++) *str = tolower(*str);
    return ret;
}


/*********************************************************************
68
 *                  _ultoa   (NTDLL.@)
69 70 71
 */
LPSTR  __cdecl _ultoa( unsigned long x, LPSTR buf, INT radix )
{
72
    char *p, buffer[8*sizeof(unsigned long) + 1];  /* assume 8-bit chars */
73 74 75 76 77 78 79 80 81

    p = buffer + sizeof(buffer);
    *--p = 0;
    do
    {
        int rem = x % radix;
        *--p = (rem <= 9) ? rem + '0' : rem + 'a' - 10;
        x /= radix;
    } while (x);
Johann Messner's avatar
Johann Messner committed
82
    strcpy( buf, p );
83 84 85 86 87
    return buf;
}


/*********************************************************************
88
 *                  _ltoa   (NTDLL.@)
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
 */
LPSTR  __cdecl _ltoa( long x, LPSTR buf, INT radix )
{
    LPSTR p = buf;
    if (x < 0)
    {
        *p++ = '-';
        x = -x;
    }
    _ultoa( x, p, radix );
    return buf;
}


/*********************************************************************
104
 *                  _itoa           (NTDLL.@)
105 106 107 108 109
 */
LPSTR  __cdecl _itoa( int x, LPSTR buf, INT radix )
{
    return _ltoa( x, buf, radix );
}
110 111 112


/*********************************************************************
113
 *		_splitpath (NTDLL.@)
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 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
 */
void __cdecl _splitpath(const char* inpath, char * drv, char * dir,
                        char* fname, char * ext )
{
  /* Modified PD code from 'snippets' collection. */
  char ch, *ptr, *p;
  char pathbuff[MAX_PATH], *path=pathbuff;

  strcpy(pathbuff, inpath);

  /* convert slashes to backslashes for searching */
  for (ptr = (char*)path; *ptr; ++ptr)
    if ('/' == *ptr)
      *ptr = '\\';

  /* look for drive spec */
  if ('\0' != (ptr = strchr(path, ':')))
  {
    ++ptr;
    if (drv)
    {
      strncpy(drv, path, ptr - path);
      drv[ptr - path] = '\0';
    }
    path = ptr;
  }
  else if (drv)
    *drv = '\0';

  /* find rightmost backslash or leftmost colon */
  if (NULL == (ptr = strrchr(path, '\\')))
    ptr = (strchr(path, ':'));

  if (!ptr)
  {
    ptr = (char *)path; /* no path */
    if (dir)
      *dir = '\0';
  }
  else
  {
    ++ptr; /* skip the delimiter */
    if (dir)
    {
      ch = *ptr;
      *ptr = '\0';
      strcpy(dir, path);
      *ptr = ch;
    }
  }

  if (NULL == (p = strrchr(ptr, '.')))
  {
    if (fname)
      strcpy(fname, ptr);
    if (ext)
      *ext = '\0';
  }
  else
  {
    *p = '\0';
    if (fname)
      strcpy(fname, ptr);
    *p = '.';
    if (ext)
      strcpy(ext, p);
  }

  /* Fix pathological case - Win returns ':' as part of the
   * directory when no drive letter is given.
   */
  if (drv && drv[0] == ':')
  {
    *drv = '\0';
    if (dir)
    {
      pathbuff[0] = ':';
      pathbuff[1] = '\0';
      strcat(pathbuff,dir);
      strcpy(dir,pathbuff);
    }
  }
}