summaryrefslogtreecommitdiff
path: root/src/mt/stringref.cc
blob: dbae3a92989425ea3e8fc4a991ae692de2646ba5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*************************************************************************
 *
 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <mt/stringref.h>
#include <unistd.h>
#include <fcntl.h>

extern "C" {
  int cfind_(const char* s,const int& sn,const char& c);
  int crfind_(const char* s,const int& sn,const char& c);
  int sfind_(const char* s,const int& sn,const char* c,const int& cn);
  int srfind_(const char* s,const int& sn,const char* c,const int& cn);
  int sfindtab_(const char* s,const int& sn,const char* c,const int& cn,const char* tab);
  int srfindtab_(const char* s,const int& sn,const char* c,const int& cn,const char* tab);
  int scomptab_(const char* s,const char* c,const int& n,const char* tab);
  void sconvert_(char* s,const int& sn,const char* tab);
  int find_of_(const char* s,const int& sn,const char* set);
  int rfind_of_(const char* s,const int& sn,const char* set);
  int find_not_of_(const char* s,const int& sn,const char* set);
  int rfind_not_of_(const char* s,const int& sn,const char* set);
  int skip_parentesis_(const char* s,const int& sn,
		       const char& left,const char& right,const char& esc);
  int strip_(char* s,const char* c,const int& cn,const char& esc);
}

////////////////////////////////////////////////////

char* sref::copyto(char* buf,int max) const {
  if(!dim) buf[0]=0;
  else if(dim<max) ((char*)memcpy(buf,map,dim))[dim]=0;
  else ((char*)memcpy(buf,map,max-1))[max-1]=0;
  return buf;
}

int sref::compare(const sref& s,const charmap& tab) const {
  int c; if(map==s.map) return dim-s.dim;
  else return (c=scomptab_(map,s.map,dim<s.dim?dim:s.dim,tab.map))?c:dim-s.dim;
}

////////////////////////////////////////////////////

void sref::tolower() { sconvert_(map,dim,cset_lcase.map); }
void sref::toupper() { sconvert_(map,dim,cset_ucase.map); }
void sref::tosim(const charmap& s) { sconvert_(map,dim,s.map); }

////////////////////////////////////////////////////

sref sref::operator()(int pos,int n) const {
  if(pos<0) { pos+=dim; if(pos<0) pos=0; }
  if(pos>=dim) return sref(map+dim,0);
  else {
    if(n<0) { n+=dim+1; if(n<0) n=0; }
    if(n>=dim-pos) return sref(map+pos,dim-pos);
    else return sref(map+pos,n);
  }
}

sref sref::left(int n) const {
  if(n<0) { n+=dim+1; if(n<0) n=0; }
  if(n>dim) return sref(map,dim);
  else return sref(map,n);
}
sref sref::leftand(int n,int m) const {
  if(n<0) { n+=dim+1; if(n<0) n=0; }
  if((n+=m)>dim) return sref(map,dim);
  else return sref(map,n);
}
sref sref::right(int n) const {
  if(n<0) { n+=dim+1; if(n<0) n=0; }
  if(n>dim) return sref(map+dim,0);
  else return sref(map+n,dim-n);
}
sref sref::past(int n,int m) const {
  if(n<0) { n+=dim+1; if(n<0) n=0; }
  if((n+=m)>dim) return sref(map+dim,0);
  else return sref(map+n,dim-n);
}

////////////////////////////////////////////////////////////////

int sref::find(char c) const { return cfind_(map,dim,c); }
int sref::rfind(char c) const { return crfind_(map,dim,c); }
int sref::find(const sref& s) const { return sfind_(map,dim,s.map,s.dim); }
int sref::rfind(const sref& s) const { return srfind_(map,dim,s.map,s.dim); }
int sref::find(const sref& s,const charmap& tab) const {
  return sfindtab_(map,dim,s.map,s.dim,tab.map);
}
int sref::rfind(const sref& s,const charmap& tab) const {
  return srfindtab_(map,dim,s.map,s.dim,tab.map);
}

int sref::find_of(const charset& s) const { return find_of_(map,dim,s.map); }
int sref::rfind_of(const charset& s) const { return rfind_of_(map,dim,s.map); }
int sref::find_not_of(const charset& s) const { return find_not_of_(map,dim,s.map); }
int sref::rfind_not_of(const charset& s) const { return rfind_not_of_(map,dim,s.map); }
  
int sref::skip(const bracket& bra) const {
  return skip_parentesis_(map,dim,bra.left,bra.right,bra.esc);
}

////////////////////////////////////////////////////////////////

int sref::save(const sref& s,int prot) const {
  sref_t path; s.copyto(path);
  int fd=open(path,O_WRONLY|O_CREAT,prot);
  if(fd<0) return -1;
  write(fd,map,dim);
  close(fd);
  return dim;
}