Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » static API

Credits go to gnolam for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2 
static API
karistouf
Member #5,126
October 2004
avatar

hello everybody... I have still a question that should be easy to answer for a lot of you ... ;-P

My app is static in its way of work ( working with dll , alllegro and windows.
But I m using
chdir("C:\\my_app_name\\saves\\folder_of_particuliar_saves");

I didn t figure out how to know wich is the hard disk ( "C:" )and the folder (\\somewhere\\my_app_name) if I let users install it anywhere on their computer.

Could someone explain me wich syntax to use to have the resident directory of installation? And how to navigate inside of it ?

I would like for example to let users use my app from an usb-key. This app is under windows, but cant be worked from emultaor under linux because of this particuliar point.
( for those who will have this suggestion: my app is communicating with hardware, a compilation under linux needs to rewrite a lot of code, so first I want it purely static)

Thanks for answer!

gnolam
Member #2,030
March 2002
avatar

guillermet christoph said:

My app is static in its way of work

You keep using that word. I do not think it means what you think it means. ;)

guillermet christoph said:

Could someone explain me wich syntax to use to have the resident directory of installation?

Just using a relative path works in itself (no chdirs are required), if the user starts the program by double-clicking the executable:

load_bitmap("foo.bmp", NULL); //load "foo.bmp" in the same directory as the exe
load_bitmap("bar/foo.bmp", NULL); //load "foo.bmp" in the subdirectory "bar"

(I'm using forward slashes here as they work in both Windows and UNIX)

But to be completely sure, you can combine get_executable_name(char *buf, int size);
with char *replace_filename(char *dest, const char *path, const char *filename, int size);

E.g.:

char path[512];
get_executable_name(path, sizeof(path));
replace_filename(path, path, "foo.bmp", sizeof(path));

load_bitmap(path, NULL);

[EDIT]
Someone must have lobotomized me in my sleep. Fixed the load_bitmap() call.

--
Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

ixilom
Member #7,167
April 2006
avatar

I believe the last row of code in gnolam's post should be

load_bitmap(path,NULL);

Maybe that was obvious to the OP though 8-)

___________________________________________
Democracy in Sweden? Not since 2008-Jun-18.
<someone> The lesbians next door bought me a rolex for my birthday.
<someone> I think they misunderstood when I said I wanna watch...

karistouf
Member #5,126
October 2004
avatar

ok... interresting...

on static definition: for me Blender ( on linux and win) is a static api: it has all the dll and drivers it needs to work. A program that need external library installed on the system is not static. But perhaps Im wrong ?

get_executable_name is good, is there any split function to navigate easely from folder up to folder down ?

my app is there : /MY_APP_FOLDER/BIN/my_app.exe
my bitmap are there: MY_APP_FOLDER/IMAGES

so i need to navigate easely...

thanks !!!

ixilom
Member #7,167
April 2006
avatar

Static to me is something that doesn't change, the opposite of dynamic.
A program that comes with its own DLL's and such I'd call "stand alone" rather than static.

As for getting different directory "levels", there is no such magic function AFAIK.
Shouldn't be too hard to make one though. Just some simple string management ;)

___________________________________________
Democracy in Sweden? Not since 2008-Jun-18.
<someone> The lesbians next door bought me a rolex for my birthday.
<someone> I think they misunderstood when I said I wanna watch...

karistouf
Member #5,126
October 2004
avatar

i hate string management .... nostring minded am I...;D

is there any function in mingw that do a replacement in a string of a certain keyword ( replace /bin by /saves ) ?

but thanks... really!

Kibiz0r
Member #6,203
September 2005
avatar

Sorry to post for no apparent reason, but...

Quote:

You keep using that word. I do not think it means what you think it means. ;)

I laughed. A lot.

karistouf
Member #5,126
October 2004
avatar

we agree that static is the good term when you do not install libraries on the system to run your app??????????????????????????
i have done some linuxing during two years and some apps were said static...
;D;D;D;D;D

ixilom
Member #7,167
April 2006
avatar

I believe they mean static as in the libraries were built into the executable itself, not requiring any external libraries.
However, if they deliver a bunch of libraries (located where the executable is), its not a static build. Merely a "stand alone".

The normal way of running Linux applications is to require the user to install the libraries that are needed, sharing the libraries with a bunch of other software.
This has been avoided in the most recent games(and some applications) and thus they come with their own libraries it should "just work" <tm>. No need for the gamer to hunt down a plethora of libraries he/she has never heard of. 8-)

___________________________________________
Democracy in Sweden? Not since 2008-Jun-18.
<someone> The lesbians next door bought me a rolex for my birthday.
<someone> I think they misunderstood when I said I wanna watch...

tobing
Member #5,213
November 2004
avatar

Quote:

we agree that static is the good term when you do not install libraries on the system to run your app

No. Static is the opposite of dynamic, and refers to how you use libraries. So I guess you think of applications that are statically linked, so there are no runtime dependencies to other libraries which may have to be installed as well. This would not be called 'static' however, but stand-alone or independent, or self-contained or whatever.

Edit: I also don't get why you use the term API. It's the shorthand for Application Programming Interface, and doesn't make sense the way you're using it...

Paul whoknows
Member #5,081
September 2004
avatar

The way you compile your program does not affect its path.

____

"The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner.

karistouf
Member #5,126
October 2004
avatar

ok lets stay statically linked.
i didn t know it was so important...
thanks for precision about api. I m in france and on forums we do not use with the same meanings english terms. There is always a kind of deformation...;D

LennyLen
Member #5,313
December 2004
avatar

Quote:

is there any function in mingw that do a replacement in a string of a certain keyword ( replace /bin by /saves ) ?

No, there isn't*. But you can use strstr() to find the first occurance of the substring you want to replace, then copy all of the proceeding string to a buffer, then strcat() what you want to replace it with, then copy the rest of the original string to the buffer.

* Not in C anyway. I don't know about C++.

edit:

Quote:

ok lets stay statically linked.

If you're using the Allegro dll, which I assume you are, since you mentined dlls in your original post, then you are dynamically linking, not statically linking (dll == Dynamic Link Library).

CGamesPlay
Member #2,559
July 2002
avatar

replace_filenamereplace_filename will change /bin to /saves, assuming the last component of the path name is /bin.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

LennyLen
Member #5,313
December 2004
avatar

Quote:

replace_filename will change /bin to /saves, assuming the last component of the path name is /bin.

That's not a MinGW (GCC) function though. :P

CGamesPlay
Member #2,559
July 2002
avatar

Sure, but this is obviously an Allegro question. See the second post in the thread :)

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

LennyLen
Member #5,313
December 2004
avatar

Quote:

See the second post in the thread

Nah, I'm busy looking at this part now:

Quote:

get_executable_name is good, is there any split function to navigate easely from folder up to folder down ?

my app is there : /MY_APP_FOLDER/BIN/my_app.exe
my bitmap are there: MY_APP_FOLDER/IMAGES

In DOS/Windows, ".." represents the parent directory, does this hold true for other OSes?

CGamesPlay
Member #2,559
July 2002
avatar

Yeah, it does. But you can't do MY_APP_FOLDER/BIN/my_app.exe/../../IMAGES, you have to get the directory name twice, then append the IMAGES part. I suppose you could get away with getting the directory name without a trailing slash and then calling replace_filename.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

karistouf
Member #5,126
October 2004
avatar

strstr used and used with strncat.
i arrived to this point but still little troubles with mingw and its way to access with "\\" .
and of course "\" is a special very special caracter...
so now i let this down and will try the magic ".."::)

BAF
Member #2,981
December 2002
avatar

Use / not \\. / will work on all platforms, plus you don't have to escape it. It also looks better because you don't have toe scape it.

karistouf
Member #5,126
October 2004
avatar

sorry but / is not working on my dev cpp + mingw32. Its like this...

But on the ".." I can say that simplest solutions are the best. It works now, I just have to adapt some few things....
so now its ok !!!

thank you
8-)

LennyLen
Member #5,313
December 2004
avatar

Quote:

sorry but / is not working on my dev cpp + mingw32. Its like this...

How did you use it? It should work fine.

eg:

output = fopen("../output.txt", "w");

karistouf
Member #5,126
October 2004
avatar

chdir ("..")
and I do not have to handle string funct...

what a pity that in mingw32 the main interresting function in libc are missing...

LennyLen
Member #5,313
December 2004
avatar

Quote:

chdir ("..")
and I do not have to handle string funct...

The following code worked perfectly for me with MinGW:

#include <stdio.h>
#include <unistd.h>

int main() {
  
  FILE *output;
  
  chdir("../files");
  output = fopen("test.txt", "w");
  fprintf(output, "Hello World!");
  
  return 0;
  
}

Quote:

what a pity that in mingw32 the main interresting function in libc are missing...

Such as?

Jonatan Hedborg
Member #4,886
July 2004
avatar

'/' works perfectly in Mingw32. There are no missing libc functions in mingw32 that I'm aware of...

 1   2 


Go to: