FB II Compiler

PG PRO

Debugging

Memory

System

Mathematics

Resources

Disk I/O

Windows

Controls

Menus

Mouse

Keyboard

Text

Fonts

Drawing

Sound

Clipboard

Printing

Communication

ASM

Made with FB

TEXT

Handle long strings


The following codes are my long string data solution. If err, return NIL handle. We don't have to check err every time, at least, and at last, only chech the handle is nil or not. (If nil, we got an err.)

LOCAL FN disposeh(hndl&)
  DIM state%

  LONG IF hndl&
    state% = FN HGETSTATE(hndl&)
    LONG IF state% AND _resource%
      CALL RELEASERESOURCE(hndl&)
    XELSE
      DEF DISPOSEH(hndl&)
    END IF
    hndl& = _nil
  END IF
END FN = hndl&

LOCAL FN handNnum(hndl&,num%)
  DIM osErr%
  DIM size&
  DIM state%

  LONG IF hndl&<>_nil
    state% = FN HGETSTATE(hndl&)
    size& = FN GETHANDLESIZE(hndl&)
    osErr% = FN HUNLOCK(hndl&)
    osErr% = FN SETHANDLESIZE(hndl&,size&+2)
    LONG IF osErr%=_noErr
      osErr% = FN HLOCK(hndl&)
      % [hndl&],num%
      osErr% = FN HSETSTATE(hndl&,state%)
    XELSE
      hndl& = FN disposeh(hndl&)
    END IF
  END IF
END FN = hndl&

LOCAL FN handNlnum(hndl&,num&)
  DIM osErr%
  DIM size&
  DIM state%

  LONG IF hndl&<>_nil
    state% = FN HGETSTATE(hndl&)
    size& = FN GETHANDLESIZE(hndl&)
    osErr% = FN HUNLOCK(hndl&)
    osErr% = FN SETHANDLESIZE(hndl&,size&+4)
    LONG IF osErr%=_noErr
      osErr% = FN HLOCK(hndl&)
      & [hndl&],num&
      osErr% = FN HSETSTATE(hndl&,state%)
    XELSE
      hndl& = FN disposeh(hndl&)
    END IF
  END IF
END FN = hndl&

LOCAL FN handNstr(hndl&,txt$)
  DIM osErr%
  DIM size&
  DIM state%

  LONG IF hndl&<>_nil
    state% = FN HGETSTATE(hndl&)
    size& = FN GETHANDLESIZE(hndl&)
    osErr% = FN HUNLOCK(hndl&)
    osErr% = FN SETHANDLESIZE(hndl&,size&+LEN(txt$))
    LONG IF osErr%=_noErr
      osErr% = FN HLOCK(hndl&)
      BLOCKMOVE @txt$+1,[hndl&]+size&,LEN(txt$)
      osErr% = FN HSETSTATE(hndl&,state%)
    XELSE
      hndl& = FN disposeh(hndl&)
    END IF
  END IF
END FN = hndl&

LOCAL FN strNhand(hndl&,txt$)
  DIM osErr%
  DIM size&
  DIM state%

  LONG IF hndl&<>_nil
    state% = FN HGETSTATE(hndl&)
    size& = FN GETHANDLESIZE(hndl&)
    osErr% = FN HUNLOCK(hndl&)
    osErr% = FN SETHANDLESIZE(hndl&,size&+LEN(txt$))
    LONG IF osErr%=_noErr
      osErr% = FN HLOCK(hndl&)
      BLOCKMOVE [hndl&],[hndl&]+LEN(txt$),size&
      BLOCKMOVE @txt$+1,[hndl&],LEN(txt$)
      osErr% = FN HSETSTATE(hndl&,state%)
    XELSE
      hndl& = FN disposeh(hndl&)
    END IF
  END IF
END FN = hndl&

LOCAL FN handNhand(aHndl&,bHndl&)
  DIM osErr%
  DIM aState%,bState%

  LONG IF aHndl&<>_nil AND bHndl&<>_nil
    aState% = FN HGETSTATE(aHndl&)
    bState% = FN HGETSTATE(bHndl&)
    osErr% = FN HUNLOCK(aHndl&)
    osErr% = FN HUNLOCK(bHndl&)
    osErr% = FN HANDANDHAND(aHndl&,bHndl&)
    LONG IF osErr% = _noErr
      osErr% = FN HSETSTATE(bHndl&,bState%)
    XELSE
'out of mem
      bHndl& = FN disposeh(bHndl&)
    END IF
    osErr% = FN HSETSTATE(aHndl&,aState%)
  END IF
END FN = bHndl&

LOCAL FN getItem(hndl&,item%,delimiter$)
  DIM osErr%
  DIM count%,sOfs&,eOfs&,size&,delLen%
  DIM newHndl&,state%

  newHndl& = _nil
  LONG IF hndl&<>_nil
    size& = FN GETHANDLESIZE(hndl&)
    delLen% = LEN(delimiter$)
    count% = 0
    eOfs& = -delLen%
    DO
      INC(count%)
      sOfs& = eOfs&+delLen%
      LONG IF sOfs&<=size&
        eOfs& = FN MUNGER(hndl&,sOfs&,@delimiter$+1,delLen%,_nil,_nil)
      XELSE
        eOfs& = -1
      END IF
    UNTIL count%=item% OR eOfs&<0

    LONG IF count%=item% AND eOfs&<0 AND item%<>1
      eOfs& = size&
    END IF

    LONG IF eOfs&>=0
      size& = eOfs&-sOfs&
      newHndl& = FN NEWHANDLE(size&)
      LONG IF newHndl&<>_nil
        state% = FN HGETSTATE(hndl&)
        osErr% = FN HLOCK(hndl&)
        osErr% = FN HLOCK(newHndl&)
        BLOCKMOVE [hndl&]+sOfs&,[newHndl&],size&
        osErr% = FN HUNLOCK(newHndl&)
        osErr% = FN HSETSTATE(hndl&,state%)
      END IF
    END IF
  END IF
END FN = newHndl&

LOCAL FN pickUp(hndl&,s$,e$)
  DIM temp&,result&

  temp& = FN getItem(hndl&,2,s$)
  result& = FN getItem(temp&,1,e$)
  temp& = FN disposeh(temp&)
END FN = result&

LOCAL FN replaceSS(hndl&,s$,t$)
  DIM osErr%
  DIM offset&
  DIM state%

  LONG IF hndl&<>_nil AND LEN(s$)>0
    offset& = 0
    state% = FN HGETSTATE(hndl&)
    osErr% = FN HUNLOCK(hndl&)
    DO
      offset& = FN MUNGER(hndl&,offset&,@s$+1,LEN(s$),@t$+1,LEN(t$))
    UNTIL offset&<0
    osErr% = FN HSETSTATE(hndl&,state%)
  END IF
END FN

LOCAL FN replaceSH(hndl&,s$,tHndl&)
  DIM osErr%
  DIM offset&
  DIM state%
  DIM tSize&,tState%

  LONG IF hndl&<>_nil AND LEN(s$)>0 AND tHndl&<>_nil
    offset& = 0
    state% = FN HGETSTATE(hndl&)
    osErr% = FN HUNLOCK(hndl&)
    tSize& = FN GETHANDLESIZE(tHndl&)
    tState% = FN HGETSTATE(tHndl&)
    osErr% = FN HLOCK(tHndl&)
    DO
      offset& = FN MUNGER(hndl&,offset&,@s$+1,LEN(s$),[tHndl&],tSize&)
    UNTIL offset&<0
    osErr% = FN HSETSTATE(hndl&,state%)
    osErr% = FN HSETSTATE(tHndl&,tState%)
  END IF
END FN

LOCAL FN str2hand(t$)
  DIM osErr%
  DIM hndl&

  hndl& = FN NEWHANDLE(LEN(t$))
  LONG IF hndl&
    osErr% = FN HLOCK(hndl&)
    BLOCKMOVE @t$+1,[hndl&],LEN(t$)
    osErr% = FN HUNLOCK(hndl&)
  END IF
END FN = hndl&

LOCAL FN hand2str$(hndl&,maxLen%)
  DIM osErr%
  DIM t$,size&,state%

  t$ = ""
  LONG IF hndl&<>_nil
    IF maxLen%>255 THEN maxLen% = 255
    size& = FN GETHANDLESIZE(hndl&)
    IF size&>maxLen% THEN size& = maxLen%
    state% = FN HGETSTATE(hndl&)
    osErr% = FN HLOCK(hndl&)
    BLOCKMOVE [hndl&],@t$+1,size&
    POKE @t$,size&
    osErr% = FN HSETSTATE(hndl&,state%)
  END IF
END FN = t$

Osamu