Re: Converting text to integers

new topic     » goto parent     » topic index » view thread      » older message » newer message

--=====================_850605912==_

At 00:05 14-12-96 -0500, you wrote:
>---------------------- Information from the mail header -----------------------
>Sender:       Euphoria Programming for MS-DOS <EUPHORIA at
>MIAMIU.ACS.MUOHIO.EDU>
>Poster:       James Harris <blackdog at CDC.NET>
>Subject:      Converting text to integers
>-------------------------------------------------------------------------------
>
>Hello all,
>
>I need a little help. I'm trying to parse out a BLASTER environment setting
>with the getenv function. I am sucessful with that but I can't seem to be
>able to covert the values from the string to an integer or atom.
>I.E. "A255 I5 D3". I can parse out the 255, 5 and 3 but I can't convert them
>to integers (The ouput of the program is in text right now for debug purposes)
>
>
>include get.e
>global constant
>        TRUE = 1,
>        FALSE= 0,
>        FAIL = -1,
>        ENVIRONOK=1,
>        DMACHANNEL=2,
>        BASEPORT=3,
>        IRQNUMBER=4,
>        DSPVERSION=5
>
>function CheckBlasterEnviron()
>--See if the Blaster enviroment is set.
>object environ
> environ = getenv("BLASTER")
> if length(environ) = 0 then --trival case, exists but nothing there
>    return FAIL
> end if
> return environ
>end function
>
>function GetBlasterInfo()
>--get the Blaster Information and return a sequence with the information.
>sequence BlasterInfo, TempVal
>object env
>atom BasePort, IRQ
>integer IsMatch
>
>env = CheckBlasterEnviron()
>BlasterInfo = repeat(0,5)
>if atom(env) = -1 then
> return FAIL
>else
> IsMatch = 0
> IsMatch = match("A",env)
>    if IsMatch > 0 then
>    BlasterInfo[BASEPORT]=env[IsMatch+1..IsMatch+3]
>    IsMatch = 0
>    end if
> IsMatch = match("I",env)
>    if IsMatch > 0 then
>    BlasterInfo[IRQNUMBER]=env[IsMatch+1..IsMatch+1]
>    IsMatch = 0
>    end if
> IsMatch = match("D",env)
>    if IsMatch > 0 then
>    BlasterInfo[DMACHANNEL]=env[IsMatch+1..IsMatch+1]
>    IsMatch = 0
>    end if
> BlasterInfo[ENVIRONOK]=TRUE
> return BlasterInfo
>end if
>end function
>
>object a, c
>a=CheckBlasterEnviron()
>if atom(a) = FAIL then
>    puts(1,"Blaster not set")
>else
>    c=GetBlasterInfo()
>    puts(1,"Blaster String is " & a & "\n")
>    puts(1,"DMA Address is " & c[BASEPORT] & "\n")
>    puts(1,"IRQ is " & c[IRQNUMBER]  & "\n")
>    puts(1,"DMA Channel is " & c[DMACHANNEL] & "\n")
>end if
>abort(1)
>

I did some minor modif to your code and added to functions HexToDec() and AToI()


--=====================_850605912==_

include get.e
global constant
        TRUE = 1,
        FALSE= 0,
        FAIL = -1,
        ENVIRONOK=1,
        DMACHANNEL=2,
        BASEPORT=3,
        IRQNUMBER=4,
        DSPVERSION=5

function CheckBlasterEnviron()
--See if the Blaster enviroment is set.
object environ
 environ = getenv("BLASTER")
 if atom(environ) then
    return FAIL
 elsif length(environ) = 0 then --trivial case, exist but nothing there
    return FAIL
 else
   return environ
 end if
end function

function GetBlasterInfo()
--get the Blaster Information and return a sequence with the information.
sequence BlasterInfo, TempVal
object env
atom BasePort, IRQ
integer IsMatch

 env = CheckBlasterEnviron()
 BlasterInfo = repeat(0,5)
 if atom(env) then
  return FAIL
 else
  IsMatch = 0
  IsMatch = match("A",env)
  if IsMatch > 0 then
     BlasterInfo[BASEPORT]=env[IsMatch+1..IsMatch+3]
     IsMatch = 0
  end if
  IsMatch = match("I",env)
     if IsMatch > 0 then
     BlasterInfo[IRQNUMBER]=env[IsMatch+1..IsMatch+2] -- could be 2 digits
     if BlasterInfo[IRQNUMBER][2] = ' ' then
        BlasterInfo[IRQNUMBER] = {BlasterInfo[IRQNUMBER][1]}
     end if
     IsMatch = 0
    end if
  IsMatch = match("D",env)
     if IsMatch > 0 then
     BlasterInfo[DMACHANNEL]={env[IsMatch+1]}
     IsMatch = 0
     end if
  BlasterInfo[ENVIRONOK]=TRUE
  return BlasterInfo
 end if
end function


function HexToDec(sequence Hex)
-- convert hexadecimal to decimal
-- input: Hex is hexadecimal string to convert
-- output: decimal number or -1  if Hex is ill formed.
integer n
    n = 0
    for i = 1 to  length(Hex) do
        if Hex[i] >= '0' and Hex[i] <= '9' then
            n = n*16 + Hex[i] - '0'
        elsif Hex[i] >='A' and Hex[i] <= 'F' then
            n = n*16 + 10 + Hex[i] - 'A'
        else
          return -1 -- ill formed hex string
        end if
    end for
    return n
end function --HexToDec()

function AToI(sequence NbStr)
-- convert string to integer
-- input: NbStr numerical string
-- output: integer
integer n,sign
    if NbStr[1] = '-' then
        sign = 1
        n = 0
    else
        sign = 0
        n = NbStr[1] - '0'
    end if
    for i = 2 to length(NbStr) do
        n = n*10 + NbStr[i] - '0'
    end for
    if sign then
        return (-n)
    else
        return n
    end if
end function  -- AToI()

object a, c
a=CheckBlasterEnviron()
if atom(a) then
    puts(1,"Blaster not set")
else
    c=GetBlasterInfo()
    puts(1,"Blaster String is " & a & "\n")
    printf(1,"PORT Address is #%s (%d)\n",{c[BASEPORT],HexToDec(c[BASEPORT])})
    printf(1,"IRQ is %d\n",{AToI(c[IRQNUMBER])})
    printf(1,"DMA Channel is %d\n",{AToI(c[DMACHANNEL])})
end if
abort(1)


--=====================_850605912==_

Jacques Deschenes
Baie-Comeau, Quebec
Canada
desja at quebectel.com

--=====================_850605912==_--

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu