Example #1
0
SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
{
    SDL_RWops *rwops = NULL;
#ifdef HAVE_STDIO_H
    FILE *fp = NULL;
#endif
    if ( !file || !*file || !mode || !*mode ) {
        SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
        return NULL;
    }

#if defined(__WIN32__) && !defined(__SYMBIAN32__)
    rwops = SDL_AllocRW();
    if (!rwops)
        return NULL; /* SDL_SetError already setup by SDL_AllocRW() */
    if (win32_file_open(rwops,file,mode) < 0) {
        SDL_FreeRW(rwops);
        return NULL;
    }
    rwops->seek  = win32_file_seek;
    rwops->read  = win32_file_read;
    rwops->write = win32_file_write;
    rwops->close = win32_file_close;

#elif HAVE_STDIO_H

#ifdef __MACOS__
    {
        char *mpath = unix_to_mac(file);
        fp = fopen(mpath, mode);
        SDL_free(mpath);
    }
#else
    fp = fopen(file, mode);
#endif
    if ( fp == NULL ) {
        SDL_SetError("Couldn't open %s", file);
    } else {
        rwops = SDL_RWFromFP(fp, 1);
    }
#else
    SDL_SetError("SDL not compiled with stdio support");
#endif /* !HAVE_STDIO_H */

    return(rwops);
}
Example #2
0
SDL_RWops *SDL_RWFromFile(const char *file, const char *mode)
{
	SDL_RWops *rwops = NULL;
	
#if defined(__DREAMCAST__)
	file_t fd;
	int dc_mode = 0;
#elif defined(_HAVE_STDIO_H)
//#ifdef HAVE_STDIO_H
	FILE *fp = NULL;
#endif
	if ( !file || !*file || !mode || !*mode ) {
		SDL_SetError("SDL_RWFromFile(): No file or no mode specified");
		return NULL;
	}

#if defined(__WIN32__) && !defined(__SYMBIAN32__)
	rwops = SDL_AllocRW();
	if (!rwops)
		return NULL; /* SDL_SetError already setup by SDL_AllocRW() */
	if (win32_file_open(rwops,file,mode) < 0) {
		SDL_FreeRW(rwops);
		return NULL;
	}	
	rwops->seek  = win32_file_seek;
	rwops->read  = win32_file_read;
	rwops->write = win32_file_write;
	rwops->close = win32_file_close;

#elif defined(HAVE_STDIO_H)

#ifdef __DREAMCAST__

	if(SDL_strchr(mode,'r') != NULL) {
		dc_mode = O_RDONLY;
	}
	
	if(SDL_strchr(mode,'w') != NULL) {
		if(dc_mode & O_RDONLY) {
			dc_mode &= ~O_RDONLY;
			dc_mode = (O_RDWR | O_CREAT | O_TRUNC);
		} else {
			dc_mode = (O_WRONLY | O_CREAT | O_TRUNC);
		}
	}
	
	if(SDL_strchr(mode,'a') != NULL) {
		if(!(dc_mode & O_RDONLY))
			dc_mode |= (O_WRONLY | O_APPEND);
	}
	
	if(SDL_strchr(mode,'+') != NULL) {
		if((dc_mode & O_WRONLY) || (dc_mode & O_RDWR)) {
			dc_mode &= ~(O_CREAT | O_TRUNC);
		} else {
			dc_mode |= O_RDONLY;
		}
	}
	
	fd = fs_open(file, dc_mode);

	if ( fd == FILEHND_INVALID ) {
		SDL_SetError("Couldn't open %s", file);
	} else {
		rwops = SDL_RWFromFD(fd, 1);
	}
	
#else

#ifdef __MACOS__
	{
		char *mpath = unix_to_mac(file);
		fp = fopen(mpath, mode);
		SDL_free(mpath);
	}
#else
	fp = fopen(file, mode);
#endif
	if ( fp == NULL ) {
		SDL_SetError("Couldn't open %s", file);
	} else {
		rwops = SDL_RWFromFP(fp, 1);
	}
	
#endif /* __DREAMCAST__ */

#else
	SDL_SetError("SDL not compiled with stdio support");
#endif /* !HAVE_STDIO_H */

	return(rwops);
}