Advertisement
retroman

Untitled

Jul 30th, 2024
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #cmdline "-gen gcc -O 1"
  2.  
  3. #ifndef NULL
  4.   const NULL = cast(any ptr,0)
  5. #endif
  6.  
  7. /'
  8. desktop\ldcad\shadow
  9. desktop\ldcad\ldraw\parts
  10.  
  11. 1 <colour> x y z a b c d e f g h i <file>
  12. 2 <colour> x1 y1 z1 x2 y2 z2
  13. 3 <colour> x1 y1 z1 x2 y2 z2 x3 y3 z3
  14. 4 <colour> x1 y1 z1 x2 y2 z2 x3 y3 z3 x4 y4 z4
  15. 5 <colour> x1 y1 z1 x2 y2 z2 x3 y3 z3 x4 y4 z4
  16. '/
  17.  
  18. 'dim shared as string sFilenames
  19. 'redim shared as long aDatFiles()
  20.  
  21. type LineType1Struct          'line type 1
  22.    as single fX,fY,fZ,fA,fB,fC,fD,fE,fF,fG,fH,fI
  23.    as ulong  lFileOffset
  24. end type
  25. type LineType2Struct          'line type 2
  26.    as single fX1,fY1,fZ1,fX2,Y2,fZ2
  27. end type
  28. type LineType3Struct          'line type 3
  29.    as single fX1,fY1,fZ1,fX2,Y2,fZ2,fX3,fY3,fZ3
  30. end type
  31. type LineType4Struct          'line type 4
  32.    as single fX1,fY1,fZ1,fX2,Y2,fZ2,fX3,fY3,fZ3,fX4,fY4,fZ4
  33. end type
  34. type LineType5Struct          'line type 5
  35.    as single fX1,fY1,fZ1,fX2,Y2,fZ2,fX3,fY3,fZ3,fX4,fY4,fZ4
  36. end type
  37.  
  38. type PartStruct
  39.    bType      as ubyte   'type ID (line/primitves  
  40.    union                 'flags
  41.       bFlags  as ubyte   'optional flags/bitfield???
  42.    end union  
  43.    union                 'color/data
  44.       wColour as ushort
  45.       wData   as ushort
  46.    end union
  47.    union                 '> type specific data
  48.       _1 as LineType1Struct
  49.       _2 as LineType2Struct
  50.       _3 as LineType3Struct
  51.       _4 as LineType4Struct
  52.       _5 as LineType5Struct      
  53.    end union  
  54. end type
  55.  
  56. type DATFile
  57.   iFilenameOffset as long                  'offset for the file name string
  58.   iPartCount      as long                  'number of parts in this file
  59.   dim as PartStruct tParts( (1 shl 25)-1 ) 'maximum number of parts (dynamic)
  60. end type
  61.  
  62. function ReadInt( pFile as ubyte ptr , byref iInt as long ) as long
  63.    dim as long iResu = 0, iRead, iHasDigits=0  
  64.    do
  65.       select case pFile[iRead]
  66.       case asc("0") to asc("9")      'add a digit to the number
  67.          iResu=iResu*10+(pFile[iRead]-asc("0"))
  68.          iHasDigits = 1
  69.       case asc(" "),9                'skip spaces/tab
  70.          if iHasDigits then exit do
  71.       case asc(!"\r")               'skip the \r in case EOL is \r\n
  72.          rem nothing to do here
  73.       case asc(!"!\n"),0  'if it's EOL/EOF then we assume it was read 0
  74.          iHasDigits = 1 : exit do
  75.       case else
  76.          exit do
  77.       end select
  78.       iRead += 1
  79.    loop  
  80.    'we're done processing digits, but did we read a number at all?
  81.    if iHasDigits=0 then iInt=pFile[iRead] : return -1
  82.    iInt = iResu
  83.    return iRead
  84. end function
  85.    
  86. function LoadFile( sFile as string , sFilename as string = "" ) as DATFile ptr
  87.    
  88.    #macro CheckError(_s)
  89.       if iResu<0 then
  90.          print _s " error reading '"+sFilename+"' at line " & iLineNum
  91.          iFailed = 1 : exit do
  92.       end if
  93.    #endmacro
  94.    #macro NextLine()
  95.       while *pFile <> asc(!"\n") andalso *pfile : pFile += 1 : wend
  96.       if *pFile=0 then exit do 'last line of file so we're done SUCCESS
  97.       iLineNum += 1 : pFile += 1 : continue do 'now it point to the being of next line
  98.    #endmacro
  99.      
  100.    dim as long iLastPart=0 , iLimitParts=-1 , iFailed=0, iLineNum = 1
  101.    dim as long iType = any , iResu = any
  102.    var pFile = cast(ubyte ptr, strptr(sFile)) 'pointer to the file
  103.    dim as DATFile ptr pT = NULL 'pointer to the file structure in memory
  104.    #define PartsToBytes(_N) (offsetof(DATFile,tParts(0))+(_N)*sizeof(PartStruct))
  105.    
  106.    do
  107.       if iLastPart > iLimitParts then 'allocate more entries if necessary
  108.          iLimitParts += 4096 'we increase the allocation every N parts        
  109.          var pNew = reallocate( pT , PartsToBytes(iLimitParts+1) )
  110.          if pNew=NULL then
  111.             print "Failed to allocate memory to load file"
  112.             iFailed = 1 : exit do 'gives up
  113.          end if
  114.          pT = pNew
  115.       end if
  116.       'at this point we should assume we are at the begin of a line so we get a line type
  117.       iResu = ReadInt( pFile , iType )
  118.       CheckError( "Syntax" ) 'failed to read the line type integer?
  119.       pFile += iResu 'advancing to the next component
  120.       select case iType 'which line type is it?      
  121.       case 0 to 5 'ignore if comment OR empty line and advance to next line        
  122.          NextLine()
  123.       end select
  124.       sleep : system
  125.    loop
  126.    
  127.    'clean-up
  128.    if iFailed then 'clean-up in case of faillure
  129.       if pT then deallocate(pT): pT=NULL 'deallocate previous buffer
  130.    else
  131.       pT->iPartCount = iLastPart
  132.       pT = reallocate( pT , PartsToBytes(pT->iPartCount) )
  133.       print iLineNum & " lines and " & pT->iPartCount & " parts were read"
  134.    end if
  135.    
  136.    return pT
  137.    
  138. end function
  139.  
  140. #define EOL !"\n"
  141. dim as string sModel = _
  142. "1 4 0 0 0 1 0 0 0 1 0 0 0 1 3024.dat"          EOL _
  143. "1 1 0 8 0 1 0 0 0 1 0 0 0 1 3024.dat"          EOL _
  144. "1 2 0 16 0 1 0 0 0 1 0 0 0 1 3005.dat"         EOL _
  145. "1 2 0 -2 -21 1 0 0 0 1 0 0 0 1 63710p01.dat"
  146.  
  147. print LoadFile( sModel , "MyModel.ldr" )
  148.  
  149. sleep
  150.  
  151.  
  152.  
  153. dim as double dTIME = timer
  154. var f = freefile()
  155. if open("C:\Users\kris\Desktop\n\datsville_rev002.013_inlined_n_boxed_f.mpd" for input as #f) then
  156.    print "Failed to open file": sleep:system
  157.  end if
  158.  dim as uinteger uFileSize = lof(f)
  159.  print csng(uFileSize/(1024*1024));"mb"
  160. #if 0
  161. dim as string sLine
  162. while not eof(f)
  163.    line input #f, sLine
  164.    'parse them
  165. wend
  166. #endif
  167. dim as string sFile = string( lof(f) , 0 )
  168. get #f,,sFile
  169. #if 0
  170.    var iLineStart = 1
  171.    do
  172.       var iPos = instr( iLineStart , sFile , !"\n" )  
  173.       if iPos=0 then exit do
  174.       iLineStart = iPos+1
  175.    loop
  176. #endif
  177.  
  178. dim as long iLineCount
  179. for N as long = 0 to uFileSize-1
  180.    select case sFile[N]
  181.    case asc(!"\n")
  182.       iLineCount += 1
  183.    end select
  184. next N
  185. print timer-dTIME
  186. sleep
  187.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement