/* Copyright (c) 1997-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. */ #include "EXTERN.h" #include "perl.h" #include "XSUB.h" typedef enum { NUMBER, STRING } RecType; /******************************************************************************* * Function to convert a string of the form "@text with embedded @@ signs@" * into "text with embedded @ signs". ******************************************************************************/ static SV *UnescapeField( const char *str, int len ) { int in, out; int prev_at = 0; char *buf = 0; SV *cleanSV = 0; Newz( 1, buf, len, char ); if ( len < 2 ) croak( "Can't un-escape string - too short to be escaped!" ); /* Skip the first and last @ signs automatically */ for( in = 1, out = 0; in < ( len - 1 ); ) { if ( str[ in ] == '@' ) { if ( prev_at ) buf[ out++ ] = '@'; prev_at = ! prev_at; in++; } else { buf[ out++ ] = str[ in++ ]; prev_at = 0; } } cleanSV = sv_2mortal( newSVpv( buf, out ) ); Safefree( buf ); return cleanSV; } /******************************************************************************* * Function to escape a string and return the escaped string ******************************************************************************/ SV * EscapeField( SV *str ) { SV *newstr; SV **t_ptr; char *p; STRLEN len; int start, end; if ( ! SvPOK ( str ) ) croak( "argument is not a string" ); p = SvPV( str, len ); newstr = sv_2mortal( newSVpv( "@", 1 ) ); for ( start = end = 0; end < len; end++ ) { if ( p[ end ] == '@' ) { sv_catpvn( newstr, p + start, end - start + 1 ); sv_catpv( newstr, "@" ); start = end + 1; } } sv_catpvn( newstr, p + start, end - start ); sv_catpv( newstr, "@" ); return newstr; } MODULE = P4::Journal PACKAGE = P4::JournalRec #****************************************************************************** # Function to parse a journal record and return a Perl array containing the # individual fields #****************************************************************************** void Fields( THIS ) SV *THIS PREINIT: register char *start; register char *end; SV **t_ptr; register int in_string; register int prev_at; /* last char was an @ */ PPCODE: if ( ! sv_isa( THIS, "P4::JournalRec" ) ) croak( "Not a P4::JournalRec object" ); t_ptr = hv_fetch( (HV *)SvRV( THIS ), "record", 6, 0 ); if ( ! t_ptr ) croak( "No member 'record' in P4::JournalRec object" ); if ( ! SvPOK ( *t_ptr ) ) croak( "'record' member is not a string" ); start = end = SvPV( *t_ptr, PL_na ); in_string = 0; for ( prev_at = 0; *end; ) { if ( in_string ) { if ( *end == '@' ) { prev_at = ! prev_at; end++; } else if ( prev_at ) { /* Found the end of the string */ EXTEND( SP, 1 ); XPUSHs( UnescapeField( start, end - start) ); in_string = 0; prev_at = 0; start = ++end; } else { end++; } } else { if ( *start == ' ' ) { end = ++start; } else if ( *end == '@' ) { in_string = 1; end++; } else if ( *end == ' ' ) { /* Found the end of the field */ EXTEND( SP, 1 ); XPUSHs( sv_2mortal( newSVpv( start, end - start ) ) ); start = ++end; } else { end++; } } } if ( *start ) { /* Push the last field. */ EXTEND( SP, 1 ); XPUSHs( sv_2mortal( newSVpv( start, end - start ) ) ); } void Set( THIS, ... ) SV *THIS PREINIT: SV *rec; SV *field; int item; char *p; RecType type; PPCODE: if ( ! sv_isa( THIS, "P4::JournalRec" ) ) croak( "Not a P4::JournalRec object" ); rec = newSVpv( "", 0 ); for ( item = 1; item < items; item++ ) { int offset = 0; STRLEN len; type = NUMBER; field = ST( item ); for ( p = SvPV( field, len ); *p; p++ ) { if ( ( *p < '0' || *p > '9' ) && ( *p < 'A' || *p > 'F' ) && ( *p != '-' ) ) type = STRING; else if ( *p == '-' && offset ) type = STRING; offset++; } /* empty fields are empty strings */ if ( len == 0 ) type = STRING; if ( type == STRING ) field = EscapeField( field ); sv_catsv( rec, field ); sv_catpv( rec, " " ); } hv_store( (HV *)SvRV( THIS ), "record", 6, rec, 0 ); EXTEND( SP, 1 ); XPUSHs( rec ); MODULE = P4::Journal PACKAGE = P4::Journal #****************************************************************************** # Function to determine whether or not the journal record we have is # complete, or whether it ends in the middle of a multi-line field. #****************************************************************************** int CompleteRecord( THIS, rec ) SV *THIS SV *rec PREINIT: int in_string = 0; int prev_at = 0; char *str; CODE: if ( ! SvPOK( rec ) ) croak( "Not a string" ); for( str = SvPV( rec, PL_na ); *str; str++ ) { if ( in_string ) { if ( *str == '@' ) { prev_at = ! prev_at; } else if ( prev_at ) { prev_at = 0; in_string = 0; } } else { if ( *str == '@' ) in_string = 1; } } RETVAL = ! in_string; OUTPUT: RETVAL