Пример #1
0
static mrb_value
org_name_getter(mrb_state *mrb, mrb_value self)
{
  return mrb_str_new_cstr(mrb, al_get_org_name());
}
Пример #2
0
/* _al_win_get_path:
 *  Returns full path to various system and user diretories
 */
ALLEGRO_PATH *_al_win_get_path(int id)
{
    char path[MAX_PATH];
    wchar_t pathw[MAX_PATH];
    ALLEGRO_USTR *pathu;
    uint32_t csidl = 0;
    HRESULT ret = 0;
    ALLEGRO_PATH *cisdl_path = NULL;

    switch (id) {
    case ALLEGRO_TEMP_PATH: {
        /* Check: TMP, TMPDIR, TEMP or TEMPDIR */

        DWORD ret = GetTempPathW(MAX_PATH, pathw);
        if (ret > MAX_PATH) {
            /* should this ever happen, windows is more broken than I ever thought */
            return NULL;
        }
        pathu = al_ustr_new_from_utf16(pathw);
        al_ustr_to_buffer(pathu, path, sizeof path);
        al_ustr_free(pathu);
        return al_create_path_for_directory(path);

    }
    break;

    case ALLEGRO_RESOURCES_PATH: { /* where the program is in */
        HANDLE process = GetCurrentProcess();
        char *ptr;

        GetModuleFileNameExW(process, NULL, pathw, MAX_PATH);
        pathu = al_ustr_new_from_utf16(pathw);
        al_ustr_to_buffer(pathu, path, sizeof path);
        al_ustr_free(pathu);
        ptr = strrchr(path, '\\');
        if (!ptr) { /* shouldn't happen */
            return NULL;
        }

        /* chop off everything including and after the last slash */
        /* should this not chop the slash? */
        *ptr = '\0';

        return al_create_path_for_directory(path);
    }
    break;

    case ALLEGRO_USER_DATA_PATH: /* CSIDL_APPDATA */
    case ALLEGRO_USER_SETTINGS_PATH:
        csidl = CSIDL_APPDATA;
        break;

    case ALLEGRO_USER_HOME_PATH: /* CSIDL_PROFILE */
        csidl = CSIDL_PROFILE;
        break;

    case ALLEGRO_USER_DOCUMENTS_PATH: /* CSIDL_PERSONAL */
        csidl = CSIDL_PERSONAL;
        break;

    case ALLEGRO_EXENAME_PATH: { /* full path to the exe including its name */
        HANDLE process = GetCurrentProcess();

        GetModuleFileNameExW(process, NULL, pathw, MAX_PATH);
        pathu = al_ustr_new_from_utf16(pathw);
        al_ustr_to_buffer(pathu, path, sizeof path);
        al_ustr_free(pathu);

        return al_create_path(path);
    }
    break;

    default:
        return NULL;
    }

    ret = SHGetFolderPathW(NULL, csidl, NULL, SHGFP_TYPE_CURRENT, pathw);
    if (ret != S_OK) {
        return NULL;
    }

    pathu = al_ustr_new_from_utf16(pathw);
    al_ustr_to_buffer(pathu, path, sizeof path);
    al_ustr_free(pathu);

    cisdl_path = al_create_path_for_directory(path);
    if (!cisdl_path)
        return NULL;

    if (csidl == CSIDL_APPDATA) {
        const char *org_name = al_get_org_name();
        const char *app_name = al_get_app_name();

        if (!app_name || !app_name[0]) {
            /* this shouldn't ever happen. */
            al_destroy_path(cisdl_path);
            return NULL;
        }

        if (org_name && org_name[0]) {
            al_append_path_component(cisdl_path, org_name);
        }

        al_append_path_component(cisdl_path, app_name);
    }

    return cisdl_path;
}
ALLEGRO_PATH *_al_unix_get_path(int id)
{
   switch (id) {
      case ALLEGRO_TEMP_PATH: {
         /* Check: TMP, TMPDIR, TEMP or TEMPDIR */
         char *envs[] = { "TMP", "TMPDIR", "TEMP", "TEMPDIR", NULL};
         uint32_t i = 0;
         for (; envs[i] != NULL; ++i) {
            char *tmp = getenv(envs[i]);
            if (tmp) {
               return al_create_path_for_directory(tmp);
            }
         }

         /* next try: /tmp /var/tmp /usr/tmp */
         char *paths[] = { "/tmp/", "/var/tmp/", "/usr/tmp/", NULL };
         for (i=0; paths[i] != NULL; ++i) {
            ALLEGRO_FS_ENTRY *fse = al_create_fs_entry(paths[i]);
            bool found = (al_get_fs_entry_mode(fse) & ALLEGRO_FILEMODE_ISDIR) != 0;
            al_destroy_fs_entry(fse);
            if (found) {
               return al_create_path_for_directory(paths[i]);
            }
         }

         /* Give up? */
         return NULL;
      } break;

      case ALLEGRO_RESOURCES_PATH: {
         ALLEGRO_PATH *exe = get_executable_name();
         exe = follow_symlinks(exe);
         al_set_path_filename(exe, NULL);
         return exe;

      } break;

      case ALLEGRO_USER_DATA_PATH:
      case ALLEGRO_USER_SETTINGS_PATH: {
         ALLEGRO_PATH *local_path = NULL;
         const char *org_name = al_get_org_name();
         const char *app_name = al_get_app_name();
         
         /* to avoid writing directly into the user's directory, require at least an app name */
         if (!app_name)
            return NULL;
         
         /* find the appropriate path from the xdg environment variables, if possible */
         if (id == ALLEGRO_USER_DATA_PATH) {
            const char *xdg_data_home = getenv("XDG_DATA_HOME");
            local_path = al_create_path_for_directory(xdg_data_home ? xdg_data_home : ".local/share");
         }
         else {
            const char *xdg_config_home = getenv("XDG_CONFIG_HOME");
            local_path = al_create_path_for_directory(xdg_config_home ? xdg_config_home : ".config");
         }
         
         if (!local_path) 
            return NULL;
         
         /* if the path is relative, prepend the user's home directory */
         if (al_path_cstr(local_path, '/')[0] != '/') {
            ALLEGRO_PATH *home_path = _unix_find_home();
            if (!home_path)
               return NULL;
            
            al_rebase_path(home_path, local_path);
            al_destroy_path(home_path);
         }

         /* only add org name if not blank */
         if (org_name && org_name[0]) {              
            al_append_path_component(local_path, al_get_org_name());
         }
         
         al_append_path_component(local_path, al_get_app_name());

        return local_path;
      } break;

      case ALLEGRO_USER_HOME_PATH:
         return _unix_find_home();
         
      case ALLEGRO_USER_DOCUMENTS_PATH: {
         ALLEGRO_PATH *local_path = _get_xdg_path("DOCUMENTS");
         return local_path ? local_path : _unix_find_home();
      } break;

      case ALLEGRO_EXENAME_PATH:
         return get_executable_name();
         break;

      default:
         return NULL;
   }

   return NULL;
}