IncFile() and IncArray() macros [Updated 22-1-2009]

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

If incfile.bi just seems to contain the text MZ, then it sounds like you've overwritten it with the contents of an executable file.
toneboy
Posts: 95
Joined: May 04, 2008 4:51
Location: Australia
Contact:

Post by toneboy »

so are you saying incfile.bi isnt supposed to be made from the code that is the macro? And that it comes with FB or something similar?
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

Incfile.bi is provided in this thread. But it sounds like the contents of your header file are actually that of an executable file. (They begin with "MZ".)

This could be due to a botched fbc command; or because you've tried to overwrite the file yourself, maybe under the impression that it's the .bi file itself that gets stored in your program; or because of some mysterious other reason I can't think of at the moment.

Try resaving incfile.bi with the contents provided in this thread, and try again. If that doesn't work, then I'm afraid I don't know. I haven't looked at this code in any depth recently...
toneboy
Posts: 95
Joined: May 04, 2008 4:51
Location: Australia
Contact:

Re: IncFile() and IncArray() macros [Updated 6-10-2008]

Post by toneboy »

Ok counting_pine, so just to clarify incfile.bi is in this thread and not as the following?

[quote= voodooattack
incfile.bi

Code: Select all

#IFNDEF INCFILE_BI
#DEFINE INCFILE_BI

#MACRO IncFileEx(label, file, sectionName, attr)
#if __FUNCTION__ <> "__FB_MAINPROC__"
   
    Dim label As Const Ubyte Ptr = Any
    Dim label##_len As Uinteger = Any
   
    #if __FB_DEBUG__
        asm jmp .LT_END_OF_FILE_##label##_DEBUG_JMP
    #else
        ' Switch to/Create the specified section
        #if attr = ""
            asm .section sectionName
        #else
            asm .section sectionName, attr
        #endif
    #endif
   
    ' Assign a label to the beginning of the file
    asm .LT_START_OF_FILE_##label#:
    asm __##label##__start = .
    ' Include the file
    asm .incbin ##file
    ' Mark the end of the the file
    asm __##label##__len = . - __##label##__start
    asm .LT_END_OF_FILE_##label:
    ' Pad it with a NULL Integer (harmless, yet useful for text files)
    asm .LONG 0
#if __FB_DEBUG__
    asm .LT_END_OF_FILE_##label##_DEBUG_JMP:
#else
    ' Switch back to the .text (code) section               
    asm .section .text
    asm .balign 16
#endif
    asm .LT_SKIP_FILE_##label:
    asm mov dword ptr [label], offset .LT_START_OF_FILE_##label#
    asm mov dword ptr [label##_len], offset __##label##__len
#else

    Extern "c"
    Extern label As Ubyte Ptr
    Extern label##_len As Uinteger
    End Extern
   
    #if __FB_DEBUG__
        asm jmp .LT_END_OF_FILE_##label##_DEBUG_JMP
    #else
        ' Switch to/create the specified section
        #if attr = ""
            asm .section sectionName
        #else
            asm .section sectionName, attr
        #endif
    #endif
   
    ' Assign a label to the beginning of the file
    asm .LT_START_OF_FILE_##label#:
    asm __##label##__start = .
    ' Include the file
    asm .incbin ##file
    ' Mark the end of the the file
    asm __##label##__len = . - __##label##__start
    asm .LT_END_OF_FILE_##label:
    ' Pad it with a NULL Integer (harmless, yet useful for text files)
    asm .LONG 0
    asm label:
    asm .int .LT_START_OF_FILE_##label#
    asm label##_len:
    asm .int __##label##__len
#if __FB_DEBUG__
    asm .LT_END_OF_FILE_##label##_DEBUG_JMP:
#else
    ' Switch back to the .text (code) section               
    asm .section .text
    asm .balign 16
#endif
    asm .LT_SKIP_FILE_##label:
#endif       
#endmacro

#macro IncFile(label, file)
    IncFileEx(label, file, .data, "")        'Use the .data (storage) section (SHARED)
#endmacro

#endif
[/quote]

because as i said, i compiled this code and made it .bi not .exe (as i thought that was how we were supposed to use it), not that I'm sure of how the actual embedding at compile-time works or is done either.


Thanks!
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

Yes, that should be a .bi, not a .exe, but it sounds like your original copy was overwritten somehow, so you might want to go into it and check that it does contain that text.
toneboy
Posts: 95
Joined: May 04, 2008 4:51
Location: Australia
Contact:

Post by toneboy »

hmm, alright, so where abouts in the freebasic folder would i find the original incfile.bi file?


Thanks
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

There is no "original" copy: grab the text from here and save it wherever you like, as long as FB can find it when you #include it.

I think we may be on different wavelengths here though, so if you have any further problems, you're going to have to give some background on what you've done and what you're trying to achieve.


EDIT: re-reading your first post, it looks like you're *compiling* the text provided, and naming the resulting executable as incfile.bi.
Don't compile the text, just save it directly.

".bi" header files are just text files containing BASIC code. They're a handy way to store definitions like function declares, constants and macros where they won't clutter up your program's code. #include pretty much just takes the text from the .bi file and acts like you'd pasted the whole thing in instead of just giving it the #include line.
toneboy
Posts: 95
Joined: May 04, 2008 4:51
Location: Australia
Contact:

Post by toneboy »

Thanks, i did, it seemed to work a treat, except when i run it it just creates a 'FBIDETEMP' or something and doesnt do anything or end :-|


Thanks
toneboy
Posts: 95
Joined: May 04, 2008 4:51
Location: Australia
Contact:

Post by toneboy »

Finally got it!!

Thanks for your help, i have no idea what was wrong, maybe something with my PC :-@
toneboy
Posts: 95
Joined: May 04, 2008 4:51
Location: Australia
Contact:

Post by toneboy »

One thing though, how would i go about accessing a single piece of data in it?

I've tried mucking around with pointers and accessing the address of the label but have had no luck, do you have to use Zstring during casting?

Thanks!
counting_pine
Site Admin
Posts: 6323
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs

Post by counting_pine »

As far as I can tell, the macro returns a ubyte ptr. You should be able to access its contents just by indexing it with [].
If you'd prefer to just access it like an array, it sounds like IncArray() will do what you want.
toneboy
Posts: 95
Joined: May 04, 2008 4:51
Location: Australia
Contact:

Post by toneboy »

oh i see what you mean, I'd never done pointer indexing before, thanks
toneboy
Posts: 95
Joined: May 04, 2008 4:51
Location: Australia
Contact:

Post by toneboy »

I am receiving and error saying:

E:\FBIDETEMP.asm: Assembler messages:
E:\FBIDETEMP.asm:107: Error: non-constant expression in ".if"

To do with a macro IncArrayEx().

Any tips?

Thanks a lot
voodooattack
Posts: 605
Joined: Feb 18, 2006 13:30
Location: Alexandria / Egypt
Contact:

Post by voodooattack »

toneboy wrote:I am receiving and error saying:

E:\FBIDETEMP.asm: Assembler messages:
E:\FBIDETEMP.asm:107: Error: non-constant expression in ".if"

To do with a macro IncArrayEx().

Any tips?

Thanks a lot
Fixed, thanks for bringing this up, looks like something changed in the compiler.
Macro updated, please copy it again from the first post (only supports byte arrays now).
toneboy
Posts: 95
Joined: May 04, 2008 4:51
Location: Australia
Contact:

Post by toneboy »

No, worries.

So would there be an limitations (like extended ascii characters) in what can be stored and used in the files if each element in the array is a byte?

I tried to just change the data type in the macro from byte to integer but in running the program the result was unsuccessful.
Post Reply