Welcome to mirror list, hosted at ThFree Co, Russian Federation.

utility_io.cpp « utilities « kdl « itasc « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8d289dbfa502991ad64e615c0220fdf2da240be9 (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/** \file itasc/kdl/utilities/utility_io.cpp
 * \ingroup itasc
 */
/*****************************************************************************
 *  	Erwin Aertbelien, Div. PMA, Dep. of Mech. Eng., K.U.Leuven
 *
 * \version 
 *		ORO_Geometry V0.2
 *
 *	\par History
 *		- $log$
 *
 *	\par Release
 *		$Name:  $ 
 * \todo
 *   make IO routines more robust against the differences between DOS/UNIX end-of-line style.
 ****************************************************************************/


#include "utility_io.h"
#include "error.h"

#include <stdlib.h>
#include <ctype.h>
#include <string.h>

namespace KDL {

//
//  _functions are private functions 
//

    void _check_istream(std::istream& is)
    {
        if ((!is.good())&&(is.eof()) )
            {
            throw Error_BasicIO_File();
            }
    }
// Eats until the end of the line
static int _EatUntilEndOfLine( std::istream& is, int* countp=NULL) {
    int ch;
    int count;
    count = 0;
    do {
        ch = is.get();
        count++;
        _check_istream(is);
    } while (ch!='\n');
    if (countp!=NULL) *countp = count;
    return ch;
}

// Eats until the end of the comment
static int _EatUntilEndOfComment( std::istream& is, int* countp=NULL) {
    int ch;
    int count;
    count = 0;
    int prevch;
    ch = 0;
    do {
        prevch = ch;
        ch = is.get();
        count++;
        _check_istream(is);
        if ((prevch=='*')&&(ch=='/')) {
            break;
        }
    } while (true);
    if (countp!=NULL) *countp = count;
    ch = is.get();
    return ch;
}

// Eats space-like characters and comments
// possibly returns the number of space-like characters eaten.
static int _EatSpace( std::istream& is,int* countp=NULL) {
    int ch;
    int count;
    count=-1;
    do { 
        _check_istream(is);

        ch = is.get(); 
        count++;
        if (ch == '#') {
            ch = _EatUntilEndOfLine(is,&count);
        }
        if (ch == '/') {
            ch = is.get();
            if (ch == '/') {
                ch = _EatUntilEndOfLine(is,&count);
            } else  if (ch == '*') {
                ch = _EatUntilEndOfComment(is,&count);
            } else {
                is.putback(ch);
                ch = '/';
            }
        }
    } while ((ch==' ')||(ch=='\n')||(ch=='\t'));
    if (countp!=NULL) *countp =  count;
    return ch;
}



// Eats whites, returns, tabs and the delim character
//  Checks whether delim char. is encountered.
void Eat( std::istream& is, int delim )
{   
    int ch;
    ch=_EatSpace(is);
    if (ch != delim) {
       throw Error_BasicIO_Exp_Delim();
    }
    ch=_EatSpace(is);   
    is.putback(ch);
}

// Eats whites, returns, tabs and the delim character
//  Checks whether delim char. is encountered.
// EatEnd does not eat all space-like char's at the end.
void EatEnd( std::istream& is, int delim )
{   
    int ch;
    ch=_EatSpace(is);
    if (ch != delim) {
       throw Error_BasicIO_Exp_Delim();
    }
}



// For each space in descript, this routine eats whites,tabs, and newlines (at least one)
// There should be no consecutive spaces in the description.
// for each letter in descript, its reads the corresponding letter in the output
// the routine is case insensitive.


// Simple routine, enough for our purposes.
//  works with ASCII chars
inline char Upper(char ch) 
{
    /*if (('a'<=ch)&&(ch<='z'))
        return (ch-'a'+'A');
    else
        return ch;
    */
    return toupper(ch);
}

void Eat(std::istream& is,const char* descript)
{
    // eats whites before word
    char ch;
    char chdescr;
    ch=_EatSpace(is);   
    is.putback(ch);
    const char* p;
    p = descript;
    while ((*p)!=0) {
        chdescr = (char)Upper(*p);
        if (chdescr==' ') {
            int count=0;
            ch=_EatSpace(is,&count);
            is.putback(ch);
            if (count==0) {
                throw Error_BasicIO_Not_A_Space();
            }
        } else {
            ch=(char)is.get();
            if (chdescr!=Upper(ch)) {
               throw Error_BasicIO_Unexpected();
            }
        }
        p++;
    }
    
}



void EatWord(std::istream& is,const char* delim,char* storage,int maxsize)
{
    int ch;
    char* p;
    int size;
    // eat white before word
    ch=_EatSpace(is);
    p = storage;
    size=0;
    int count = 0;
    while ((count==0)&&(strchr(delim,ch)==NULL)) {
        *p = (char) toupper(ch);
        ++p;
        if (size==maxsize) {
           throw Error_BasicIO_ToBig();
        }
        _check_istream(is);
        ++size;
        //ch = is.get();
        ch =_EatSpace(is,&count);
    }
    *p=0;
    is.putback(ch);
}


}