/******************************************************************************* Copyright (c) 2008, Perforce Software, Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL PERFORCE SOFTWARE, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ /******************************************************************************* * Name : p4mapmaker.cpp * * Author : Tony Smith <tony@perforce.com> * * Description : Class to encapsulate Perforce map manipulation from Ruby * ******************************************************************************/ #include <ruby.h> #include "undefdups.h" #include <clientapi.h> #include <mapapi.h> #include "extconf.h" #include "debug.h" #include "p4rubydebug.h" #include "p4mapmaker.h" P4MapMaker::P4MapMaker() { map = new MapApi; } P4MapMaker::~P4MapMaker() { delete map; } P4MapMaker::P4MapMaker( const P4MapMaker &m ) { StrBuf l, r; const StrPtr *s; MapType t; int i; map = new MapApi; for( i = 0; i < m.map->Count(); i++ ) { s = m.map->GetLeft( i ); if( !s ) break; l = *s; s = m.map->GetRight( i ); if( !s ) break; r = *s; t = m.map->GetType( i ); map->Insert( l, r, t ); } } P4MapMaker * P4MapMaker::Join( P4MapMaker *l, P4MapMaker *r) { P4MapMaker *m = new P4MapMaker(); delete m->map; m->map = MapApi::Join( l->map, r->map ); return m; } void P4MapMaker::Insert( VALUE m ) { StrBuf in; StrBuf lbuf; StrBuf r; StrRef l; MapType t = MapInclude; in = STR2CSTR( m ); SplitMapping( in, lbuf, r ); l = lbuf.Text(); // Look for mapType in lhs only. if( l[ 0 ] == '-' ) { l += 1; t = MapExclude; } else if( l[ 0 ] == '+' ) { l += 1; t = MapOverlay; } map->Insert( l, r, t ); } void P4MapMaker::Insert( VALUE l, VALUE r ) { StrBuf left; StrBuf right; StrBuf * dest = &left; int quoted = 0; int index = 0; const char *p; MapType t = MapInclude; p = STR2CSTR( l ); for( ; ; ) { for( index = 0; *p; p++ ) { switch( *p ) { case '"': quoted = !quoted; break; case ' ': case '\t': // Embedded whitespace ok; leading not. if( quoted || index ) { dest->Extend( *p ); index++; } break; case '-': if( !index ) t = MapExclude; else dest->Extend( *p ); index++; break; case '+': if( !index ) t = MapOverlay; else dest->Extend( *p ); index++; break; default: dest->Extend( *p ); index++; } } if( dest == &right ) break; dest = &right; p = STR2CSTR( r ); quoted = 0; } left.Terminate(); right.Terminate(); map->Insert( left, right, t ); } int P4MapMaker::Count() { return map->Count(); } void P4MapMaker::Clear() { map->Clear(); } void P4MapMaker::Reverse() { MapApi * nmap = new MapApi; const StrPtr * l; const StrPtr * r; MapType t; for( int i = 0; i < map->Count(); i++ ) { l = map->GetLeft( i ); r = map->GetRight( i ); t = map->GetType( i ); nmap->Insert( *r, *l, t ); } delete map; map = nmap; } VALUE P4MapMaker::Translate( VALUE p, int fwd ) { StrBuf from; StrBuf to; MapDir dir = MapLeftRight; if( !fwd ) dir = MapRightLeft; from = STR2CSTR( p ); if( map->Translate( from, to, dir ) ) return rb_str_new( to.Text(), to.Length() ); return Qnil; } VALUE P4MapMaker::Lhs() { VALUE a = rb_ary_new(); StrBuf s; const StrPtr * l; MapType t; int quote; for( int i = 0; i < map->Count(); i++ ) { s.Clear(); quote = 0; l = map->GetLeft( i ); t = map->GetType( i ); if( l->Contains( StrRef( " " ) ) ) { quote++; s << "\""; } switch( t ) { case MapInclude: break; case MapExclude: s << "-"; break; case MapOverlay: s << "+"; }; s << l->Text(); if( quote ) s << "\""; rb_ary_push( a, rb_str_new( s.Text(), s.Length() ) ); } return a; } VALUE P4MapMaker::Rhs() { VALUE a = rb_ary_new(); StrBuf s; const StrPtr * r; int quote; for( int i = 0; i < map->Count(); i++ ) { s.Clear(); quote = 0; r = map->GetRight( i ); if( r->Contains( StrRef( " " ) ) ) { quote++; s << "\""; } s << r->Text(); if( quote ) s << "\""; rb_ary_push( a, rb_str_new( s.Text(), s.Length() ) ); } return a; } VALUE P4MapMaker::ToA() { VALUE a = rb_ary_new(); StrBuf s; const StrPtr * l; const StrPtr * r; MapType t; int quote; for( int i = 0; i < map->Count(); i++ ) { s.Clear(); quote = 0; l = map->GetLeft( i ); r = map->GetRight( i ); t = map->GetType( i ); if( l->Contains( StrRef( " " ) ) || r->Contains( StrRef( " " ) ) ) { quote++; s << "\""; } switch( t ) { case MapInclude: break; case MapExclude: s << "-"; break; case MapOverlay: s << "+"; }; s << l->Text(); if( quote ) s << "\" \""; else s << " "; s << r->Text(); if( quote ) s << "\""; rb_ary_push( a, rb_str_new( s.Text(), s.Length() ) ); } return a; } void P4MapMaker::Inspect( StrBuf &b ) { if( !map->Count() ) { b << "(empty)"; return; } const StrPtr *l, *r; int t; b << "\n"; for( int i = 0; i < map->Count(); i++ ) { l = map->GetLeft( i ); r = map->GetRight( i ); t = map->GetType( i ); b << "\t"; switch( t ) { case MapExclude: b << "-"; break; case MapOverlay: b << "+"; break; case MapInclude: break; } b << l->Text(); b << " "; b << r->Text(); b << "\n"; } } // // Take a single string containing either a half-map, or both halves of // a mapping and split it in two. If there's only one half of a mapping in // the input, then l, and r are set to the same value as 'in'. If 'in' // contains two halves, then they are split. // void P4MapMaker::SplitMapping( const StrPtr &in, StrBuf &l, StrBuf &r ) { char * pos; int quoted = 0; int split = 0; StrBuf * dest = &l; pos = in.Text(); l.Clear(); r.Clear(); while( *pos ) { switch( *pos ) { case '"': quoted = !quoted; break; case ' ': if( !quoted && !split ) { // whitespace in the middle. skip it, and start updating // the destination split = 1; dest->Terminate(); dest = &r; } else if( !quoted ) { // Trailing space on rhs. ignore } else { // Embedded space dest->Extend( *pos ); } break; default: dest->Extend( *pos ); } pos++; } l.Terminate(); r.Terminate(); if( !r.Length() ) r = l; }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#8 | 14682 | Git Fusion |
Git Fusion branch management Imported from Git ghost-of-change-num: 960958 ghost-of-sha1: 005052ae424bd69f426f7209e741ca1c8c3253c7 ghost-precedes-sha1: ad052c71a568ef12165e143a6866ad9ceffbb4a1 parent-branch: None@960958 push-state: incomplete |
||
#7 | 14608 | jmistry |
Add encoding to Strings As part of adding Ruby 1.9 support we need to associate the encoding for Ruby's strings from the server. This approach is similar to Sven's (in changelist 257263), where everything but the 'content' charset was set to 'utf8'. The content charset is picked up from P4CHARSET and this is used to translate any file content. Also disabled the Ruby 1.9 warning for each compile. User visible change to be documented in release notes. |
||
#6 | 14592 | Sven Erik Knop |
Enable P4-Ruby to compile and test with Ruby 1.9. The current solution is far from ideal because it is not possible to compile and test both Ruby 1.8 and Ruby 1.9 in parallel. The Makefile writes both artifacts and binaries to the same location. This means a user/tester/builder needs to choose on Ruby platform or ensure 'make clean' is called first. Many of the test cases also still fail in Ruby 1.9. We also need to investigate the Unicode story with Ruby 1.9 and see if the lessons learned from Python 3 can be applied somehow. Infrastructure change, no functional change yet. |
||
#5 | 14567 | tony |
Pull latest changes from p09.2 branch back home to main Integration-only change |
||
#4 | 14553 | ateague |
Integrate change for Map::reverse bug fix where it used to change the map instead of returning a reversed copy of the map. Already in the release notes for 8.2. |
||
#3 | 14538 | tony |
Fix quoting bug in P4::Map code. Quotes should not be passed down to the MapApi layer - they just need to be respected in order to identify the left- and right-hand sides of the mappings. Includes some tests to check the handling of mappings with embedded spaces and quotes. Bug fix to unreleased functionality |
||
#2 | 14537 | tony |
Skip leading whitespace when mappings are being inserted. I couldn't reproduce the crash, but hopefully this fixes it anyway. Bug fix to unreleased functionality. |
||
#1 | 14534 | tony |
Add support for the MapApi class in P4Ruby. This change adds a new class, P4::Map, to P4Ruby's canon. The methods on P4::Map are: P4::Map.new - Constructor P4::Map.load - Load a map from an array P4::Map.join - Join two maps to create a third P4::Map#clear - Empty a map P4::Map#count - Return the number of entries P4::Map#empty? - Tests whether a map object is empty P4::Map#translate - Translate a string through a map P4::Map#includes? - Tests whether a path is mapped P4::Map#lhs - Returns the left side as an array P4::Map#rhs - Returns the right side as an array P4::Map#to_a - Returns the map as an array (Note, the first three are class methods, the rest instance methods) There is also P4::Map#reverse(), but I'm not documenting that yet as I'm not sure I understand the semantics of reversing complex maps well enough. User-visible enhancement documented in p4rubynotes.txt and slated for 2008.2 release. |