SDLGameEngine

src/sgestring.c

00001 /*
00002  * Copyright (c) 2007 Heiko Irrgang
00003  *
00004  * The license and distribution terms for this file may be
00005  * found in the file COPYING in this distribution or at
00006  * http://93-interactive.com/cms/products/software/sdl-game-engine/license/
00007  */
00008 
00009 #include <sge.h>
00010 
00011 int sgeCharIsWhitespace(char c) {
00012         if (
00013                 (c==' ') ||
00014                 (c=='\t') ||
00015                 (c=='\n') ||
00016                 (c=='\v') ||
00017                 (c=='\a') ||
00018                 (c=='\f') ||
00019                 (c=='\r')
00020            ) {
00021                 return YES;
00022         }
00023         return NO;
00024 }
00025 
00026 char *sgeTrim(char *str) {
00027         char *ret=NULL;
00028         int cont=1;
00029         int i=0;
00030         int start=0;
00031         int end=0;
00032         int len=strlen(str);
00033 
00034         while ( (cont) && (i<len-1) ) {
00035                 if (!sgeCharIsWhitespace(str[i])) {
00036                         start=i;
00037                         cont=0;
00038                 }
00039                 i++;
00040         }
00041 
00042         cont=1;
00043         i=len-1;
00044         while ( (cont) && (i>=0) ) {
00045                 if (!sgeCharIsWhitespace(str[i])) {
00046                         end=i;
00047                         cont=0;
00048                 }
00049                 i--;
00050         }
00051 
00052         if (end<=start) {
00053                 sgeNew(ret, char);
00054                 return ret;
00055         }
00056 
00057         len=end-start+1;
00058         sgeMalloc(ret, char, len+1);
00059         strncpy(ret, str+start, len);
00060         return ret;
00061 }
00062 
00063 char *sgeLower(char *str) {
00064         char *ret=strdup(str);
00065         char *c=ret;
00066         for ( ; *c; ++c) *c = tolower(*c);
00067         return ret;
00068 }
00069 
00070 char *sgeUpper(char *str) {
00071         char *ret=strdup(str);
00072         char *c=ret;
00073         for ( ; *c; ++c) *c = toupper(*c);
00074         return ret;
00075 }
00076 
00077 static char *sgeConfigPartFromString(char *str, int part) {
00078         char *wc=strdup(str);
00079         char *eq=strchr(wc, '=');
00080         char *p=NULL;
00081 
00082         if (eq==NULL) {
00083                 sgeFree(wc);
00084                 return NULL;
00085         }
00086 
00087         eq[0]=0;
00088         if (part==0) {
00089                 p=sgeTrim(wc);
00090         } else {
00091                 p=sgeTrim(eq+1);
00092         }
00093         sgeFree(wc);
00094         return p;
00095 }
00096 
00097 char *sgeConfigNameFromString(char *str) {
00098         return sgeConfigPartFromString(str, 0);
00099 }
00100 
00101 char *sgeConfigValueFromString(char *str) {
00102         return sgeConfigPartFromString(str, 1);
00103 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines