Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 We need some sort of library for general cross platform programming
Index -> General Programming
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
DtY




PostPosted: Sun Mar 14, 2010 2:39 pm   Post subject: We need some sort of library for general cross platform programming

Problem
Writing cross platform, even graphical, applications has become very easy thanks to libraries like SDL and wxWidgets, but one problem remains. External data files, every operating system puts them in different places.

On Windows, they usually sit next to the executable (and I'm not sure how they know where to find them if the executable is started from a different CWD), on Linux (and possibly other Unices) they sit in $PREFIX/share/$NAME, on Mac OS X they are in the application bundle, and the program can ask for access to the files.

The same problem occurs with user data, on Windows it's put somewhere in Documents and Settings, on Linux it's put in a dot folder in the user's home directory, on Mac OS X it is in ~/Library/Application Support/$NAME

I have a proposal to solve this problem, and maybe something like it already exists, if it does, I'd love to know.

Solution
A statically linked library that can direct applications to where to find data files, and where they can write files for user settings. This would be written in pure C, since that can be linked with C, C++, Objective C and possibly even C# (I don't know if C# can link with C).

There would actually be one version of this library for each platform, plus one for development. During development, you would want your data files in the same place as your source (or rather binaries), the development library would use a directory like ./Data and ./Settings, which would work on any platform, and be suitable for development. When you are ready for release, you would link with your platforms library, instead of the development library (which could be done with a command line switch in the build system, or equivalent).

Since the libraries all need to work the same, they would use the same header.

The functions included would be:
c:
//Used to set and retrieve the program name, so data is stored somewhere local to your program (using a static variable)
//Returns the name of the program, and only sets it if name != NULL
char *dataFiles_setApplicationName(const char *name);

//Stores the path to a data file or settings file in a buffer, up to a certain amount of characters
void dataFiles_fileName(const char *fname, char *buffer, unsigned int maxLen, bool settings);

//Opens a file directly, for convenience
FILE *dataFiles_fopen(const char *fname, const char *mode, bool settings);


The `settings` flag would be used to create a file that can be used for user specific settings. Data files (settings=false) would be global files that are not guaranteed to be writeable, settings files (settings=true) would be user local files that are guaranteed to be writable).

The development library would be implemented something like this, to give you an idea of what I mean, if you are still unsure (UNTESTED):
c:
#include <datafiles.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>

char *dataFiles_setApplicationName(const char *name) {
    static char *appName = malloc(1);

    if (name != NULL) {
        unsigned int len = strlen(name) + 1;
        free(appName);
        appName = malloc(len * sizeof(char));
        strcpy(appName, name);
    }
    return appName;
}

void dataFiles_fileName(const char *fname, char *buffer,
        unsigned int maxLen, bool settings) {
    if (settings)
        strcpy(buffer, "Settings/");
    else
        strcpy(buffer, "Data/");

    strncat(buffer, fname, maxLen-strlen(buffer));
}

FILE *dataFiles_fopen(const char *fname, const char *mode, bool settings) {
    char buffer[256];
    FILE *f;

    dataFiles_fileName(fname, buffer, settings);
    f = fopen(buffer, mode);
    return f;
}


---

So, first of all, this seems like something that should exist already, so if it does, I'd like to know.

But, if it doesn't, does this seem like a good idea, is there anything I overlooked?
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Sun Mar 14, 2010 3:15 pm   Post subject: RE:We need some sort of library for general cross platform programming

But that would make cross-platform development easier! We can't have that!

I'd like to see centralized application support on all OS's. Anyone who's ever tried uninstalling apps on mac knows what I mean. There's the app itself, then its system library files, user library files, other stuff and it really is just a pain. I want all of my app's files in a folder dedicated to that app, in the same location as the app itself. Windows has that (mostly) and I dunno about Linux, but I'd like to see it on every OS.
DtY




PostPosted: Sun Mar 14, 2010 3:54 pm   Post subject: RE:We need some sort of library for general cross platform programming

When I uninstall Mac apps, I don't bother cleaning it so that there's no trace, I just drop the bundle in the trash.

I think there's a program that will go and delete all files that the program you're removing created (it has user submitted profiles that list the files it creates). Putting everything with the application (except user specific stuff, imo) is a good idea though, but that would require a lot more mucking with the lower level stuff than I want to do.

Judging by your making it easier comment, I suppose you think it's a good idea?

And on Linux, stuff goes all over the place. I don't even know where to look if I want to find an application's content. Executables aren't even all in one place, on my Mac there are ten items in the path (tells the shell where to look for executables). Granted, there are reasons for each one to be separate.

(I just realized that I was talking about Linux, then gave my Mac as an example, but the Unix stuff is pretty much the same).
rdrake




PostPosted: Sun Mar 14, 2010 4:15 pm   Post subject: RE:We need some sort of library for general cross platform programming

DtY, your prayers have been answered with AppTrap. Courtesy of Gianni.
DtY




PostPosted: Sun Mar 14, 2010 5:32 pm   Post subject: Re: RE:We need some sort of library for general cross platform programming

rdrake @ Sun Mar 14, 2010 4:15 pm wrote:
DtY, your prayers have been answered with AppTrap. Courtesy of Gianni.
Ah, perfect for the lazy Mac user! Thanks for that
btiffin




PostPosted: Sun Mar 14, 2010 10:47 pm   Post subject: Re: We need some sort of library for general cross platform programming

DtY,

Nothing stopping someone from here at compsci.ca being the world famous author of the ubiquitous libcross.

Write it and they will come. Or not.

Cheers
DtY




PostPosted: Mon Mar 15, 2010 10:17 am   Post subject: Re: We need some sort of library for general cross platform programming

btiffin @ Sun Mar 14, 2010 10:47 pm wrote:
DtY,

Nothing stopping someone from here at compsci.ca being the world famous author of the ubiquitous libcross.

Write it and they will come. Or not.

Cheers
I actually started it last night, I just wanted to make sure that (a) It didn't already exist and (b) I wasn't overlooking something obvious.

I finished the development version already, and I'm going to work on the linux and mac versions today. If someone else wants to do the Windows version (and for any other platform), that would be great, if not, I'm going to try to get my XP virtual machine working.

If anyone is interested, the source will be on Github, and it's going to be public domain.
DtY




PostPosted: Fri Mar 19, 2010 11:32 am   Post subject: RE:We need some sort of library for general cross platform programming

Okay, I've got something up: http://github.com/jeffayle/libdatafiles

So far, Mac OS X works, and Linux works for settings. I'm not sure how to detect what the prefix is, I'd rather not have another function you have to call to set it. Any advice?
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> General Programming
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: