Difference between revisions of "CcFixListDatatypes"

From Second Life Wiki
Jump to navigation Jump to search
(New page: 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 cu...)
 
m (<lsl> tag to <source>)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:LSL_User-Defined_Functions]]
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.
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.
Does not currently cast keys to keys.  It casts them to strings.


<lsl>
<source lang="lsl2">
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//
//
Line 50: Line 52:
{
{
     //Liandra Ceawlin 2008-09-20
     //Liandra Ceawlin 2008-09-20
     integer i;
     integer i = ~llGetListLength(l);
    for( i=0; i<llGetListLength(l); i++ )
    string c;
   
    @next1;
    @next2;
    while( ++i )
     {
     {
         integer is_integer = TRUE;
         integer test = TRUE;
         integer is_float = TRUE;
         integer j = 0;
        integer is_vector = TRUE;
         string s = llStringTrim( llList2String(l, i), STRING_TRIM);
        integer is_rot = TRUE;
         if( s )
        integer j;
         string s = llStringTrim(llList2String(l,i),STRING_TRIM);
         if( s == "" )
            jump cont1;
        for( j=0; j<llStringLength(s); j++ )
         {
         {
             string c = llGetSubString(s,j,j);
             list n;
             if( (integer)c==0 && c!="0" )
             if( (llGetSubString(s, 0, 0) == "<") && (llGetSubString(s, -1, -1) == ">") )
             {
             {
                 is_integer = FALSE;
                 list tl = llParseString2List(llDeleteSubString(s, -1, 0), [","], []);
                 if( c != "." )
                j = ~llGetListLength(tl);
                     is_float = FALSE;
                 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);
                }
             }
             }
        }
        if( llGetSubString(s,0,0)=="<" && llGetSubString(s,llStringLength(s)-1,llStringLength(s)-1)==">" )
        {
            string tmp = llDeleteSubString(s,0,0);
            tmp = llDeleteSubString(tmp,llStringLength(tmp)-1,llStringLength(tmp)-1);
            list tl = llParseString2List(tmp,[","],[]);
            if( llGetListLength(tl) == 3 )
                is_rot = FALSE;
            else if( llGetListLength(tl) == 4 )
                is_vector = FALSE;
             else
             else
             {
             {
                 is_rot = FALSE;
                 j = ~llStringLength(s);
                is_vector = FALSE;
                 while( ++j )
                jump cont2;
            }
            for( j=0; j<llGetListLength(tl); j++ )
            {
                 integer k;
                string ts = llStringTrim(llList2String(tl,j),STRING_TRIM);
                for( k=0; k<llStringLength(ts); k++ )
                 {
                 {
                     string tc = llGetSubString(ts,k,k);
                     c = llGetSubString(s, j, j);
                     if( (integer)tc==0 && tc!="0" && tc!="." && tc!="-" )
                     if( !(integer)c && c!="0" )
                     {
                     {
                         is_rot=FALSE;
                         test = (c != ".");
                         is_vector = FALSE;
                         if(test)
                        jump cont3;
                            jump next2;
                     }
                     }
                 }
                 }
                if( test )
                    n = [(integer)s];
                else
                    n = [(float)s];
                l = llListReplaceList(l, n, i, i);
             }
             }
         }
         }
        else
        {
            is_vector = FALSE;
            is_rot = FALSE;
        }
        list n;
        if( is_integer )
            n += (integer)s;
        else if( is_float )
            n += (float)s;
        else if( is_vector )
            n += (vector)s;
        else if( is_rot )
            n += (rotation)s;
        else
            jump cont4;
        l = llListReplaceList(l,n,i,i);
        @cont1;
        @cont2;
        @cont3;
        @cont4;
     }
     }
     return l;
     return l;
Line 143: Line 130:
         for( i=0; i<llGetListLength(shape); i++ )
         for( i=0; i<llGetListLength(shape); i++ )
             shape = llListReplaceList( shape, [llList2String(shape,i)], i, i );
             shape = llListReplaceList( shape, [llList2String(shape,i)], i, i );
           
       
         llSay( DEBUG_CHANNEL, "This will cause an error." );
         llSay( DEBUG_CHANNEL, "This will cause an error." );
         llSetPrimitiveParams( shape );
         llSetPrimitiveParams( shape );
Line 153: Line 140:
          
          
         llSay( DEBUG_CHANNEL, "Done." );
         llSay( DEBUG_CHANNEL, "Done." );
     }      
     }
}
}
</lsl>
</source>

Latest revision as of 15:12, 22 January 2015


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." );
    }
}