p4ruby 2022.1.2359956-x64-mingw-ucrt

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,459 @@
1
+ /*******************************************************************************
2
+
3
+ Copyright (c) 2008, Perforce Software, Inc. All rights reserved.
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright
9
+ notice, this list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright
12
+ notice, this list of conditions and the following disclaimer in the
13
+ documentation and/or other materials provided with the distribution.
14
+
15
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
+ ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY
19
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+
26
+ *******************************************************************************/
27
+
28
+ /*******************************************************************************
29
+ * Name : p4mapmaker.cpp
30
+ *
31
+ * Author : Tony Smith <tony@perforce.com>
32
+ *
33
+ * Description : Class to encapsulate Perforce map manipulation from Ruby
34
+ *
35
+ ******************************************************************************/
36
+ #include <ruby.h>
37
+ #include "undefdups.h"
38
+ #include <p4/clientapi.h>
39
+ #include <p4/mapapi.h>
40
+ #include <p4/debug.h>
41
+ #include "p4rubydebug.h"
42
+ #include "p4utils.h"
43
+ #include "p4mapmaker.h"
44
+
45
+
46
+ P4MapMaker::P4MapMaker()
47
+ {
48
+ map = new MapApi;
49
+ }
50
+
51
+ P4MapMaker::~P4MapMaker()
52
+ {
53
+ delete map;
54
+ }
55
+
56
+ P4MapMaker::P4MapMaker( const P4MapMaker &m )
57
+ {
58
+ StrBuf l, r;
59
+ const StrPtr *s;
60
+ MapType t;
61
+ int i;
62
+
63
+ map = new MapApi;
64
+ for( i = 0; i < m.map->Count(); i++ )
65
+ {
66
+ s = m.map->GetLeft( i );
67
+ if( !s ) break;
68
+ l = *s;
69
+
70
+ s = m.map->GetRight( i );
71
+ if( !s ) break;
72
+ r = *s;
73
+
74
+ t = m.map->GetType( i );
75
+
76
+ map->Insert( l, r, t );
77
+ }
78
+ }
79
+
80
+
81
+ P4MapMaker *
82
+ P4MapMaker::Join( P4MapMaker *l, P4MapMaker *r)
83
+ {
84
+ P4MapMaker *m = new P4MapMaker();
85
+ delete m->map;
86
+
87
+ m->map = MapApi::Join( l->map, r->map );
88
+ return m;
89
+ }
90
+
91
+ void
92
+ P4MapMaker::Insert( VALUE m )
93
+ {
94
+ StrBuf in;
95
+ StrBuf lbuf;
96
+ StrBuf r;
97
+ StrRef l;
98
+ MapType t = MapInclude;
99
+
100
+ in = StringValuePtr( m );
101
+ SplitMapping( in, lbuf, r );
102
+
103
+ l = lbuf.Text();
104
+
105
+ // Look for mapType in lhs only.
106
+ if( l[ 0 ] == '-' )
107
+ {
108
+ l += 1;
109
+ t = MapExclude;
110
+ }
111
+ else if( l[ 0 ] == '+' )
112
+ {
113
+ l += 1;
114
+ t = MapOverlay;
115
+ }
116
+
117
+ map->Insert( l, r, t );
118
+ }
119
+
120
+
121
+ void
122
+ P4MapMaker::Insert( VALUE l, VALUE r )
123
+ {
124
+ StrBuf left;
125
+ StrBuf right;
126
+ StrBuf * dest = &left;
127
+ int quoted = 0;
128
+ int index = 0;
129
+
130
+ const char *p;
131
+ MapType t = MapInclude;
132
+
133
+ p = StringValuePtr( l );
134
+ for( ; ; )
135
+ {
136
+ for( index = 0; *p; p++ )
137
+ {
138
+ switch( *p )
139
+ {
140
+ case '"':
141
+ quoted = !quoted;
142
+ break;
143
+
144
+ case ' ':
145
+ case '\t':
146
+ // Embedded whitespace ok; leading not.
147
+ if( quoted || index )
148
+ {
149
+ dest->Extend( *p );
150
+ index++;
151
+ }
152
+ break;
153
+
154
+ case '-':
155
+ if( !index )
156
+ t = MapExclude;
157
+ else
158
+ dest->Extend( *p );
159
+ index++;
160
+ break;
161
+
162
+ case '+':
163
+ if( !index )
164
+ t = MapOverlay;
165
+ else
166
+ dest->Extend( *p );
167
+ index++;
168
+ break;
169
+
170
+ default:
171
+ dest->Extend( *p );
172
+ index++;
173
+ }
174
+ }
175
+
176
+ if( dest == &right )
177
+ break;
178
+
179
+ dest = &right;
180
+ p = StringValuePtr( r );
181
+ quoted = 0;
182
+ }
183
+ left.Terminate();
184
+ right.Terminate();
185
+
186
+ map->Insert( left, right, t );
187
+ }
188
+
189
+ int
190
+ P4MapMaker::Count()
191
+ {
192
+ return map->Count();
193
+ }
194
+
195
+ void
196
+ P4MapMaker::Clear()
197
+ {
198
+ map->Clear();
199
+ }
200
+
201
+ void
202
+ P4MapMaker::Reverse()
203
+ {
204
+ MapApi * nmap = new MapApi;
205
+ const StrPtr * l;
206
+ const StrPtr * r;
207
+ MapType t;
208
+
209
+ for( int i = 0; i < map->Count(); i++ )
210
+ {
211
+ l = map->GetLeft( i );
212
+ r = map->GetRight( i );
213
+ t = map->GetType( i );
214
+
215
+ nmap->Insert( *r, *l, t );
216
+ }
217
+
218
+ delete map;
219
+ map = nmap;
220
+ }
221
+
222
+ VALUE
223
+ P4MapMaker::Translate( VALUE p, int fwd )
224
+ {
225
+ StrBuf from;
226
+ StrBuf to;
227
+ MapDir dir = MapLeftRight;
228
+
229
+ if( !fwd )
230
+ dir = MapRightLeft;
231
+
232
+ from = StringValuePtr( p );
233
+ if( map->Translate( from, to, dir ) )
234
+ return P4Utils::ruby_string( to.Text(), to.Length() );
235
+ return Qnil;
236
+ }
237
+
238
+ VALUE
239
+ P4MapMaker::Lhs()
240
+ {
241
+ VALUE a = rb_ary_new();
242
+ StrBuf s;
243
+ const StrPtr * l;
244
+ MapType t;
245
+ int quote;
246
+
247
+ for( int i = 0; i < map->Count(); i++ )
248
+ {
249
+ s.Clear();
250
+ quote = 0;
251
+
252
+ l = map->GetLeft( i );
253
+ t = map->GetType( i );
254
+
255
+ if( l->Contains( StrRef( " " ) ) )
256
+ {
257
+ quote++;
258
+ s << "\"";
259
+ }
260
+
261
+ switch( t )
262
+ {
263
+ case MapInclude:
264
+ break;
265
+ case MapExclude:
266
+ s << "-";
267
+ break;
268
+ case MapOverlay:
269
+ s << "+";
270
+ };
271
+
272
+ s << l->Text();
273
+ if( quote ) s << "\"";
274
+
275
+ rb_ary_push( a, P4Utils::ruby_string( s.Text(), s.Length() ) );
276
+ }
277
+ return a;
278
+ }
279
+
280
+ VALUE
281
+ P4MapMaker::Rhs()
282
+ {
283
+ VALUE a = rb_ary_new();
284
+ StrBuf s;
285
+ const StrPtr * r;
286
+ int quote;
287
+
288
+ for( int i = 0; i < map->Count(); i++ )
289
+ {
290
+ s.Clear();
291
+ quote = 0;
292
+
293
+ r = map->GetRight( i );
294
+
295
+ if( r->Contains( StrRef( " " ) ) )
296
+ {
297
+ quote++;
298
+ s << "\"";
299
+ }
300
+
301
+ s << r->Text();
302
+ if( quote ) s << "\"";
303
+
304
+ rb_ary_push( a, P4Utils::ruby_string( s.Text(), s.Length() ) );
305
+ }
306
+ return a;
307
+ }
308
+
309
+ VALUE
310
+ P4MapMaker::ToA()
311
+ {
312
+ VALUE a = rb_ary_new();
313
+ StrBuf s;
314
+ const StrPtr * l;
315
+ const StrPtr * r;
316
+ MapType t;
317
+ int quote;
318
+
319
+ for( int i = 0; i < map->Count(); i++ )
320
+ {
321
+ s.Clear();
322
+ quote = 0;
323
+
324
+ l = map->GetLeft( i );
325
+ r = map->GetRight( i );
326
+ t = map->GetType( i );
327
+
328
+ if( l->Contains( StrRef( " " ) ) ||
329
+ r->Contains( StrRef( " " ) ) )
330
+ {
331
+ quote++;
332
+ s << "\"";
333
+ }
334
+
335
+ switch( t )
336
+ {
337
+ case MapInclude:
338
+ break;
339
+ case MapExclude:
340
+ s << "-";
341
+ break;
342
+ case MapOverlay:
343
+ s << "+";
344
+ };
345
+
346
+ s << l->Text();
347
+
348
+ if( quote ) s << "\" \"";
349
+ else s << " ";
350
+
351
+ s << r->Text();
352
+ if( quote ) s << "\"";
353
+
354
+ rb_ary_push( a, P4Utils::ruby_string( s.Text(), s.Length() ) );
355
+ }
356
+ return a;
357
+ }
358
+
359
+ void
360
+ P4MapMaker::Inspect( StrBuf &b )
361
+ {
362
+ if( !map->Count() )
363
+ {
364
+ b << "(empty)";
365
+ return;
366
+ }
367
+
368
+ const StrPtr *l, *r;
369
+ int t;
370
+
371
+ b << "\n";
372
+
373
+ for( int i = 0; i < map->Count(); i++ )
374
+ {
375
+
376
+ l = map->GetLeft( i );
377
+ r = map->GetRight( i );
378
+ t = map->GetType( i );
379
+
380
+ b << "\t";
381
+ switch( t )
382
+ {
383
+ case MapExclude:
384
+ b << "-";
385
+ break;
386
+
387
+ case MapOverlay:
388
+ b << "+";
389
+ break;
390
+
391
+ case MapInclude:
392
+ break;
393
+ }
394
+
395
+ b << l->Text();
396
+ b << " ";
397
+ b << r->Text();
398
+ b << "\n";
399
+ }
400
+ }
401
+
402
+ //
403
+ // Take a single string containing either a half-map, or both halves of
404
+ // a mapping and split it in two. If there's only one half of a mapping in
405
+ // the input, then l, and r are set to the same value as 'in'. If 'in'
406
+ // contains two halves, then they are split.
407
+ //
408
+ void
409
+ P4MapMaker::SplitMapping( const StrPtr &in, StrBuf &l, StrBuf &r )
410
+ {
411
+ char * pos;
412
+ int quoted = 0;
413
+ int split = 0;
414
+ StrBuf * dest = &l;
415
+
416
+ pos = in.Text();
417
+
418
+ l.Clear();
419
+ r.Clear();
420
+
421
+ while( *pos )
422
+ {
423
+ switch( *pos )
424
+ {
425
+ case '"':
426
+ quoted = !quoted;
427
+ break;
428
+
429
+ case ' ':
430
+ if( !quoted && !split )
431
+ {
432
+ // whitespace in the middle. skip it, and start updating
433
+ // the destination
434
+ split = 1;
435
+ dest->Terminate();
436
+ dest = &r;
437
+ }
438
+ else if( !quoted )
439
+ {
440
+ // Trailing space on rhs. ignore
441
+ }
442
+ else
443
+ {
444
+ // Embedded space
445
+ dest->Extend( *pos );
446
+ }
447
+ break;
448
+
449
+ default:
450
+ dest->Extend( *pos );
451
+ }
452
+ pos++;
453
+ }
454
+ l.Terminate();
455
+ r.Terminate();
456
+
457
+ if( !r.Length() )
458
+ r = l;
459
+ }
@@ -0,0 +1,69 @@
1
+ /*******************************************************************************
2
+
3
+ Copyright (c) 2008, Perforce Software, Inc. All rights reserved.
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright
9
+ notice, this list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright
12
+ notice, this list of conditions and the following disclaimer in the
13
+ documentation and/or other materials provided with the distribution.
14
+
15
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
+ ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY
19
+ DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
+ (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21
+ LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22
+ ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+
26
+ *******************************************************************************/
27
+
28
+ /*******************************************************************************
29
+ * Name : p4mapmaker.h
30
+ *
31
+ * Author : Tony Smith <tony@perforce.com>
32
+ *
33
+ * Description : Class to encapsulate Perforce map manipulation from Ruby
34
+ *
35
+ ******************************************************************************/
36
+
37
+ class MapApi;
38
+ class P4MapMaker
39
+ {
40
+ public:
41
+ P4MapMaker();
42
+ P4MapMaker( const P4MapMaker &m );
43
+
44
+ ~P4MapMaker();
45
+
46
+ static P4MapMaker * Join( P4MapMaker *l, P4MapMaker *r);
47
+
48
+ void Insert( VALUE m );
49
+ void Insert( VALUE l, VALUE r );
50
+
51
+ void Reverse();
52
+ void Clear();
53
+ int Count();
54
+ VALUE Translate( VALUE p, int fwd = 1 );
55
+ VALUE Lhs();
56
+ VALUE Rhs();
57
+ VALUE ToA();
58
+
59
+ void Inspect( StrBuf &b );
60
+
61
+ // We hold no references to Ruby objects, so no GC to do...
62
+ void GCMark() {}
63
+
64
+ private:
65
+ void SplitMapping( const StrPtr &in, StrBuf &l, StrBuf &r );
66
+ MapApi * map;
67
+ };
68
+
69
+