program ToFlat
* To convert a dynamic information file to a fixed flat file format.
***
LastUpdated... = "Rev: 11:08 14JUN93 PROG_ANDREW MAPS.PROD 177 Z PROGS>ANDREW>BP>TOFLAT"
* Revision Log
* Who..... When.... Why................................................
* Andrew   15Feb93  Initial coding.
*
***

dim Record(200)

crt "[TOFLAT (c) 1993 Andrew McLaughlin. All rights reserved.]"
      Parms = upcase(trim(@SENTENCE))
      convert " " to @FM in Parms
      FileName = Parms<2>
      if not(FileName) then
         crt "Usage: ToFlat filename   (toflat)"
         stop
      end
      crt "Processing dynamic file ":FileName:" into flat form."
      crt "Flat file name will be ":FileName:".FLAT in the current account."
      gosub OpenFiles
      gosub BuildDict
      gosub ReportFileFormat
      gosub BuildFlatFile
      return

OpenFiles:
      open "",FileName to File else
         stop "Unable to open the ":FileName:" file.  (toflat)"
      end
      open "DICT",FileName to DictFile else
         stop "Unable to open the DICT ":FileName:" file.  (toflat)"
      end
      open "&UFD&" to UFDFile else
         stop "Unable to open &UFD& file. (ToFlat)"
      end
      openseq "&UFD&",FileName:".FLAT" to SeqFile else
         if status() gt 0 then
            stop "Unable to open &UFD&>":FileName:".FLAT. Status() = ":Status():"  (toflat)"
         end
      end
      weofseq SeqFile
      return

BuildDict:
      Size = ""
      Conv = ""
      Format = ""
      Name = ""
      crt "Building structure information from dictionary... (one 'd' equals one record)"
      select DictFile
      loop
         readnext DictName else exit
         read DictRec from DictFile, DictName else continue
         if DictRec<1>[1,1] eq "D" then
            Sized = len(fmt(" ",DictRec<5>))
            Attrib = DictRec<2> + 1
            if Sized gt Size<Attrib> then
               Size<Attrib> = Sized
               Conv<Attrib> = DictRec<3>
               Name<Attrib> = DictName
               Format<Attrib> = DictRec<5>
            end
            crt "d":Attrib:
         end
      repeat
      crt
      TotalSize = sum(Size)
      return

BuildFlatFile:
      select File
      Fields = dcount(Name, @FM)
      crt "Building flat file... (one * equals one record)"
      loop
         readnext RecordName else exit
         matread Record from File, RecordName else continue
         for i = 199 to 1 step -1
            Record(i+1) = Record(i)
         next i
         Record(1) = RecordName
         Values = 1
         for i = 2 to Fields
            if len(Record(i)) gt 0 then
               cValues = dcount(Record(i),@VM)
               Values = if cValues gt Values then cValues else Values
            end
         next i
         for v = 1 to Values
            SubValues = 1
            for i = 2 to Fields
               if len(Record(1)<1,v>) gt 0 then
                  cSubValues = dcount(Record(i)<1,v>,@SM)
                  SubValues = if cSubValues gt SubValues then cSubValues else SubValues
               end
            next i
            for s = 1 to SubValues
               Line = fmt(oconv(Record(1)<1,1>,Conv<1>),Format<1>) [1,Size<1>]
               for f = 2 to Fields
                  if Name<f> then  ;* Skip undefined fields
                     Line := fmt(oconv(Record(f)<1,v,s>,Conv<f>),Format<f>) [1,Size<f>]
                  end
               next f
               crt "+":
               writeseq Line to SeqFile else continue
            next s
         next v
         crt "*":
      repeat
      crt
      return

CountValues:
      return

CountSubValues:
      return

ReportFileFormat:
      execute "SETPTR ,,,,,,BRIEF,BANNER ":FileName:",FORM XLAND"
      crt "Building format report and copybook... (one * equals one attribute)"
      printer on
      Fields = dcount(Name,@FM)
      print "Fld ":fmt("Field Name","'.'L#32"):" Format...."
      CopyBook = space(6):"* Copybook for ":FileName:" file. ":TotalSize:" bytes."
      CopyBook<-1> = space(7):"01  ":FileName
      for i = 1 to Fields
         PL = ""
         if i = 1 then
            PL = "Key ":fmt(Name<i>,"L#32"):" ":fmt(Format<i>,"R#10")
            CopyBook<-1> = space(11):"03  ":fmt(FileName:".KEY","L#40"):" PIC X(":Size<i>:")."
         end else
            PL = fmt(i-1,"R#3"):" ":fmt(Name<i>,"L#32"):" ":fmt(Format<i>,"R#10")
            if Name<i> then
               CopyBook<-1> = space(11):"03  ":fmt(FileName:".":Name<i>,"L#40"):" PIC X(":Size<i>:")."
            end
         end
         print PL
         crt "*":
      next i
      crt
      printer off
      printer close
      write CopyBook to UFDFile, FileName:".COPY"
      crt FileName:" flat file format report has been spooled to XLAND."
      crt "A copybook record ":FileName:".COPY has been saved in the current directory."
      return

   end
