CcFixListDatatypes

From Second Life Wiki
Revision as of 19:03, 9 October 2008 by Liandra Ceawlin (talk | contribs) (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...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.

<lsl> /////////////////////////////////////////////////////////////////////////////// // // 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;
   for( i=0; i<llGetListLength(l); i++ )
   {
       integer is_integer = TRUE;
       integer is_float = TRUE;
       integer is_vector = TRUE;
       integer is_rot = TRUE;
       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);
           if( (integer)c==0 && c!="0" )
           {
               is_integer = FALSE;
               if( c != "." )
                   is_float = FALSE;
           }
       }
       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
           {
               is_rot = FALSE;
               is_vector = FALSE;
               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);
                   if( (integer)tc==0 && tc!="0" && tc!="." && tc!="-" )
                   {
                       is_rot=FALSE;
                       is_vector = FALSE;
                       jump cont3;
                   }
               }
           }
       }
       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;

}

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

} </lsl>