-- Copyright: Mario Schweigler (C) 2005-2006
-- Institution: Computing Laboratory, University of Kent, Canterbury, UK
-- Description: pony internal utilities code file

-- This program is free software; you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation; either version 2 of the License, or
-- (at your option) any later version.
--
-- This program 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 the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
-- MA 02110-1301, USA.

--{{{  Auxiliary PROCs and FUNCTIONs

--{{{  PROC pony.int.concat.str
-- Concatenate two strings
-- Parameters: str  | RESULT: string to be returned
--             str1 | VAL: first string
--             str1 | VAL: second string
PROC pony.int.concat.str (RESULT MOBILE []BYTE str, VAL []BYTE str1, str2)
  SEQ
    str := MOBILE [(SIZE str1) + (SIZE str2)]BYTE
    [str FOR SIZE str1] := str1
    [str FROM SIZE str1 FOR SIZE str2] := str2
:
--}}}
--{{{  PROC pony.int.append.str
-- Append a string to a string
-- Parameters: str     | string to be extended
--             app.str | VAL: string to be appended
PROC pony.int.append.str (MOBILE []BYTE str, VAL []BYTE app.str)
  INITIAL MOBILE []BYTE old.str IS str:
  pony.int.concat.str (str, old.str, app.str)
:
--}}}
--{{{  PROC pony.int.prepend.str
-- Prepend a string to a string
-- Parameters: str     | string to be extended
--             pre.str | VAL: string to be prepended
PROC pony.int.prepend.str (MOBILE []BYTE str, VAL []BYTE pre.str)
  INITIAL MOBILE []BYTE old.str IS str:
  pony.int.concat.str (str, pre.str, old.str)
:
--}}}
--{{{  PROC pony.int.append.int
-- Append an integer to a string
-- Parameters: str     | string to be extended
--             app.int | VAL: integer to be appended
PROC pony.int.append.int (MOBILE []BYTE str, VAL INT app.int)
  IF
    --{{{  Most negative integer
    app.int = (MOSTNEG INT)
      pony.int.append.str (str, "-2147483648")
    --}}}
    --{{{  Zero
    app.int = 0
      pony.int.append.str (str, "0")
    --}}}
    --{{{  Else
    TRUE
      INT abs:
      INT neg.offset:
      [10]INT digits:
      INITIAL INT num.digits IS 0:
      MOBILE []BYTE int.str:
      SEQ
        --{{{  Get absolute
        IF
          app.int < 0
            SEQ
              abs := -app.int
              neg.offset := 1
          TRUE
            SEQ
              abs := app.int
              neg.offset := 0
        --}}}
        --{{{  Get digits
        WHILE abs > 0
          SEQ
            digits[num.digits] := abs \ 10
            abs := abs / 10
            num.digits := num.digits + 1
        --}}}
        --{{{  Concat digits (and sign if applicable)
        int.str := MOBILE [num.digits + neg.offset]BYTE
        IF
          app.int < 0
            int.str[0] := '-'
          TRUE
            SKIP
        SEQ i = neg.offset FOR num.digits
          int.str[i] := (BYTE ((INT '0') + digits[((num.digits - i) - 1) + neg.offset]))
        --}}}
        pony.int.append.str (str, int.str)
    --}}}
:
--}}}
--{{{  PROC pony.int.append.hex
-- Append an integer in hex format to a string
-- Parameters: str     | string to be extended
--             app.int | VAL: integer to be appended
PROC pony.int.append.hex (MOBILE []BYTE str, VAL INT app.int)
  IF
    --{{{  Most negative integer
    app.int = (MOSTNEG INT)
      pony.int.append.str (str, "#80000000")
    --}}}
    --{{{  Zero
    app.int = 0
      pony.int.append.str (str, "#00000000")
    --}}}
    --{{{  Else
    TRUE
      VAL []BYTE cmap IS "0123456789ABCDEF":
      [9]BYTE hex.str:
      SEQ
        hex.str[0] := '#'
        SEQ i = 1 FOR 8
          hex.str[i] := cmap[((app.int >> ((8 - i) << 2)) /\ #0000000F)]
        pony.int.append.str (str, hex.str)
    --}}}
:
--}}}
--{{{  PROC pony.int.append.bool
-- Append a boolean to a string
-- Parameters: str      | string to be extended
--             app.bool | VAL: boolean to be appended
PROC pony.int.append.bool (MOBILE []BYTE str, VAL BOOL app.bool)
  IF
    app.bool
      pony.int.append.str (str, "TRUE")
    TRUE
      pony.int.append.str (str, "FALSE")
:
--}}}
--{{{  BOOL FUNCTION pony.int.equal.str
-- Check whether two strings are equal
-- Parameters: str1 | VAL: first string
--             str2 | VAL: second string
BOOL FUNCTION pony.int.equal.str (VAL []BYTE str1, str2)
  BOOL result:
  VALOF
    IF
      (SIZE str1) <> (SIZE str2)
        result := FALSE
      TRUE
        SEQ
          result := TRUE
          INITIAL INT i IS 0:
          WHILE result AND (i < (SIZE str1))
            SEQ
              IF
                str1[i] <> str2[i]
                  result := FALSE
                TRUE
                  SKIP
              i := i + 1
    RESULT result
:
--}}}

--}}}

