Exemplo n.º 1
0
int File_copyTo( const IFile* self, const IPath* targetPath, int force )
{
	bool status = 0;

	char* target = NULL;
	IFile* target_file = NULL;
	IDirectory* target_dir = new_Directory_path( targetPath );
	if ( Directory_exists( target_dir ) )
	{
		target = CharString_cat3( Path_getCommon( targetPath ), "/", Path_getBasename( self->path ) );
	} else {
		target = CharString_copy( Path_getCommon( targetPath ) );
	}

	target_file = new_File( target );
	if ( !File_exists( target_file ) || force )
	{
		if ( File_open( target_file, "w" ) )
		{
			byte buffer[256];
			int read;
		
			while ( (read = File_read( self, buffer, 256 )) )
			{
				File_write( target_file, buffer, read );
				status = 1;
			}
			if ( !status )
			{
				fprintf( stdout, "File.c::copyTo: could not copy data\n" );
			}
		}
		File_close( target_file );
	}		
	free_Directory( target_dir );
	free_File( target_file );
	free_CharString( target );	

	return status;
}
Exemplo n.º 2
0
// Determine which directory should store the user's configuration.
//
// For most Unix and Windows platforms:
// If a config file already exists in program_dir, it will return it in priority
// (Useful for development, and possibly for upgrading from DOS version)
// If the standard directory doesn't exist yet, this function will attempt 
// to create it ($(HOME)/.grafx2, or %APPDATA%\GrafX2)
// If it cannot be created, this function will return the executable's
// own directory.
// IN: The directory containing the executable
// OUT: Write into config_dir. Trailing / or \ is kept.
void Set_config_directory(const char * program_dir, char * config_dir)
{
  // AmigaOS4
  #if defined(__amigaos4__) || defined(__AROS__)
    strcpy(config_dir,"PROGDIR:");
  // GP2X
  #elif defined(__GP2X__) || defined(__WIZ__) || defined(__CAANOO__)
    // On the GP2X, the program is installed to the sdcard, and we don't want to mess with the system tree which is
    // on an internal flash chip. So, keep these settings locals.
    strcpy(config_dir,program_dir);
  #elif defined(__MINT__)  
    strcpy(config_dir,program_dir);
  #else
    char filename[MAX_PATH_CHARACTERS];

    // In priority: check root directory
    strcpy(config_dir, program_dir);
    // On all the remaining targets except OSX, the executable is in ./bin
    #if !defined(__macosx__)
    strcat(config_dir, "../");
    #endif
    strcpy(filename, config_dir);
    strcat(filename, CONFIG_FILENAME);

    if (!File_exists(filename))
    {
      char *config_parent_dir;
      #if defined(__WIN32__)
        // "%APPDATA%\GrafX2"
        const char* Config_SubDir = "GrafX2";
        config_parent_dir = getenv("APPDATA");
      #elif defined(__BEOS__) || defined(__HAIKU__)
        // "~/.grafx2", the BeOS way
        const char* Config_SubDir = ".grafx2";
        config_parent_dir = getenv("$HOME");
      #elif defined(__macosx__)
        // "~/Library/Preferences/com.googlecode.grafx2"
        const char* Config_SubDir = "Library/Preferences/com.googlecode.grafx2";
        config_parent_dir = getenv("HOME");
      #elif defined(__MINT__)
         const char* Config_SubDir = "";
         printf("GFX2.CFG not found in %s\n",filename);
         strcpy(config_parent_dir, config_dir);
      #else
        // "~/.grafx2"      
        const char* Config_SubDir = ".grafx2";
        config_parent_dir = getenv("HOME");
      #endif

      if (config_parent_dir && config_parent_dir[0]!='\0')
      {
        int size = strlen(config_parent_dir);
        strcpy(config_dir, config_parent_dir);
        if (config_parent_dir[size-1] != '\\' && config_parent_dir[size-1] != '/')
        {
          strcat(config_dir,PATH_SEPARATOR);
        }
        strcat(config_dir,Config_SubDir);
        if (Directory_exists(config_dir))
        {
          // Répertoire trouvé, ok
          strcat(config_dir,PATH_SEPARATOR);
        }
        else
        {
          // Tentative de création
          if (!Create_ConfigDirectory(config_dir)) 
          {
            // Réussi
            strcat(config_dir,PATH_SEPARATOR);
          }
          else
          {
            // Echec: on se rabat sur le repertoire de l'executable.
            strcpy(config_dir,program_dir);
            #if defined(__macosx__)
              strcat(config_dir, "../");
            #endif
          }
        }
      }
    }
  #endif
}