|
|
1.1 root 1: /*
2: Hatari - str.c
3:
1.1.1.6 ! root 4: This file is distributed under the GNU General Public License, version 2
! 5: or at your option any later version. Read the file gpl.txt for details.
1.1 root 6:
7: String functions.
8: */
1.1.1.3 root 9: const char Str_fileid[] = "Hatari str.c : " __DATE__ " " __TIME__;
1.1 root 10:
1.1.1.3 root 11: #include <stdio.h>
1.1 root 12: #include <ctype.h>
13: #include <stdbool.h>
1.1.1.3 root 14: #include <SDL_types.h>
15: #include "configuration.h"
1.1 root 16: #include "str.h"
17:
18:
19: /**
20: * Remove whitespace from beginning and end of a string.
21: * Returns the trimmed string (string content is moved
22: * so that it still starts from the same address)
23: */
24: char *Str_Trim(char *buffer)
25: {
26: int i, linelen;
27:
28: if (buffer == NULL)
29: return NULL;
30:
31: linelen = strlen(buffer);
32:
33: for (i = 0; i < linelen; i++)
34: {
35: if (!isspace(buffer[i]))
36: break;
37: }
38:
39: if (i > 0 && i < linelen)
40: {
41: linelen -= i;
42: memmove(buffer, buffer + i, linelen);
43: }
44:
45: for (i = linelen; i > 0; i--)
46: {
47: if (!isspace(buffer[i-1]))
48: break;
49: }
50:
51: buffer[i] = '\0';
52:
53: return buffer;
54: }
55:
56:
57: /**
58: * Convert a string to uppercase in place.
59: */
60: char *Str_ToUpper(char *pString)
61: {
62: char *str = pString;
63: while (*str)
64: {
65: *str = toupper(*str);
66: str++;
67: }
68: return pString;
69: }
70:
71:
72: /**
1.1.1.4 root 73: * Convert string to lowercase in place.
1.1 root 74: */
75: char *Str_ToLower(char *pString)
76: {
77: char *str = pString;
78: while (*str)
79: {
80: *str = tolower(*str);
81: str++;
82: }
83: return pString;
84: }
85:
86:
87: /**
1.1.1.4 root 88: * truncate string at first unprintable char (e.g. newline).
1.1 root 89: */
90: char *Str_Trunc(char *pString)
91: {
92: int i = 0;
93: char *str = pString;
94: while (str[i] != '\0')
95: {
96: if (!isprint((unsigned)str[i]))
97: {
98: str[i] = '\0';
99: break;
100: }
101: i++;
102: }
103: return pString;
104: }
105:
106:
107: /**
108: * check if string is valid hex number.
109: */
110: bool Str_IsHex(const char *str)
111: {
112: int i = 0;
113: while (str[i] != '\0' && str[i] != ' ')
114: {
115: if (!isxdigit((unsigned)str[i]))
116: return false;
117: i++;
118: }
119: return true;
120: }
1.1.1.5 root 121:
122:
123: /**
124: * Convert potentially too long host filenames to 8.3 TOS filenames
125: * by truncating extension and part before it, replacing invalid
126: * GEMDOS file name characters with INVALID_CHAR + upcasing the result.
127: *
128: * Matching them from the host file system should first try exact
129: * case-insensitive match, and then with a pattern that takes into
130: * account the conversion done in here.
131: */
132: void Str_Filename2TOSname(const char *source, char *dst)
133: {
134: char *dot, *tmp, *src;
135: int len;
136:
137: src = strdup(source); /* dup so that it can be modified */
138: len = strlen(src);
139:
140: /* does filename have an extension? */
141: dot = strrchr(src, '.');
142: if (dot)
143: {
144: /* limit extension to 3 chars */
145: if (src + len - dot > 3)
146: dot[4] = '\0';
147:
148: /* if there are extra dots, convert them */
149: for (tmp = src; tmp < dot; tmp++)
150: if (*tmp == '.')
151: *tmp = INVALID_CHAR;
152:
1.1.1.6 ! root 153: /* limit part before extension to 8 chars */
! 154: if (dot - src > 8)
! 155: memmove(src + 8, dot, strlen(dot) + 1);
1.1.1.5 root 156: }
1.1.1.6 ! root 157: else if (len > 8)
! 158: src[8] = '\0';
! 159:
! 160: strcpy(dst, src);
1.1.1.5 root 161: free(src);
162:
1.1.1.6 ! root 163: /* upcase and replace rest of invalid characters */
1.1.1.5 root 164: for (tmp = dst; *tmp; tmp++)
165: {
166: if (*tmp < 33 || *tmp > 126)
167: *tmp = INVALID_CHAR;
168: else
169: {
170: switch (*tmp)
171: {
172: case '*':
173: case '/':
174: case ':':
175: case '?':
176: case '\\':
177: case '{':
178: case '}':
179: *tmp = INVALID_CHAR;
1.1.1.6 ! root 180: break;
! 181: default:
! 182: *tmp = toupper(*tmp);
1.1.1.5 root 183: }
184: }
185: }
186: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.