Re: A89: Re: Re: Custom Variables
[Prev][Next][Index][Thread]
Re: A89: Re: Re: Custom Variables
Hi!
> I'd say look at Zeljko Juric's docs on TI VAT functions. I've
> found nothing really too difficult about making the transition from
> using ANSI file functions to VAT functions after skimming the docs.
Yes, it is straightforward. Principally, to create a file you need to
do the following:
1) Create a new VAT symbol using SymAdd;
2) Allocate a space for the variable using HeapAlloc;
3) Dereference the symbol to get a pointer to the VAT entry, then
store the handle returned by HeapAlloc to it;
4) Put the actual file length in first two bytes of allocated
space.
For anybody interested, I included a simple demo which creates a
string file (I use this internally), but it is easy to adapt to
any file type. Note that used method is not the best: it initially
allocates as many space as avaliable, then relocates the space on
necessary size on closing, but it is worth to look on it. Note
also that CreateFile may be even simpler if you want to use it
using CreateFile($(example)) instead of CreateFile("example"),
i.e. if you avoid usage of ANSI strings:
#include <nostub.h>
#include <alloc.h>
#include <vat.h>
int _ti89;
HANDLE CreateFile(char *FileName)
// Returns a handle, H_NULL in case of error
{
HANDLE h;
SYM_ENTRY *sym_entry;
char str[30],*sptr=str;
*sptr=0; while(*++sptr=*FileName++);
if(!(h=HeapAlloc(HeapMax()))) return H_NULL;
if(!(sym_entry=DerefSym(SymAdd(sptr)))) return H_NULL;
*(long*)HLock(sym_entry->handle=h)=0x00010000;
return h;
}
void AppendCharToFile(HANDLE h,unsigned char c)
{
char *base=HeapDeref(h);
unsigned len=*(unsigned*)base;
if(len>HeapSize(h)-10) return;
*(unsigned*)base=len+1;
base[len+2]=c;
}
void AppendBlockToFile(HANDLE h,void *addr,unsigned int len)
{
unsigned int i;
for(i=len;i;i--) AppendCharToFile(h,*((char*)addr)++);
}
void CloseFile(HANDLE h)
{
AppendCharToFile(h,0); AppendCharToFile(h,0x2D);
HeapUnlock(h);
HeapRealloc(h,*(unsigned*)HeapDeref(h)+3);
}
void _main(void)
{
static char s[]="Hello world!";
HANDLE h;
h=CreateFile("example");
AppendBlockToFile(h,s,12);
CloseFile(h);
}
Zeljko Juric
Follow-Ups: