|
|
1.1 ! root 1: /* ! 2: Hatari - str.c ! 3: ! 4: This file is distributed under the GNU Public License, version 2 or at ! 5: your option any later version. Read the file gpl.txt for details. ! 6: ! 7: String functions. ! 8: */ ! 9: const char Str_fileid[] = "Hatari str.c : " __DATE__ " " __TIME__; ! 10: ! 11: #include <stdio.h> ! 12: #include <ctype.h> ! 13: #include <stdbool.h> ! 14: #include <stdlib.h> ! 15: #include <SDL_types.h> ! 16: #include "configuration.h" ! 17: #include "str.h" ! 18: ! 19: ! 20: /** ! 21: * Remove whitespace from beginning and end of a string. ! 22: * Returns the trimmed string (string content is moved ! 23: * so that it still starts from the same address) ! 24: */ ! 25: char *Str_Trim(char *buffer) ! 26: { ! 27: int i, linelen; ! 28: ! 29: if (buffer == NULL) ! 30: return NULL; ! 31: ! 32: linelen = strlen(buffer); ! 33: ! 34: for (i = 0; i < linelen; i++) ! 35: { ! 36: if (!isspace(buffer[i])) ! 37: break; ! 38: } ! 39: ! 40: if (i > 0 && i < linelen) ! 41: { ! 42: linelen -= i; ! 43: memmove(buffer, buffer + i, linelen); ! 44: } ! 45: ! 46: for (i = linelen; i > 0; i--) ! 47: { ! 48: if (!isspace(buffer[i-1])) ! 49: break; ! 50: } ! 51: ! 52: buffer[i] = '\0'; ! 53: ! 54: return buffer; ! 55: } ! 56: ! 57: ! 58: /** ! 59: * Convert a string to uppercase in place. ! 60: */ ! 61: char *Str_ToUpper(char *pString) ! 62: { ! 63: char *str = pString; ! 64: while (*str) ! 65: { ! 66: *str = toupper(*str); ! 67: str++; ! 68: } ! 69: return pString; ! 70: } ! 71: ! 72: ! 73: /** ! 74: * Convert string to lowercase in place. ! 75: */ ! 76: char *Str_ToLower(char *pString) ! 77: { ! 78: char *str = pString; ! 79: while (*str) ! 80: { ! 81: *str = tolower(*str); ! 82: str++; ! 83: } ! 84: return pString; ! 85: } ! 86: ! 87: ! 88: /** ! 89: * truncate string at first unprintable char (e.g. newline). ! 90: */ ! 91: char *Str_Trunc(char *pString) ! 92: { ! 93: int i = 0; ! 94: char *str = pString; ! 95: while (str[i] != '\0') ! 96: { ! 97: if (!isprint((unsigned)str[i])) ! 98: { ! 99: str[i] = '\0'; ! 100: break; ! 101: } ! 102: i++; ! 103: } ! 104: return pString; ! 105: } ! 106: ! 107: ! 108: /** ! 109: * check if string is valid hex number. ! 110: */ ! 111: bool Str_IsHex(const char *str) ! 112: { ! 113: int i = 0; ! 114: while (str[i] != '\0' && str[i] != ' ') ! 115: { ! 116: if (!isxdigit((unsigned)str[i])) ! 117: return false; ! 118: i++; ! 119: } ! 120: return true; ! 121: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.