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