Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Library: Type Check

Kri Ayakashi
Registered User
Join date: 11 Apr 2007
Posts: 3
03-12-2008 03:29
This little snippet let's you check what type hides within string before you cast it to other things.
From: someone

// type.lsl
//
// Type check
//
// @author Kri Ayakashi (Bartosz Ptaszynski)
// @email [email]yazzgoth@gmail.com[/email]
// @web http://www.yazzgoth.org/
//
// This is available under BSD licence.
//
// Copyright (c) 2008, Bartosz Ptaszynski
// 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 the <ORGANIZATION> 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.

integer type(string obj)
{
if( (string) ((integer)obj) == obj )
return TYPE_INTEGER;
else if( (string) ((float)obj) == obj)
return TYPE_FLOAT;
else if( (string) ((vector)obj) == obj)
return TYPE_VECTOR;
else if( (string) ((rotation)obj) == obj)
return TYPE_ROTATION;
else if( llStringLength(obj)==36 && llGetSubString(obj, 8, 8)=="-" && llGetSubString(obj, 13, 13)=="-" && llGetSubString(obj, 18, 18)=="-" && llGetSubString(obj, 23, 23)=="-";)
return TYPE_KEY;
else
return TYPE_STRING;
}

default
{
state_entry()
{
llOwnerSay((string)type((string)llGetOwner()));
llOwnerSay((string)type("abc";));
llOwnerSay((string)type((string)123));
llOwnerSay((string)type((string)123.0));
llOwnerSay((string)type((string)<1.0, 1.0, 0.0>;));
llOwnerSay((string)type((string)<0.0, 0.0, 0.0, 1.0>;));
}


}
Nada Epoch
The Librarian
Join date: 4 Nov 2002
Posts: 1,423
Library bump
03-12-2008 19:36
:)
_____________________
i've got nothing. ;)
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
03-12-2008 21:50
a simpler valid key test:
if ((key)obj) return TYPE_KEY;

also be aware that leading zeros on integers are valid, but will be ignored, and trailling zeros can be reported on valid floats, which may skew your test results for all types using them
_____________________
|
| . "Cat-Like Typing Detected"
| . This post may contain errors in logic, spelling, and
| . grammar known to the SL populace to cause confusion
|
| - Please Use PHP tags when posting scripts/code, Thanks.
| - Can't See PHP or URL Tags Correctly? Check Out This Link...
| -