enumstr.sa


Generated by gen_html_sa_files from ICSI. Contact gomes@icsi.berkeley.edu for details
 
------------------------->  GNU Sather - sourcefile  <-------------------------
-- Copyright (C) 2000 by K Hopper, University of Waikato, New Zealand        --
-- This file is part of the GNU Sather library. It is free software; you may --
-- redistribute  and/or modify it under the terms of the GNU Library General --
-- Public  License (LGPL)  as published  by the  Free  Software  Foundation; --
-- either version 2 of the license, or (at your option) any later version.   --
-- This  library  is distributed  in the  hope that it will  be  useful, but --
-- WITHOUT ANY WARRANTY without even the implied warranty of MERCHANTABILITY --
-- or FITNESS FOR A PARTICULAR PURPOSE. See Doc/LGPL for more details.       --
-- The license text is also available from:  Free Software Foundation, Inc., --
-- 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                     --
-------------->  Please email comments to <bug-sather@gnu.org>  <--------------


partial class ENUM_STR < $TEXT, $ANCHORED_FMT

partial class ENUM_STR < $TEXT, $ANCHORED_FMT is -- This partial class provides for the conversion of enumerated -- quantities to/from text strings. -- Version 1.1 Mar 99. Copyright K Hopper, U of Waikato -- Development History -- ------------------- -- Date Who By Detail -- ---- ------ ------ -- 28 Sep 98 kh Original from BOOL class -- 26 Mar 99 kh Revised for V8 of text classes private stub val_count : CARD ; -- Dummy! private const Max_Val : CARD := val_count ; const offset : CARD := 1 ; -- This is the offset from the index of first element of the Names array -- (ie zero) to the first enumeration value numeric code. const cardinality : CARD := val_count ; -- the default value! private shared Names : ARRAY{STR} ; -- for external representation! private shared lib : LIBCHARS ; private check_names( loc_lib : LIBCHARS ) pre ~void(loc_lib) -- post (lib = loc_lib) -- make comment: K.Kodama is -- This auxiliary routine attempts to set the value of the Names array -- if void or of a different culture to the required one. -- If this action is not successful then an exception has been raised. --#OUT+"enumstr.sa check_names 1\n"; if void(Names) or (lib /= loc_lib) then lib :LIBCHARS:= loc_lib ; --#OUT+"enumstr.sa check_names 2\n"; loc_res : RESOURCES := lib.culture.resources ; --#OUT+"enumstr.sa check_names 3:"+loc_res.Leaf_Name_ref+"\n"; rname:RUNES:=SYS::rune_name(self); --#OUT+"enumstr.sa check_names 4: class name:"+rname.str+"\n"; loc_names : ARRAY{STR} := loc_res.read(rname,val_count) ; --#OUT+"enumstr.sa check_names 5\n"; Names := loc_names; end; --#OUT+"enumstr.sa check_names 6\n"; end ; is_enum( str : STR ) : CONVERSION_RESULTS pre true post (result = CONVERSION_RESULTS::All_Right) or (result = CONVERSION_RESULTS::Out_of_Range) or (result = CONVERSION_RESULTS::Empty) is -- This routine checks that the format of of the leading characters of -- the given string in the given repertoire and encoding corresponds to that -- required for a real number, returning the relevant result state. --#OUT+"enumstr.sa is_enum.1\n"; if str.size = 0 then return CONVERSION_RESULTS::Empty; end ; --#OUT+"enumstr.sa is_enum.2\n"; check_names(str.index_lib) ; --#OUT+"enumstr.sa is_enum.3\n"; if ~void(lookup(str)) then --#OUT+"enumstr.sa is_enum.4\n"; return CONVERSION_RESULTS::All_Right else --#OUT+"enumstr.sa is_enum.5\n"; return CONVERSION_RESULTS::Out_of_Range end end ; build( loc_cursor : STR_CURSOR ) : SAME pre ~void(loc_cursor) and ~loc_cursor.is_done post (( result.is_nil and (initial(loc_cursor.index) = loc_cursor.index)) or (initial(loc_cursor.index) < loc_cursor.index) ) is -- This routine creates the enumerated value corresponding to the textual -- representation contained in the string indicated or the cursor has not been moved! --#OUT+"enumstr.sa build 1\n"; check_names(loc_cursor.buffer.index_lib) ; --#OUT+"enumstr.sa build 1.2\n"; loc_str : STR ; start_index : CARD := loc_cursor.index ; --#OUT+"enumstr.sa build 2\n"; loc_cursor.skip_space ; -- skip padding if loc_cursor.is_done then -- nothing to build/test loc_cursor.set_index(start_index) ; return nil else loc_str := loc_cursor.get_word ; -- up to whitespace item or end! end ; --#OUT+"enumstr.sa build 3.loc_str=["+loc_str+"]\n"; if (is_enum(loc_str) = CONVERSION_RESULTS::All_Right) then --#OUT+"enumstr.sa build 4\n"; res : CARD := lookup(loc_str) ; if res > Max_Val then res := res - Max_Val end ; return enum(res) else -- back to original position! --#OUT+"enumstr.sa build 5\n"; loc_cursor.set_index(start_index) ; return nil end end ; create( str : STR ) : SAME is -- This is the creation routine from a string which is any of the -- abbreviations listed in the Name table. If the name table has not been -- read in this is done first. --#OUT+"enumstr.sa create:"+str+"\n"; return build(str.cursor) end ; str( lib : LIBCHARS ) : STR is -- This routine provides a string representation of self in the given -- character repertoire and encoding - including a nil value. if is_nil then return ENV_CHAR::Nil_Name else check_names(lib) ; return Names[card - offset].str end end ; str : STR is -- This routine provides a string representation of self in the default -- character repertoire and encoding - including a nil value. return str(LIBCHARS::default) end ; fmt( format : ANCHORED_DESCR, lib : LIBCHARS ) : STR pre ~is_nil and ~void(format) and ~void(lib) post ~void(result) is -- This routine returns the formatted string representation of self in -- accordance with the string format. res : STR := str(lib) ; loc_fill : STR := STR::create(lib) + format.filler.char ; if res.size < format.leading then -- needs a filler res := loc_fill.repeat(format.leading - res.size) + res end ; return res + loc_fill.repeat(format.trailing) end ; fmt( format : ANCHORED_DESCR ) : STR pre ~is_nil and (format.width > 0) post ~void(result) is -- This routine returns the formatted string representation of self in -- accordance with the string format. return fmt(format,LIBCHARS::default) end ; end ; -- ENUM_STR