diff options
| author | Henrik Rydberg <rydberg@euromail.se> | 2011-10-08 20:30:28 +0200 |
|---|---|---|
| committer | Henrik Rydberg <rydberg@euromail.se> | 2011-10-08 20:30:28 +0200 |
| commit | 5df79c53745fde5d6c3340a2979b1429cd5892c1 (patch) | |
| tree | 1a81af141708b826e9c61e8a04019994fcca8298 /src/mt/stringref.cc | |
Initial import of htcd system 1.0
Signed-off-by: Henrik Rydberg <rydberg@euromail.se>
Diffstat (limited to 'src/mt/stringref.cc')
| -rw-r--r-- | src/mt/stringref.cc | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/src/mt/stringref.cc b/src/mt/stringref.cc new file mode 100644 index 0000000..66b3d3e --- /dev/null +++ b/src/mt/stringref.cc | |||
| @@ -0,0 +1,126 @@ | |||
| 1 | /************************************************************************* | ||
| 2 | * | ||
| 3 | * HTCd - Copyright (C) 1998-2006 Henrik Rydberg | ||
| 4 | * | ||
| 5 | * This program is free software; you can redistribute it and/or modify | ||
| 6 | * it under the terms of the GNU General Public License as published by | ||
| 7 | * the Free Software Foundation; either version 2 of the License, or | ||
| 8 | * (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This program is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | * GNU General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU General Public License | ||
| 16 | * along with this program; if not, write to the Free Software | ||
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 18 | */ | ||
| 19 | |||
| 20 | #include <mt/stringref.h> | ||
| 21 | #include <unistd.h> | ||
| 22 | #include <fcntl.h> | ||
| 23 | |||
| 24 | extern "C" { | ||
| 25 | int cfind_(const char* s,const int& sn,const char& c); | ||
| 26 | int crfind_(const char* s,const int& sn,const char& c); | ||
| 27 | int sfind_(const char* s,const int& sn,const char* c,const int& cn); | ||
| 28 | int srfind_(const char* s,const int& sn,const char* c,const int& cn); | ||
| 29 | int sfindtab_(const char* s,const int& sn,const char* c,const int& cn,const char* tab); | ||
| 30 | int srfindtab_(const char* s,const int& sn,const char* c,const int& cn,const char* tab); | ||
| 31 | int scomptab_(const char* s,const char* c,const int& n,const char* tab); | ||
| 32 | void sconvert_(char* s,const int& sn,const char* tab); | ||
| 33 | int find_of__(const char* s,const int& sn,const char* set); | ||
| 34 | int rfind_of__(const char* s,const int& sn,const char* set); | ||
| 35 | int find_not_of__(const char* s,const int& sn,const char* set); | ||
| 36 | int rfind_not_of__(const char* s,const int& sn,const char* set); | ||
| 37 | int skip_parentesis__(const char* s,const int& sn, | ||
| 38 | const char& left,const char& right,const char& esc); | ||
| 39 | int strip_(char* s,const char* c,const int& cn,const char& esc); | ||
| 40 | } | ||
| 41 | |||
| 42 | //////////////////////////////////////////////////// | ||
| 43 | |||
| 44 | char* sref::copyto(char* buf,int max) const { | ||
| 45 | if(!dim) buf[0]=0; | ||
| 46 | else if(dim<max) ((char*)memcpy(buf,map,dim))[dim]=0; | ||
| 47 | else ((char*)memcpy(buf,map,max-1))[max-1]=0; | ||
| 48 | return buf; | ||
| 49 | } | ||
| 50 | |||
| 51 | int sref::compare(const sref& s,const charmap& tab) const { | ||
| 52 | int c; if(map==s.map) return dim-s.dim; | ||
| 53 | else return (c=scomptab_(map,s.map,dim<s.dim?dim:s.dim,tab.map))?c:dim-s.dim; | ||
| 54 | } | ||
| 55 | |||
| 56 | //////////////////////////////////////////////////// | ||
| 57 | |||
| 58 | void sref::tolower() { sconvert_(map,dim,cset_lcase.map); } | ||
| 59 | void sref::toupper() { sconvert_(map,dim,cset_ucase.map); } | ||
| 60 | void sref::tosim(const charmap& s) { sconvert_(map,dim,s.map); } | ||
| 61 | |||
| 62 | //////////////////////////////////////////////////// | ||
| 63 | |||
| 64 | sref sref::operator()(int pos,int n) const { | ||
| 65 | if(pos<0) { pos+=dim; if(pos<0) pos=0; } | ||
| 66 | if(pos>=dim) return sref(map+dim,0); | ||
| 67 | else { | ||
| 68 | if(n<0) { n+=dim+1; if(n<0) n=0; } | ||
| 69 | if(n>=dim-pos) return sref(map+pos,dim-pos); | ||
| 70 | else return sref(map+pos,n); | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | sref sref::left(int n) const { | ||
| 75 | if(n<0) { n+=dim+1; if(n<0) n=0; } | ||
| 76 | if(n>dim) return sref(map,dim); | ||
| 77 | else return sref(map,n); | ||
| 78 | } | ||
| 79 | sref sref::leftand(int n,int m) const { | ||
| 80 | if(n<0) { n+=dim+1; if(n<0) n=0; } | ||
| 81 | if((n+=m)>dim) return sref(map,dim); | ||
| 82 | else return sref(map,n); | ||
| 83 | } | ||
| 84 | sref sref::right(int n) const { | ||
| 85 | if(n<0) { n+=dim+1; if(n<0) n=0; } | ||
| 86 | if(n>dim) return sref(map+dim,0); | ||
| 87 | else return sref(map+n,dim-n); | ||
| 88 | } | ||
| 89 | sref sref::past(int n,int m) const { | ||
| 90 | if(n<0) { n+=dim+1; if(n<0) n=0; } | ||
| 91 | if((n+=m)>dim) return sref(map+dim,0); | ||
| 92 | else return sref(map+n,dim-n); | ||
| 93 | } | ||
| 94 | |||
| 95 | //////////////////////////////////////////////////////////////// | ||
| 96 | |||
| 97 | int sref::find(char c) const { return cfind_(map,dim,c); } | ||
| 98 | int sref::rfind(char c) const { return crfind_(map,dim,c); } | ||
| 99 | int sref::find(const sref& s) const { return sfind_(map,dim,s.map,s.dim); } | ||
| 100 | int sref::rfind(const sref& s) const { return srfind_(map,dim,s.map,s.dim); } | ||
| 101 | int sref::find(const sref& s,const charmap& tab) const { | ||
| 102 | return sfindtab_(map,dim,s.map,s.dim,tab.map); | ||
| 103 | } | ||
| 104 | int sref::rfind(const sref& s,const charmap& tab) const { | ||
| 105 | return srfindtab_(map,dim,s.map,s.dim,tab.map); | ||
| 106 | } | ||
| 107 | |||
| 108 | int sref::find_of(const charset& s) const { return find_of__(map,dim,s.map); } | ||
| 109 | int sref::rfind_of(const charset& s) const { return rfind_of__(map,dim,s.map); } | ||
| 110 | int sref::find_not_of(const charset& s) const { return find_not_of__(map,dim,s.map); } | ||
| 111 | int sref::rfind_not_of(const charset& s) const { return rfind_not_of__(map,dim,s.map); } | ||
| 112 | |||
| 113 | int sref::skip(const bracket& bra) const { | ||
| 114 | return skip_parentesis__(map,dim,bra.left,bra.right,bra.esc); | ||
| 115 | } | ||
| 116 | |||
| 117 | //////////////////////////////////////////////////////////////// | ||
| 118 | |||
| 119 | int sref::save(const sref& s,int prot) const { | ||
| 120 | sref_t path; s.copyto(path); | ||
| 121 | int fd=open(path,O_WRONLY|O_CREAT,prot); | ||
| 122 | if(fd<0) return -1; | ||
| 123 | write(fd,map,dim); | ||
| 124 | close(fd); | ||
| 125 | return dim; | ||
| 126 | } | ||
