home *** CD-ROM | disk | FTP | other *** search
- (*************************************************************************
-
- :Program. ConfigDemo.mod
- :Contents. small demo for reading and writing iff-configuration files
- :Author. Hartmut Goebel [hG]
- :Copyright. Public Domain
- :Language. Oberon
- :Translator. Amiga Oberon V2.15
- :History. V1.0, 15 Apr 1992 [hG]
- :Support. Olfan `Olsen' Barthel: fragment from `term' source
- :Date. 19 Apr 1992 10:34:59
-
- *************************************************************************)
-
- MODULE ConfigDemo;
-
- IMPORT
- d := Dos,
- e := Exec,
- iff:= IFFParse,
- ol := OberonLib,
- req:= Requests,
- s := SYSTEM,
- str:= Strings;
-
- CONST
- NumConfigChunks = 4;
- Version = 200; Revision = 42;
-
- TYPE
- ChunkStops = ARRAY 2 * NumConfigChunks OF LONGINT;
-
- CONST
- TEST = s.VAL(LONGINT,"TEST"); (* global FORM-Type *)
- VERS = s.VAL(LONGINT,"VERS"); (* VersionInfo Follows *)
- CNFG = s.VAL(LONGINT,"CNFG"); (* NewConfig follows *)
- PORT = s.VAL(LONGINT,"PORT"); (* name of ARexx-Port *)
- SCRN = s.VAL(LONGINT,"SCRN"); (* screen name *)
- STRT = s.VAL(LONGINT,"STRT"); (* name of source file *)
-
- StopAt = ChunkStops( (* That ar all the chunks we wanna read *)
- TEST, CNFG,
- TEST, PORT,
- TEST, SCRN,
- TEST, STRT);
-
- TYPE
- Configuration * = STRUCT
- left*, top*, width*, height*: INTEGER;
- screenDepth*: INTEGER;
- aslLeft*, aslTop*, aslWidth*, aslHeight*: INTEGER;
- END;
-
- VersionInfoPtr = POINTER TO VersionInfo;
- VersionInfo = STRUCT
- ver, rev: INTEGER;
- END;
-
- CONST
- ThisVersion = VersionInfo(Version,Revision);
-
- StdConfig * = Configuration(
- 0,0,640,200, (* window edges *)
- 0, (* screendepth *)
- 20,20,100,200); (* asl edges *)
-
- VAR
- PortName *: e.STRPTR;
- ScreenName * : e.STRPTR;
- StartUpName *: e.STRPTR; (* for ARexx-Scripts *)
- Config *: Configuration;
-
-
- PROCEDURE WriteConfig * (Name: ARRAY OF CHAR): BOOLEAN; (* $CopyArrays- *)
- VAR
- handle: iff.IFFHandlePtr;
- success: BOOLEAN;
- len: LONGINT;
- BEGIN
- success := FALSE;
-
- handle := iff.AllocIFF();
- IF handle # NIL THEN
-
- handle.stream := s.VAL(s.ADDRESS,d.Open(Name,d.newFile));
- IF handle.stream # NIL THEN
-
- iff.InitIFFasDOS(handle);
- IF iff.OpenIFF(handle,iff.write) = 0 THEN
-
- (* Push outmost chunk onto stack *)
- IF iff.PushChunk(handle,TEST,iff.idFORM,iff.IFFSizeUnknown) = 0 THEN
-
- (* Add a version identifier *)
- IF iff.PushChunk(handle,0,VERS,iff.IFFSizeUnknown) = 0 THEN
- IF (iff.WriteChunkBytes(handle,ThisVersion,SIZE(ThisVersion)) = SIZE(ThisVersion))
- & (iff.PopChunk(handle) = 0) THEN
- success := TRUE;
- END;
- END;
-
- (* Push the config chunk on the stack *)
- IF success & (iff.PushChunk(handle,0,CNFG,iff.IFFSizeUnknown) = 0) THEN
- IF (iff.WriteChunkBytes(handle,Config,SIZE(Config)) = SIZE(Config))
- & (iff.PopChunk(handle) = 0) THEN
- success := TRUE;
- END;
- END;
-
- (* Now all the other chunks we wonna write *)
- IF success & (PortName # NIL) THEN
- IF (iff.PushChunk(handle,0,PORT,iff.IFFSizeUnknown) = 0) THEN
- len := str.Length(PortName^);
- IF (iff.WriteChunkBytes(handle,PortName^,len) # len)
- OR (iff.PopChunk(handle) # 0) THEN
- success := FALSE;
- END;
- END;
- END;
-
- IF success & (ScreenName # NIL) THEN
- IF (iff.PushChunk(handle,0,SCRN,iff.IFFSizeUnknown) = 0) THEN
- len := str.Length(ScreenName^);
- IF (iff.WriteChunkBytes(handle,ScreenName^,len) # len)
- OR (iff.PopChunk(handle) # 0) THEN
- success := FALSE;
- END;
- END;
- END;
-
- IF success & (StartUpName # NIL) THEN
- IF (iff.PushChunk(handle,0,STRT,iff.IFFSizeUnknown) = 0) THEN
- len := str.Length(StartUpName^);
- IF (iff.WriteChunkBytes(handle,StartUpName^,len) # len)
- OR (iff.PopChunk(handle) # 0) THEN
- success := FALSE;
- END;
- END;
- END;
-
- (* Seems that we're done, now try to pop the FORM chunk
- * and return.
- *)
- IF iff.PopChunk(handle) # 0 THEN
- success := FALSE;
- END;
- END;
-
- iff.CloseIFF(handle); (* Close the handle (flush any pending data). *)
- END;
-
- (* Close the DOS handle itself. *)
- IF ~d.Close(s.VAL(d.FileHandlePtr,handle.stream)) THEN
- success := FALSE;
- END;
- END;
-
- iff.FreeIFF(handle); (* And free the IFF handle. *)
- END;
-
- IF success & d.SetProtection(Name,LONGSET{d.execute}) THEN END;
- RETURN success;
- END WriteConfig;
-
-
- PROCEDURE ReadConfig * (Name: ARRAY OF CHAR): BOOLEAN; (* $CopyArrays- *)
- VAR
- handle: iff.IFFHandlePtr;
- success: BOOLEAN;
- size, err: LONGINT;
- prop: iff.StoredPropertyPtr;
- chunk: iff.ContextNodePtr;
- verInfo: VersionInfoPtr;
-
- PROCEDURE ReadIFFString (VAR to: e.STRPTR);
- VAR
- string: e.STRING;
- BEGIN
- success := FALSE;
-
- (* Read only as much char as in the chunk *)
- IF chunk.size < SIZE(e.STRING) THEN
- size := chunk.size;
- ELSE
- size := SIZE(e.STRING);
- END;
-
- (* The file read pointer is positioned just in front of the first data
- * to be read, so let's don't disappoint iffparse and read it.
- *)
- IF iff.ReadChunkBytes(handle,string,size) = size THEN
- ol.New(to,size+1);
- IF to # NIL THEN
- e.CopyMem(string,to^,size);
- success := TRUE;
- END;
- END;
- END ReadIFFString;
-
- BEGIN
- success := FALSE;
-
- handle := iff.AllocIFF();
- IF handle # NIL THEN
-
- handle.stream := s.VAL(s.ADDRESS,d.Open(Name,d.oldFile));
- IF handle.stream # NIL THEN
- iff.InitIFFasDOS(handle);
-
- IF iff.OpenIFF(handle,iff.read) = 0 THEN
-
- (* Collect version number ID if available *)
- IF iff.PropChunk(handle,TEST,VERS) = 0 THEN
-
- (* The following line tells iffparse to stop at the
- * very beginning of one of the config chunks contained in a
- * `TEST ' FORM chunk.
- *)
- IF iff.StopChunks(handle,StopAt,NumConfigChunks) = 0 THEN
-
- (* Parse the file... *)
- LOOP
- err := iff.ParseIFF(handle,iff.iffParseScan);
- CASE err OF
- |iff.IFFErrEOF: EXIT; (* finished *)
- |iff.IFFErrEOC: (* No action *)
- |0:
- chunk := iff.CurrentChunk(handle);
- IF (chunk # NIL) & (chunk.type = TEST) THEN
- CASE chunk.id OF
- |CNFG:
- prop := iff.FindProp(handle,TEST,VERS); (* Did we get a version ID? *)
- IF prop # NIL THEN
- (* Is it the file format we are able to read? *)
- verInfo := s.VAL(s.ADDRESS,prop.data);
- IF (verInfo.ver <= Version) & (verInfo.rev <= Revision) THEN
- IF chunk.size < SIZE(Config) THEN
- size := chunk.size;
- ELSE
- size := SIZE(Config);
- END;
- (* The file read pointer is positioned
- * just in front of the first data
- * to be read, so let's don't disappoint
- * iffparse and read it.
- *)
- IF iff.ReadChunkBytes(handle,Config,size) = size THEN
- success := TRUE; END;
- END;
- END;
- |PORT: ReadIFFString(PortName);
- |SCRN: ReadIFFString(ScreenName);
- |STRT: ReadIFFString(StartUpName);
- ELSE (* just ignore other Chunks *)
- END; (* CASE chunk.id *)
- END; (* IF chunk # NIL *)
- ELSE
- success := FALSE; EXIT; (* error uccured *)
- END; (* CASE err *)
- IF ~success THEN EXIT; END
- END; (* LOOP *)
-
- END; (* IF StopChunks *)
- END; (* IF PropChunk *)
-
- iff.CloseIFF(handle);
- END; (* IF OpenIFF *)
-
- IF ~d.Close(s.VAL(d.FileHandlePtr,handle.stream)) THEN
- success := FALSE; END;
-
- END; (* IF d.OPpen *)
-
- iff.FreeIFF(handle);
- END; (* IF AllocHandle *)
-
- RETURN success;
- END ReadConfig;
-
- BEGIN
- req.Assert(d.dos.lib.version>=36,"ConfigDemo needs AmigaOS 2.0 or higher!");
-
- PortName := s.ADR("ARexxPort");
- ScreenName := s.ADR("MyScreen");
- StartUpName := s.ADR("StartMeUp!!");
-
- IF WriteConfig("t:test.iff") THEN END;
-
- PortName := NIL; ScreenName := NIL; StartUpName := NIL;
-
- IF ReadConfig("t:test.iff") THEN
- d.PrintF("%s\n",PortName);
- d.PrintF("%s\n",ScreenName);
- d.PrintF("%s\n",StartUpName);
- ELSE
- IF d.PutStr("--> ReadError\n") = 0 THEN END;
- END;
- END ConfigDemo.
-
-