home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / opus / v5 / fd2module / fd2module.e < prev    next >
Text File  |  1996-11-11  |  7KB  |  208 lines

  1. /* Name : fd2module
  2. ** Version : v1.1
  3. ** Authors : original programming by Alex McCracken
  4. **           modified on 11.11.96 by Dominique Dutoit (ddutoit@arcadis.be)
  5. **
  6. ** Comments from Alex :
  7. ** This program is heavily based on Wouter van Oortmerssen's pragma2module.
  8. ** In fact about 90% of the code belongs to Wouter, so I claim no credit for
  9. ** this. However, since Wouter's praga2module works very well for files in
  10. ** the correct format, I must state that if this fails it is most probably my
  11. ** fault.  You may use this program as you see fit, however should it fail and
  12. ** eat your dog, cause your telly to explode, or cause any problems
  13. ** what-so-ever, I will not be held responsible.  In other word use this at
  14. ** your own risk.  I have made every effort to ensure it works, but I cannot
  15. ** guarantee to have found all the niggly little ones that plauge almost all
  16. ** programs.
  17. **
  18. ** Comments from Dom:
  19. ** - "_lib.fd" is gone away forever, really usefull with Opus5.
  20. ** - fd2module is also able to find the library name by himself if the lib name
  21. **   is in the first comment of the fd file.
  22. **
  23. ** Usage :
  24. ** The program in invoked by typing (CLI only):
  25. **     fd2module <libname>
  26. **
  27. ** where libname is the name of the fd file. (no more _lib.fd)
  28. ** This will produce a file <libname>.m .  At the moment the program echos the
  29. ** fd file as it reads it, but this may change in a future release.  You will
  30. ** need to give the program the name of the library explicitly, again this may
  31. ** change.
  32. **
  33. ** Distribution:
  34. ** Public domain, you can modify and distribute the codes again and again.
  35. */
  36.  
  37. /* FD2Module
  38. **   convert a library fd file to an E module.
  39. **   Usage: fd2module <file>
  40. **   converts <file.fd> to <file.m>
  41. */
  42.    
  43. ENUM INPUT_ERROR=10,OUTPUT_ERROR,FORMAT_ERROR
  44.  
  45. DEF cfh,efh,eof,done,
  46.     gotbase=FALSE,
  47.     gotlibname=FALSE,
  48.     public=TRUE,
  49.     offset=30,
  50.     cfile[200]:STRING,
  51.     efile[200]:STRING,
  52.     cstring[200]:STRING,
  53.     libstr[50]:STRING
  54.  
  55. PROC main()
  56.     DEF p
  57.  
  58.     StrCopy( cfile, arg, ALL)
  59.     p:= InStr( cfile, '_lib.fd')
  60.     IF ( p = -1 ) THEN p := InStr( cfile, '.fd' )
  61.     MidStr( efile, cfile, 0, p)
  62.     StrAdd( efile, '.m', ALL)
  63.     WriteF( 'Amiga E FD2Module\nconverting: "\s" to "\s"\n', cfile, efile)
  64.     IF ( cfh := Open( cfile, OLDFILE)) = 0 THEN closeAll(INPUT_ERROR)
  65.     IF ( efh := Open( efile, NEWFILE)) = 0 THEN closeAll(OUTPUT_ERROR)
  66.     REPEAT
  67.         eof := ReadStr(cfh,cstring)
  68.         done := convert(cstring)
  69.     UNTIL eof OR done
  70.     WriteF( 'last offset: -\d\n', offset)
  71.     Out( efh, $FF)
  72.     WriteF( 'Done.\n')
  73.     closeAll(0)
  74. ENDPROC
  75.  
  76. PROC closeAll(er)
  77.     IF ( cfh <> 0) THEN Close(cfh)
  78.     IF ( efh <> 0) THEN Close(efh)
  79.     SELECT er
  80.         CASE INPUT_ERROR;  WriteF( 'Could not open input file!\n')
  81.         CASE OUTPUT_ERROR; WriteF( 'Could not open output file!\n')
  82.         CASE FORMAT_ERROR; WriteF( 'Function definition file format error!\n')
  83.     ENDSELECT
  84.     CleanUp(er)
  85. ENDPROC
  86.  
  87. /* format of line to convert:
  88.    ##base _<Basename>
  89.      or
  90.    ##bias <offset>
  91.      or
  92.    ##public
  93.      or
  94.    ##private
  95.      or
  96.    ##end
  97.      or
  98.    * <comment> or <libname>
  99.      or
  100.    <funcname>(<paramlist>)(<reglist>)*/
  101.  
  102. PROC convert(str)
  103.  
  104.     DEF pos,pos2,off2,len,narg,a,empty,dstr[50]:STRING,basestr[50]:STRING,
  105.         funcstr[50]:STRING,regstr[20]:STRING,
  106.         tstr[80]:STRING,t2str[80]:STRING,t3str[80]:STRING,reg,check
  107.  
  108.     MidStr(tstr,str,TrimStr(str)-str,ALL)
  109.     LowerStr(tstr)
  110.     WriteF('\s\n',str)
  111.     IF StrCmp(tstr,'* "', STRLEN) AND ( gotlibname = FALSE )
  112.         pos:=STRLEN
  113.         pos2:=InStr(tstr,'.library"',0)
  114.         IF ( pos2 = -1 ) THEN pos2:=InStr(tstr,'.device"',0)
  115.         IF ( pos2 > -1 )
  116.             MidStr( libstr, str, 3, StrLen( str ) - 4 )
  117.             WriteF('Library will be: \s\n',libstr)
  118.             gotlibname := TRUE
  119.         ENDIF
  120.     ELSEIF StrCmp(tstr,'##base ',STRLEN) OR StrCmp(tstr,'##base\t',STRLEN)
  121.         pos:=STRLEN
  122.         pos2:=InStr(tstr,'_',0)
  123.         IF pos2=-1 THEN closeAll(FORMAT_ERROR)
  124.         IF gotbase=FALSE
  125.             gotbase:=TRUE
  126.             MidStr(basestr,str,(pos2+1),ALL)
  127.             LowerStr(basestr)
  128.             WriteF('Base will be: \s\n',basestr)
  129.             IF ( gotlibname = FALSE )
  130.                 WriteF('Correct name of this library (with the ".library" or ".device"):\n>')
  131.                 ReadStr(stdout,libstr)
  132.                 WriteF('Library will be: \s\n',libstr)
  133.             ENDIF
  134.             Write(efh,["EM","OD",6]:INT,6)
  135.             Write(efh,libstr,EstrLen(libstr)+1)
  136.             Write(efh,basestr,EstrLen(basestr)+1)
  137.         ENDIF
  138.     ELSEIF StrCmp(tstr,'##bias ',STRLEN) OR StrCmp(tstr,'##bias\t',STRLEN)
  139.         pos:=STRLEN
  140.         MidStr(t2str,tstr,pos,ALL)
  141.         pos2:=TrimStr(t2str)
  142.         MidStr(t3str,t2str,pos2-t2str,ALL)
  143.         off2:=Val(t3str,NIL)
  144.         IF off2=0 THEN closeAll(FORMAT_ERROR)
  145.         WHILE off2<>offset
  146.             Write(efh,'Dum',3)                     /* "empty function slots" */
  147.             Out(efh,16)
  148.             IF offset>off2 THEN closeAll(FORMAT_ERROR)
  149.             offset:=offset+6
  150.         ENDWHILE
  151.     ELSEIF StrCmp(tstr,'##private',ALL)
  152.         public:=FALSE
  153.     ELSEIF StrCmp(tstr,'##public',ALL)
  154.         public:=TRUE
  155.     ELSEIF StrCmp(tstr,'##end',ALL)
  156.         RETURN TRUE
  157.     ELSEIF StrCmp(tstr,'*',STRLEN)
  158.         NOP
  159.     ELSE
  160.         IF public
  161.             pos:=0
  162.             pos2:=InStr(str,'(',pos)
  163.             IF pos2=-1 THEN closeAll(FORMAT_ERROR)
  164.             MidStr(funcstr,str,pos,pos2-pos)
  165.             IF funcstr[0]>="a" THEN funcstr[0]:=funcstr[0]-32
  166.             IF funcstr[1]<"a" THEN funcstr[1]:=funcstr[1]+32
  167.             Write(efh,funcstr,EstrLen(funcstr))
  168.             pos:=pos2+1
  169.             pos2:=InStr(str,'(',pos)
  170.             IF pos2=-1 THEN closeAll(FORMAT_ERROR)
  171.             narg:=0
  172.             MidStr(dstr,str,pos2+1,ALL)
  173.             UpperStr(dstr)
  174.             WHILE StrCmp(dstr,')',1)=FALSE
  175.                 IF EstrLen(dstr)<2 THEN closeAll(FORMAT_ERROR)
  176.                 MidStr(regstr,dstr,0,2)
  177.                 IF StrCmp(regstr,'D',1) OR StrCmp(regstr,'A',1)
  178.                     IF StrCmp(regstr,'D',1)
  179.                         reg:=0
  180.                     ELSEIF StrCmp(regstr,'A',1)
  181.                         reg:=8
  182.                     ENDIF
  183.                     MidStr(regstr,regstr,1,ALL)
  184.                     reg:=reg+Val(regstr,{check})
  185.                     IF check<1 THEN closeAll(FORMAT_ERROR)
  186.                 ELSE
  187.                     closeAll(FORMAT_ERROR)
  188.                 ENDIF
  189.                 MidStr(dstr,dstr,2,ALL)
  190.                 IF StrCmp(dstr,',',1) OR StrCmp(dstr,'/',1)
  191.                     MidStr(dstr,dstr,1,ALL)
  192.                 ENDIF
  193.                 Out(efh,reg)
  194.                 INC narg
  195.             ENDWHILE
  196.             IF narg=0 THEN Out(efh,16)
  197.             offset:=offset+6
  198.         ELSE
  199.             Write(efh,'Dum',3)
  200.             Out(efh,16)
  201.             offset:=offset+6
  202.         ENDIF
  203.     ENDIF
  204. ENDPROC FALSE
  205.  
  206. version:
  207. CHAR'\0$VER: fd2module 1.1 (11.11.96) \tWritten by Dominique Dutoit (ddutoit@arcadis.be)\0'
  208.