Page 1 of 1

Problem with Globals not updating. Am I missing something?

Posted: Mon Jul 07, 2003 7:24 pm
by Tipperton
In main file:

Code: Select all

Global BuffLen.l

XIncludeFile "BuffProcs.pb"

ReadBuff()
WriteBuff()
End
In BuffProcs.pb:

Code: Select all

Procedure ReadBuff()
  BuffLen=1000
EndProcedure

Procedure WriteBuff()
  ReadBuff()
  Test=BuffLen
EndProcedure
Here's the problem I'm having. ReadBuff works fine. But when it finishes and returns to the main program BuffLen no longer has the value that ReadBuff gave it. Also in WriteBuff ReadBuff is called and works fine. But when it returns to WriteBuff, again the value given to BuffLen isn't holding.

It's as if the variables are defaulting to Protected which is not what I want.

Any help would be appreaciated.

Re: Problem with Globals not updating. Am I missing somethin

Posted: Mon Jul 07, 2003 8:34 pm
by ebs
I assume you mean XIncludeFile, since there is no XInclude statement.

The code you showed works just the way I would expect. If you insert a few Debug statements to see what's going on:

Code: Select all

Global BuffLen.l

XInclude "BuffProcs.pb"

Debug "BuffLen before ReadBuff() = " + Str(BuffLen)
ReadBuff()
Debug "BuffLen after ReadBuff() = " + Str(BuffLen)
WriteBuff()
Debug "BuffLen after WriteBuff() = " + Str(BuffLen)
End

Code: Select all

Procedure ReadBuff()
  BuffLen=1000
EndProcedure

Procedure WriteBuff()
  ReadBuff()
  Test=BuffLen
  Debug "Test in WriteBuff() = " + Str(Test)
EndProcedure
You get

Code: Select all

BuffLen before ReadBuff() = 0
BuffLen after ReadBuff() = 1000
Test in WriteBuff() = 1000
BuffLen after WriteBuff() = 1000
which indicates that the global variable is getting (and holding) the value of 1000 correctly.

Posted: Mon Jul 07, 2003 8:50 pm
by Tipperton
Hmm... Well that isn't the actual code, just a simplified version for illistration and maybe that's the problem, that it's something in the actual code causing it. So...

In main file:

Code: Select all

;/ CRC Test file generator

Global BuffLen.l
Global BuffOffset.l
Global FileOffset.l
Global *IBuff.b

XIncludeFile "Common.pb"
XIncludeFile "Functions.pb"

*IBuff=AllocateMemory(0,65536,0)
If Not(*IBuff)
  DisplayError("Could not allocate buffer memory.")
  End
Else
  ClearBuff()
EndIf

Open_winMain()

SetGadgetText(#strIFName,"G:\FFA\UPCD\20020509\UPDATA.TXT")
SetGadgetText(#strOFName,"G:\FFA\UPCD\20020509\UPDATA.OUT")

;DebugLevel 2

Repeat
  
  Event = WaitWindowEvent()
  
  If Event = #PB_EventGadget
    
    GadgetID = EventGadgetID()
    
    If GadgetID = #btnIFNBrowse
      SetGadgetText(#strIFName,OpenFileRequester("Choose test file","","Text files (*.txt)|*.txt|All files (*.*|*.*",1))
      
    ElseIf GadgetID = #btnOFNBrowse
      SetGadgetText(#strOFName,SaveFileRequester("Save test data file","","Output files (*.out)|*.out|Text files (*.txt)|*.txt|All files (*.*|*.*",1))
      
    ElseIf GadgetID = #btnStart
      If ReadFile(0,GetGadgetText(#strIFName))
        CallDebugger
        ReadLine(0)
        If CreateFile(1,GetGadgetText(#strOFName))
          DisableGadget(#btnStart,1)
          LineNum=0
          OBuff.s=""
          SetGadgetText(#strLines,StrU(LineNum,#Long))
          While BuffOffset<BuffLen And ~Eof(0)
            UseFile(0)
            Debug "Percent done "+StrF(Loc()/Lof()*100),1
            SetGadgetState(#barProgress,Round(Loc()/Lof()*100,1))
            ILine.s=ReadLine(0)
            Debug ILine,2
            If Len(ILine)
              OLine.s=CleanUPRecord(ILine)
              If Len(OLine)
                OBuff+RSet(Hex(CRC32Fingerprint(@OLine,Len(OLine))),8,"0")+Chr(13)+Chr(10)
                If Len(OBuff)>60000
                  UseFile(1)
                  WriteString(OBuff)
                  OBuff=""
                EndIf
              EndIf
              LineNum+1
            EndIf
            If MOD(LineNum,100)=0
              SetGadgetText(#strLines,StrU(LineNum,#Long))
            EndIf
            DoEvents()
          Wend
          CloseFile(0)
          CloseFile(1)
          End
        Else
          DisplayError("Could not create output file.")
          CloseFile(0)
        EndIf
      Else
        DisplayError("Could not open input file.")
      EndIf
    EndIf
    
  EndIf
  
Until Event = #PB_EventCloseWindow

FreeMemory(0)

End
In Functions.pb:

Code: Select all

Procedure.b LoadBuffer(FNum.l)
  ClearBuff()
  If ~Eof(FNum)
    UseFile(FNum)
    FileSeek(FileOffset)
    BuffLen=ReadData(*IBuff,65536)
    If BuffLen=65536
      BuffLen=TruncateBuff()
    EndIf
    FileOffset+BuffLen
    BuffOffset=0
    ClearCRLF()
    ProcedureReturn #True
  Else
    ProcedureReturn #False
  EndIf
EndProcedure

Procedure.s ReadLine(FNum.l)
  If BuffOffset>=BuffLen Or Not(BuffLen)
    If Not(LoadBuffer(FNum))
      ProcedureReturn ""
    EndIf
  EndIf
  Debug *IBuff
  Result.s=PeekS(*IBuff+BuffOffset)
  BuffOffset+Len(Result)
  While BuffOffset<BuffLen And Not(PeekB(*IBuff+BuffOffset))
    BuffOffset+1
  Wend
  ProcedureReturn Result
EndProcedure
Here's the problem I'm having. LoadBuff. But when it finishes and returns to ReadLine, BuffLen no longer has the value that LoadBuff gave it. And BuffLen's value never gets back to the main program.

ClearBuff just fills the buffer with zeros. TruncateBuff searches for the last occurance of either CR or LF and sets the buffer length to that. ClearCRLF searches the buffer and replaces all occurances of CR and LF with zeros (makes using PeekS easier).

It's as if the variables are defaulting to Protected which is not what I want.

Any help would be appreaciated.

Posted: Mon Jul 07, 2003 10:12 pm
by ebs
Just a hunch, since I don't have all the code or your files:

BuffLen is set in only two places in the LoadBuffer() routine.
Check its value after the ReadData() and TruncateBuff() calls.
Does the correct value come back from TruncateBuff()?

Posted: Mon Jul 28, 2003 6:03 pm
by Tipperton
I did finally figure out the problem...

Two very simiar procedures so I just copied and pasted...

Problem turned out to be I forgot to delete the lines that reset the global values from the second procedure.... :oops: