graphics

experiments with opengl3.2/ogles3.3 on linux and win7
git clone http://frotz.net/git/graphics.git
Log | Files | Refs

stringutils.h (2179B)


      1 /* Copyright 2013 Brian Swetland <swetland@frotz.net>
      2  *
      3  * Licensed under the Apache License, Version 2.0 (the "License");
      4  * you may not use this file except in compliance with the License.
      5  * You may obtain a copy of the License at
      6  *
      7  *     http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software
     10  * distributed under the License is distributed on an "AS IS" BASIS,
     11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12  * See the License for the specific language governing permissions and
     13  * limitations under the License.
     14  */
     15 
     16 #ifndef _STRING_UTILS_H_
     17 #define _STRING_UTILS_H_
     18 
     19 #include <string.h>
     20 
     21 #include "types.h"
     22 
     23 class stringptr {
     24 public:
     25 	stringptr() : str(""), len(0) {}
     26 	stringptr(const char *s) : str(s), len(strlen(s)) {}
     27 	stringptr(const char *s, int _len) : str(s), len(_len) {}
     28 	int length() { return len; }
     29 	const char *cstr(void) { return str; }
     30 	operator const char *() { return str; }
     31 private:
     32 	const char *str;
     33 	int len;
     34 };
     35 
     36 template <int N>
     37 class stackstring {
     38 public:
     39 	stackstring() {
     40 		len = 0;
     41 		oops = 0;
     42 		str[0] = 0;
     43 	}
     44 	stackstring(stringptr sp) {
     45 		int l2 = sp.length();
     46 		if (l2 >= N) {
     47 			len = N - 1;
     48 			oops = 1;
     49 		} else {
     50 			len = l2;
     51 			oops = 0;
     52 		}
     53 		memcpy(str, sp.cstr(), len + 1);
     54 	}
     55 	stackstring<N>& trim(int n) {
     56 		if (len > n) {
     57 			len = n;
     58 			str[len] = 0;
     59 		}
     60 		return *this;
     61 	}
     62 	stackstring<N>& append(const char *astr, int alen) {
     63 		if ((N - len - 1) >= alen) {
     64 			memcpy(str + len, astr, alen + 1);
     65 			len += alen;
     66 		} else {
     67 			memcpy(str + len, astr, N - len - 1);
     68 			len = N - 1;
     69 			oops = 1;
     70 		}
     71 		return *this;
     72 	}
     73 	stackstring<N>& append(stringptr sp) {
     74 		return append(sp.cstr(), sp.length());
     75 	}
     76 	stackstring<N>& append(char c) {
     77 		return append(&c, 1);
     78 	}
     79 	int length(void) { return len; }
     80 	int error(void) { return oops; }
     81 	const char *cstr(void) { return str; }
     82 	operator const char *() { return str; }
     83 	operator stringptr () { return stringptr(str, len); }
     84 private:
     85 	DISALLOW_COPY_AND_ASSIGN(stackstring<N>);
     86 	int len;
     87 	int oops;
     88 	char str[N];
     89 };
     90 
     91 typedef stackstring<1024> string1024;
     92 typedef stackstring<128> string128;
     93 
     94 #endif