News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Please help me out with my program.

Started by S5, October 10, 2007, 08:04:25 PM

Previous topic - Next topic

S5

Hello, I've just started my first program with HLA, and I've seem to have run into a brick wall with it.

Basically, I've tried to make it open an existing file (.v3o). Pick out the bits that I want, and write them into a new file (.obj). But when I run it, I get an Conversion Error exception, and that's where I'm stuck. I'm moving through the .v3o file byte by byte, and examining it to see if I should write it to the .obj file, or leave it and carry on with the next byte (or that's the plan anyway).

Here is what I have so far.


program v3o2obj;
#include( "stdlib.hhf" )

static
inputhandle:dword;
outputhandle:dword;
prevbyte:byte;
counter:int32;
begin v3o2obj;
mov (0,counter);
fileio.open("swt_body.v3o", fileio.r);
mov( eax, inputhandle);
fileio.openNew("swt_body.obj");
mov( eax, outputhandle);

while(fileio.eof(inputhandle)= false) do

while(!fileio.eoln(inputhandle)) do

fileio.getb(inputhandle);
if (al = $44) then //$44
mov(al, prevbyte);
elseif
    (al = $5D) then //$5D
    mov($02, prevbyte);
elseif
    (al = $2C && prevbyte = $44) then
fileio.putb(outputhandle, $77);
mov($02, prevbyte);
mov(1,counter);
elseif
    (counter < 4 && al != $2C && prevbyte =$01) then
fileio.putb(outputhandle, al);
elseif
    (counter < 4 && al = $2C && prevbyte =$01) then
      inc(counter);
elseif
    (counter = 4 && al = $2C && prevbyte =$01) then
fileio.put(outputhandle,nl);
mov(0,counter);
mov($02, prevbyte);
else
  nop();
endif;
   endwhile;

endwhile;
fileio.close(inputhandle);
fileio.close(outputhandle);
end v3o2obj;


Here's an example of what is in the .v3o file :


...
[NUMPOLYGONS=340]
[NUMBONES=0]
[NUMMUSCLEANIMS=0]
[NUMMUSCLEFRAMES=0]

SRF, stw, 0, 0, 0, stw.dds, 517, 0, 0, 0, , 0, 0, 0, 0, 0, 0, 0, 0
D, -9657, -2447, 1690, -210, -90, -114, 865, 444, 0, 0, 0, 0
D, -9940, 2430, 4266, -238, 80, 43, 694, 235, 0, 0, 0, 0
D, -8586, 1916, 6498, -173, 53, 179, 712, 29, 0, 0, 0, 0
D, 8794, 2976, 3653, 124, 119, 189, 44, 60, 0, 0, 0, 0
...


I'm trying to go through each line, see if it starts with 'D'. If it does,  print 'v' into the .obj file in place of the 'D', and write the first three sets of numbers in each line into the .obj file too, minus the commas.

So basically in the .obj file, I'll have :


...
v -9657 -2447 1690
v -9940 2430 4266
v -8586 1916 6498
v 8794 2976 3653
...


Thank you for any pointers you can give me.  :bg

Sevag.K

Is this for an exercise to learn the fileio library or for your own personal use?

It's not clear what your code is doing.  For example, prevbyte will never equal to $01, so none of the conditional code that depends on this will execute.

Personally, I would use a memory mapped file or treat the whole thing like a string and use string functions.
If you have downloaded HIDE, you can make use of hidelib which has a buffer class that makes this kind of thing easy.

If you must use fileio routines, just grab one line at a time and use low level scanning functions to scan it and HLA string functions to extract sub strings to write to the destination file.

Here is an example of what your program might look like if you used the buffer class in HIDE

program temp;

#includeonce ("stdlib.hhf")
#includeonce ("hide/hidelib.hhf")

?@nodisplay := true;
?@noalignstack := true;

storage
buf :tBuffer;
dest :tBuffer;
indexA :dword;
indexB :dword;

static
s :str.strvar(1000);

begin temp;

// create a dest buffer
dest.create();

// create a buffer and load 'swt_body.v3o into it
buf.create( "swt_body.v3o" );
buf.cursorToBOF();
forever
breakif( buf.checkEOF() );
// advance cursor to next "D," in file
if( buf.searchf( "D,") != -1) then

// skip over "D,"
buf.cursorRight( 2 );
mov( eax, indexA );

// now find 3 commas
mov( 3, ecx );
_loop:
buf.searchf( "," );
buf.cursorRight( 1 ); // must skip over the comma
dec( ecx );
jnz _loop;

// eax now contains the end index
// we don't want to skip over the last comma!
dec( eax );
mov( eax, indexB );

// set up a line in the dest buffer
dest.insert( "v " );
buf.extractToBuffer( indexA, indexB, dest );
dest.append( nl );

// go on to next line
buf.nextLine();

endif;


endfor;

dest.toFile( "swt_body.obj" );

end temp;



Don't try to build the above program if you don't have HIDE installed since HLA doesn't come with hidelib or its headers.

Randall Hyde

Quote from: S5 on October 10, 2007, 08:04:25 PM
Hello, I've just started my first program with HLA, and I've seem to have run into a brick wall with it.

Basically, I've tried to make it open an existing file (.v3o). Pick out the bits that I want, and write them into a new file (.obj). But when I run it, I get an Conversion Error exception, and that's where I'm stuck. I'm moving through the .v3o file byte by byte, and examining it to see if I should write it to the .obj file, or leave it and carry on with the next byte (or that's the plan anyway).

Here is what I have so far.



fileio.getb(inputhandle);


Here's an example of what is in the .v3o file :



SRF, stw, 0, 0, 0, stw.dds, 517, 0, 0, 0, , 0, 0, 0, 0, 0, 0, 0, 0
D, -9657, -2447, 1690, -210, -90, -114, 865, 444, 0, 0, 0, 0
D, -9940, 2430, 4266, -238, 80, 43, 694, 235, 0, 0, 0, 0
D, -8586, 1916, 6498, -173, 53, 179, 712, 29, 0, 0, 0, 0
D, 8794, 2976, 3653, 124, 119, 189, 44, 60, 0, 0, 0, 0
...



fileio.getb reads 8-bit *hexadecimal* values (unsigned). The file above looks to me like it contains signed decimal integer values, which are typically *much* larger than what will fit in eight bits. I haven't looked closely enough to see if you're properly skipping the "SRF, std, stw.dds, and D entries, but if getb hits one of those, or the '-', that will certainly cause a conversion error. Of course, once you get past that, you'll get "ValueOutOfRange" errors because many of the values above will be too large for eight bits when you try to read them as eight-bit integers. I think you want to call geti32 or some comparable routine to process these numeric values.
hLater,
Randy Hyde

S5

Thanks for your replies, and sorry for not replying sooner. I've been busy in work, and haven't had time to do much with my computer lately.

QuoteIs this for an exercise to learn the fileio library or for your own personal use?

Basically, I was trying to convert some game (Emergency3) files from their .v3o format to .obj format. I did start doing this manually with notepad since the files are ASCII files anyway, but if the v3o files were big files it took a long time to do. So, I thought I'd have a go at making a program that would do it for me. Plus, I'd get to grips with using the fileio library. So to answer your question, a bit of both really.

I've ended up with something similar to what I've done before, but altered some bits of it, thanks to your help.

program v3o2obj;
#include( "stdlib.hhf" )

static
inputhandle:dword;
outputhandle:dword;
prevbyte:char;
counter:int32;
begin v3o2obj;
mov (0,counter);
fileio.open("swt_body.v3o", fileio.r);
mov( eax, inputhandle);
fileio.openNew("swt_body.obj");
mov( eax, outputhandle);

while(fileio.eof(inputhandle)= false) do

while(!fileio.eoln(inputhandle)) do

fileio.getc(inputhandle);
if (al = 'D') then //look for D.
mov(al, prevbyte);
elseif
    (al = ']') then //look for ].
    mov('@', prevbyte);
elseif
    (al = ',' && prevbyte = 'D') then
fileio.putc(outputhandle, 'v'); //replace "D," with 'v'
mov('#', prevbyte);
mov(1,counter);
elseif
    (counter < 4 && al != ',' && prevbyte ='#') then
fileio.putc(outputhandle, al);
elseif
    (counter < 4 && al = ',' && prevbyte ='#') then
      inc(counter);
elseif
    (counter = 4 && al = ',' && prevbyte ='#') then
fileio.put(outputhandle,nl); //after 4th comma put a newline.
mov(0,counter);
mov('@', prevbyte);
else
  nop();
endif;
   endwhile;

endwhile;
fileio.close(inputhandle);
fileio.close(outputhandle);
end v3o2obj;


I used the @ and # characters as something to put in prevbyte, so they didn't keep adding 1 to the counter everytime it found a comma, and also after it finds the last close square bracket in the file which is just before the first D entries. The @ and # characters don't appear anywhere in the v3o files, so it's safe to use those.