|
|
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 <SDL_types.h> ! 15: #include "configuration.h" ! 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: /** ! 73: * Convert string to lowercase in place. ! 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: /** ! 88: * truncate string at first unprintable char (e.g. newline). ! 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: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.