Garfield++ 3.0
A toolkit for the detailed simulation of particle detectors based on ionisation measurement in gases and semiconductors
Loading...
Searching...
No Matches
findmark.h
Go to the documentation of this file.
1#ifndef FINDMARK_H
2#define FINDMARK_H
3/*
4The functions of this family are for finding wanted sequences of symbols
5in strings or input streams. Such functions are often needed
6at treating of any data for finding right
7position from which some meaningful data begin at stream.
8
9Copyright (c) 2000 I. B. Smirnov
10
11Permission to use, copy, modify, distribute and sell this file for any purpose
12is hereby granted without fee, provided that the above copyright notice,
13this permission notice, and notices about any modifications of the original
14text appear in all copies and in supporting documentation.
15The file is provided "as is" without express or implied warranty.
16*/
17
18#include <cstring>
20
21namespace Heed {
22
23// The function findmark finds string s in stream file
24// and returns 1 at current position at the next symbol.
25// Finding no string it returns 0.
26int findmark(std::istream& file, const char* s);
27
28// Returns the same, also finds the length of string, the index of its
29// beginning and the index of the next symbol after string.
30// Indexes are count from current position in stream at which the function
31// was called.
32// Previously this function was findmark_uca from unsigned char alternative.
33// Note that dispite of T the internally this class is working with char.
34// The string ws is copied to string char before the work.
35// T is any class with index operation.
36template <class T>
37int findmark_a(std::istream& file, T ws, long qws, long& nbeg, long& nnext);
38
39// The same but returns also the previous symbol or '\0' for the string
40// at the beginning of the stream.
41// T is any class with index operation.
42template <class T>
43int findmark_b(std::istream& file, T ws, long qws, long& nbeg, long& nnext,
44 char& prev);
45
46template <class T>
47int findmark_a(std::istream& file, T ws, long qws, long& nbeg, long& nnext) {
48 mfunname("int findmark_a(...)");
49 check_econd11(qws, < 1, mcerr);
50 // check_econd12(qws , > , ws.get_qel() , mcerr);
51
52 nbeg = 0;
53 nnext = 0;
54
55 char* s = new char[qws + 1];
56 for (long n = 0; n < qws; n++) s[n] = ws[n];
57 s[qws] = '\0';
58 int ic;
59
60 char* fs = new char[qws + 1];
61 for (long n = 0; n < qws; n++) {
62 if (file.eof() == 1) {
63 delete[] fs;
64 delete[] s;
65 return 0;
66 }
67 ic = file.get();
68 fs[n] = ic;
69 }
70 fs[qws] = '\0';
71 nnext = qws;
72
73 while (strcmp(fs, s) != 0) {
74 for (long n = 1; n < qws; n++) fs[n - 1] = fs[n];
75 if (file.eof() == 1) {
76 delete[] fs;
77 delete[] s;
78 return 0;
79 }
80 ic = file.get();
81 fs[qws - 1] = ic;
82 nbeg++;
83 nnext++;
84 }
85 delete[] fs;
86 delete[] s;
87 return 1;
88}
89
90template <class T>
91int findmark_b(std::istream& file, T ws, long qws, long& nbeg, long& nnext,
92 char& prev) {
93 mfunname("int findmark_b(...)");
94 check_econd11(qws, < 1, mcerr);
95
96 nbeg = 0;
97 nnext = 0;
98 prev = '\0';
99
100 char* s = new char[qws + 1];
101 for (long n = 0; n < qws; n++) s[n] = ws[n];
102 s[qws] = '\0';
103 int ic;
104
105 char* fs = new char[qws + 1];
106 for (long n = 0; n < qws; n++) {
107 if (file.eof() == 1) {
108 delete[] fs;
109 delete[] s;
110 return 0;
111 }
112 ic = file.get();
113 fs[n] = ic;
114 }
115 fs[qws] = '\0';
116 nnext = qws;
117
118 while (strcmp(fs, s) != 0) {
119 prev = fs[0];
120 for (long n = 1; n < qws; n++) fs[n - 1] = fs[n];
121 if (file.eof() == 1) {
122 delete[] fs;
123 delete[] s;
124 return 0;
125 }
126 ic = file.get();
127 fs[qws - 1] = ic;
128 nbeg++;
129 nnext++;
130 }
131 delete[] fs;
132 delete[] s;
133 return 1;
134}
135
136// The following two functions find any of given strings, the first met.
137// They return the number of string met ( in the interval 0 -- q-1).
138// If none of strings found, they return -1.
139int find1ofnmark(std::istream& file, int q, // number of strings
140 char* s[]); // addresses
141int find1ofnmark(std::istream& file, int q, // number of strings
142 const std::string str[]); // addresses
143
144}
145
146#endif
#define check_econd11(a, signb, stream)
Definition: FunNameStack.h:155
#define mfunname(string)
Definition: FunNameStack.h:45
Definition: BGMesh.cpp:6
int findmark_b(std::istream &file, T ws, long qws, long &nbeg, long &nnext, char &prev)
Definition: findmark.h:91
int findmark(std::istream &file, const char *s)
Definition: findmark.cpp:19
int find1ofnmark(std::istream &file, int q, char *s[])
Definition: findmark.cpp:44
int findmark_a(std::istream &file, T ws, long qws, long &nbeg, long &nnext)
Definition: findmark.h:47
#define mcerr
Definition: prstream.h:128