#135404 - Takaishi_Takeru - Mon Jul 23, 2007 4:24 am
Hi.
I want to know if its possible to store a string into an array.
(This is for NDS dev, im trying to read a file line by line, and store each line into the array.....)
Like this one:
circle(0,649,CR1_Sprite,AROR_Sprite,112,48);
And then i need to execute the code in the array...
Its possible, or is there a better way to store (200-300) lines like that one, and then execute them?
Thanks. =3
#135412 - sgeos - Mon Jul 23, 2007 5:33 am
Precompiling is genarally better. How you do it depends on your scripts. You could use an array of structs and do something like this:
Code: |
struct a
{
void (*f)(int a, int b, int c, int d);
int a;
int b;
int c;
int d;
} a;
void run(struct a pStruct)
{
// load from struct for clarity
void (*f)(int a, int b, int c, int d) = pStruct->f;
int a = pStruct->a;
int b = pStruct->b;
int c = pStruct->c;
int d = pStruct->d;
// execute
f(a,b,c,d);
}
void run_many(struct a *pList)
{
int i;
for (i = 0; NULL != pList[i]->f; i++)
run( &(pList[i]) );
}
// data looks like this
// you need functions that act
draw(int a, int b, int c, int d) { /* do something */ };
line(int a, int b, int c, int d) { /* do something */ };
fill(int a, int b, int c, int d) { /* do something */ };
// and a "script" held in an array
// use something like perl to convert a text file script into an array
struct a list[] =
{
{draw,1,2,SOLID,0},
{line,7,7,DOTS, 4},
{fill,5,1,RED, 1},
{NULL,0,0,0,0}
};
// run it like this
run_many(list); |
This code is untested, so there is probably a minor error that will prevent it from compiling.
-Brendan
#135504 - Takaishi_Takeru - Tue Jul 24, 2007 12:25 am
Thanks sgeos, but is there a way yo do this without precompiling?
Or can i read a line from the text file and then execute it? (without ussing arrays)...or somethin like that?
Thanks.
#135506 - calcprogrammer1 - Tue Jul 24, 2007 1:03 am
I'm looking at the same sort of thing. I want to make a file browser, but I'd like to be able to store the names of each file and directory to an array, which would be used to generate the GUI. Is there a better way to do this, or can I use an array of strings?
_________________
DS Firmware 1, Datel Games n' Music card / Chism's FW hacked GBA MP v2 CF
There's no place like 127.0.0.1.
#135520 - sajiimori - Tue Jul 24, 2007 4:36 am
Takaishi-san,
A program that executes source code (in a particular language) is called an interpreter for that language. If you are wanting to execute C source code without compiling it, you must write a C interpreter. However, this is unusual because C is not very appropriate as an interpreted language, although many scripting languages resemble C superficially.
Typical interpreters almost always translate the source code into a more compact form before starting to execute it, even if the translation step is immediately followed by execution (rather than being done much earlier). Separating the translation and execution steps tends to simplify the interpreter program.
If you can perform the translation step during the build process, that would eliminate the need for your ROM to contain a translator. You only need a translator in the ROM if the program will be dealing with unknown source code, e.g. code entered by the user.
Have you ever written a text adventure? If so, you've written an interpreter: The user enters a command, then your program interprets it and executes some action. The only difference between a text adventure and what you're describing is where the source code is coming from: the keyboard versus a text file.
And if you've never written a text adventure, try it out on your PC. In fact, prototype everything on your PC before doing it on a handheld system -- it's much easier to work with.
calcprogrammer1, storing lists of filenames as an array of strings sounds good.
#135523 - calcprogrammer1 - Tue Jul 24, 2007 5:01 am
It sounded good, I made an array of structs instead, so that I could store the names and stats, but I have a compiling issue, check my post in the DS Development forum if you could...
_________________
DS Firmware 1, Datel Games n' Music card / Chism's FW hacked GBA MP v2 CF
There's no place like 127.0.0.1.
#135524 - Takaishi_Takeru - Tue Jul 24, 2007 5:04 am
mm, the code on the text is declared on the compiled code, the text have only the call to the object.
The code is entered by the user, but it wont be anything else but that object.
But maybe i could read the data form the file, and the compiled code puts the data on the object?
Thanks for your replay =3
#135533 - sgeos - Tue Jul 24, 2007 7:30 am
You should split things that stay the same from user input.
It is quite possible to collect user input using precompiled scripts.
All you have to do is change internal state variables. (Note that these may be pointers.)
Even random (numbers) can be thrown into your scripts- just change an internal variable to some random value.
To use the same array of structs format:
Code: |
// prototypes
void getColor(void);
void draw(void);
// script
struct command_item list
{
{getUserColor},
{draw},
{NULL}
};
// module variables
int mMyColor;
// functions
int getColor(void)
{
return mMyColor;
}
void setColor(int pMyColor)
{
mMyColor = pMyColor;
}
void getUserColor(void)
{
if ( press(A) )
setColor(RED);
else if ( press(B) )
setColor(GREEN);
else
setColor(BLUE);
}
void draw(void)
{
switch ( getColor() )
{
case RED:
drawRed();
case GREEN:
drawGreen();
case BLUE:
drawBlue();
default:
drawWhite();
}
} |
What are you trying to do?
-Brendan
#135556 - Takaishi_Takeru - Tue Jul 24, 2007 3:54 pm
I just need to execute the code which is readed from the text file.
The code on the text have the same format, and the user will change that file.
The text file have like 400 lines like this one:
circle(0,649,CR1_Sprite,AROR_Sprite,112,48);
but maybe to read it on the code i could use something like:
c, 0, 649, cr1_sprite, aror_sprite, 112, 48;
.....using fseek( ?), serching for "," to separate each part.
But im not sure how to use fseek. Or how to make the code to decide how to separate each part of the text file on each variable on the code...
Thanks again.
#135570 - sajiimori - Tue Jul 24, 2007 6:43 pm
If the code has a complex format (with many symbols, like in C), your interpreter will be more complex, but maybe it will be easier for the user to write code.
Try using the simple comma-separated syntax first, because it's easier for you to interpret. Later you can get more complex.
As I said earlier, you should write the code for your PC first, before doing anything on GBA or DS. Try implementing this function, and use it to write the rest of your program:
Code: |
// This reads a token from a file and stores it in a char array.
// Use fgetc to read from the source, and stop when you see a comma.
// Each time you call this function, it will read the next token.
// The function returns 1 if if successful, or 0 for fail (like end-of-file).
int readToken(FILE* source, char* dest, int maxLength);
|
Edit: Here's an example of using it.
Code: |
#define MAX_TOKEN_LENGTH 64
void test()
{
int result;
FILE* source = fopen(...);
char token[MAX_TOKEN_LENGTH];
result = readToken(source, token, MAX_TOKEN_LENGTH);
if(result == 0)
{
printf("First token not found.\n");
return;
}
printf("The first token was %s.\n", token);
result = readToken(source, token, MAX_TOKEN_LENGTH);
if(result == 0)
{
printf("Second token not found.\n");
return;
}
printf("The second token was %s.\n", token);
fclose(source);
}
|