CcFixListDatatypes

From Second Life Wiki
Jump to navigation Jump to search


Walks a list, casts the elements to the appropriate types, and returns a fixed list. Useful for things like llSetPrimitiveParams when you've parsed your data out of a string.

Does not currently cast keys to keys. It casts them to strings.

///////////////////////////////////////////////////////////////////////////////
//
// ccFixListDatatypes
//      Walks a list and casts the data to the appropriate type, then returns
//  the cast list.  Useful for things like llSetPrimitiveParams when data is
//  parsed from strings.
//
//  v3 - Initial public release
//  v4 - Fixes parsing of negative elements in vectors and rotations.
//
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2008, Ceawlin Creations
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// 
//     * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//     * 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.
//     * Neither the name of Ceawlin Creations, Liandra Ceawlin, or A. J.
// Taylor, nor the names of its contributors may be used to endorse or promote
// products derived from this software without specific prior written
// permission.
// 
// 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 THE COPYRIGHT OWNER OR CONTRIBUTORS 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.
//
///////////////////////////////////////////////////////////////////////////////

// This code is far from optimal, lol.
list    ccFixListDatatypes( list l )
{
    //Liandra Ceawlin 2008-09-20
    integer i = ~llGetListLength(l);
    string c;
    
    @next1;
    @next2;
    while( ++i )
    {
        integer test = TRUE;
        integer j = 0;
        string s = llStringTrim( llList2String(l, i), STRING_TRIM);
        if( s )
        {
            list n;
            if( (llGetSubString(s, 0, 0) == "<") && (llGetSubString(s, -1, -1) == ">") )
            {
                list tl = llParseString2List(llDeleteSubString(s, -1, 0), [","], []);
                j = ~llGetListLength(tl);
                test = (j == -4);//TRUE if vector
                if( test || (j == -5) )
                {
                    while( ++j )
                    {
                        string ts = llStringTrim(llList2String(tl, j), STRING_TRIM);
                        integer k = ~llStringLength(ts);
                        while( ++k )
                        {
                            c = llGetSubString(ts, k, k);
                            if( !(integer)c && !~llSubStringIndex("0.-", c))
                                jump next1;
                        }
                    }
                    if( test )
                        n = [(vector)s];
                    else
                        n = [(rotation)s];
                    l = llListReplaceList(l, n, i, i);
                }
            }
            else
            {
                j = ~llStringLength(s);
                while( ++j )
                {
                    c = llGetSubString(s, j, j);
                    if( !(integer)c && c!="0" )
                    {
                        test = (c != ".");
                        if(test)
                            jump next2;
                    }
                }
                if( test )
                    n = [(integer)s];
                else
                    n = [(float)s];
                l = llListReplaceList(l, n, i, i);
            }
        }
    }
    return l;
}

default
{
    on_rez( integer param )
    {
        llSay( 0, "Open your script error/warning window and touch me to run the test." );
    }
    
    touch_start(integer det)
    {
        llSay( DEBUG_CHANNEL, "Testing..." );
        list shape= llGetPrimitiveParams([PRIM_TYPE]);
        
        // cast shape to all strings, like we just parsed it out of some chat or something....
        integer i;
        for( i=0; i<llGetListLength(shape); i++ )
            shape = llListReplaceList( shape, [llList2String(shape,i)], i, i );
        
        llSay( DEBUG_CHANNEL, "This will cause an error." );
        llSetPrimitiveParams( shape );
        
        // Cast the string list into the proper datatypes.
        list new = ccFixListDatatypes( shape );
        llSay( DEBUG_CHANNEL, "But this shouldn't..." );
        llSetPrimitiveParams( [PRIM_TYPE]+new );
        
        llSay( DEBUG_CHANNEL, "Done." );
    }
}