source: main/trunk/greenstone2/build-src/packages/isis-gdl/IsisUtil.cpp@ 26670

Last change on this file since 26670 was 6127, checked in by mdewsnip, 20 years ago

IsisGdl package for reading CDS/ISIS databases. Provided by Jean-Claude Dauphin at UNESCO, and modified slightly to compile and run under Linux.

  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 KB
Line 
1/**********************************************************************
2 *
3 * IsisUtil.cpp
4 * Copyright (C) 2003 UNESCO
5 *
6 * A component of the Greenstone digital library software
7 * from the New Zealand Digital Library Project at the
8 * University of Waikato, New Zealand.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 *
24 *********************************************************************/
25
26#include "stdafx.h"
27#include "IsisUtil.h"
28#include <algorithm>
29
30
31//---------------------------------------------------------------------------------
32// bool RepFieldSep(TCHAR c)
33//
34// This function is a predicate that takes a TCHAR and returns a value that indicates
35// if that TCHAR is a repeteable field separator.
36//----------------------------------------------------------------------------------
37bool RepFieldSep(char c)
38{
39 return (c == SYSRSEP);
40}
41
42//--------------------------------------------------------------------------------
43// vector<string> SplitOccurences(const cstring& s)
44//
45// This function takes as input a string which contains a field and return a
46// vector<string> which will contain an entry for each field occurence.
47//--------------------------------------------------------------------------------
48std::vector<cstring> SplitOccurences(const cstring& s)
49{
50 typedef cstring::const_iterator iter;
51 std::vector<cstring> ret;
52
53 iter i = s.begin();
54 //TRACE("\ni=%s",i);
55 while (i != s.end())
56 {
57 // find end of occurence
58 iter j = std::find_if(i,s.end(),RepFieldSep);
59
60 // copy the characters in [i, j]
61 ret.push_back(cstring(i, j));
62 //TRACE("\ni=%s j=%s string(i,j)=%s", i, j, string(i,j).c_str());
63
64 i = j;
65 if (j != s.end())
66 i++;
67 }
68 return ret;
69}
70
71
72bool WSubFieldSep(WCHAR c)
73{
74 return (c == SUBFIELD_SEP);
75}
76
77//--------------------------------------------------------------------------------
78// vector<cstring> SplitSubfields(const cstring& s, TCHAR sc)
79//
80// This function takes as input a string which contains a field and return a
81// vector<string> which will contain an entry for each subfield occurrence.
82//--------------------------------------------------------------------------------
83std::vector<cstring> SplitSubfields(const cstring& s, TCHAR sc/*=0*/)
84{
85 typedef cstring::const_iterator iter;
86 std::vector<cstring> ret;
87
88 iter i= s.begin();
89 iter j;
90 while (i < s.end())
91 {
92 // Look for subfield separator (^)
93 iter k = std::find_if(i ,s.end(),WSubFieldSep);
94 if (k == s.end())
95 {
96 j = s.end(); // separator not found
97 }
98 else
99 {
100 // Special case when field is terminated by "^x" or "^"
101 if (k==s.end()-1 || k==s.end()-2)
102 break;
103 i = k + 2; // separator found, skip next char
104 assert(i<s.end());
105 j = std::find_if(i ,s.end() ,WSubFieldSep);
106 }
107 ret.push_back(cstring(i, j));
108 i = j;
109 }
110 return ret;
111}
112
113
114//--------------------------------------------------------------------------------
115// vector<string> SplitOccurences(const awstring& s)
116//
117// This function takes as input a string which contains a field and return a
118// vector<string> which will contain an entry for each field occurence.
119//--------------------------------------------------------------------------------
120// std::vector<awstring> SplitOccurences(const awstring& s)
121// {
122// typedef awstring::const_iterator iter;
123// std::vector<awstring> ret;
124
125// iter i = s.begin();
126// //TRACE("\ni=%s",i);
127// while (i != s.end())
128// {
129// // find end of occurence
130// iter j = std::find_if(i,s.end(),RepFieldSep);
131
132// // copy the characters in [i, j]
133// ret.push_back(awstring(i, j));
134// //TRACE("\ni=%s j=%s string(i,j)=%s", i, j, string(i,j).c_str());
135
136// i = j;
137// if (j != s.end())
138// i++;
139// }
140// return ret;
141// }
142
143
144bool SubFieldSep(TCHAR c)
145{
146 return (c == SUBFIELD_SEP);
147}
148
149//--------------------------------------------------------------------------------
150// vector<awstring> SplitSubfields(const awstring& s, TCHAR sc)
151//
152// This function takes as input a string which contains a field and return a
153// vector<string> which will contain an entry for each subfield occurrence.
154//--------------------------------------------------------------------------------
155// std::vector<awstring> SplitSubfields(const awstring& s, TCHAR sc/*=0*/)
156// {
157// typedef awstring::const_iterator iter;
158// std::vector<awstring> ret;
159
160// iter i = s.begin();
161// //TRACE("\ni=%s",i);
162// bool found=false;
163// while (i != s.end())
164// {
165// // find index of subfield separator (^)
166// iter j = std::find_if(i,s.end(),SubFieldSep);
167// if (found && j<s.end()-1)
168// {
169// // copy the characters in [i, j]
170// ret.push_back(awstring(i+1, j));
171// }
172// if (sc==0 || s.at(j-s.begin()+1)==sc)
173// found = true;
174// else
175// found = false;
176
177
178// i = j+1;
179// if (j != s.end())
180// i++;
181// }
182// return ret;
183// }
Note: See TracBrowser for help on using the repository browser.