NetSurf
findfile.c
Go to the documentation of this file.
1/*
2 * Copyright 2010 Ole Loots <ole@monochrom.net>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <unistd.h>
20
21#include "utils/log.h"
22#include "utils/corestrings.h"
23
24#include "atari/gemtk/gemtk.h"
25#include "atari/findfile.h"
26#include "atari/gui.h"
27#include "atari/osspec.h"
28
29char * local_file_to_url( const char * filename )
30{
31 #define BACKSLASH 0x5C
32 char * url;
33
34 NSLOG(netsurf, INFO, "in: %s", filename);
35
36 if( strlen(filename) <= 2){
37 return( NULL );
38 }
39
40 char * fname_local = malloc( strlen(filename)+1 );
41 char * start = (char*)fname_local;
42 strcpy( start, filename );
43
44 /* convert backslashes: */
45 for( unsigned int i=0; i<strlen(start); i++ ){
46 if( start[i] == BACKSLASH ){
47 start[i] = '/';
48 }
49 }
50
51 // TODO: make file path absolute if it isn't yet.
52 url = malloc( strlen(start) + FILE_SCHEME_PREFIX_LEN + 1);
53 strcpy(url, FILE_SCHEME_PREFIX );
54 strcat(url, start );
55
56 free(fname_local);
57
58 NSLOG(netsurf, INFO, "out: %s", url);
59
60 return( url );
61 #undef BACKSLASH
62}
63
64
65/**
66 * Locate a shared resource file by searching known places in order.
67 * Search order is: ./, NETSURF_GEM_RESPATH, ./$HOME/.netsurf/, $NETSURFRES/
68 * (where NETSURFRES is an environment variable)
69 *
70 * \param buf buffer to write to. must be at least PATH_MAX chars
71 * \param filename file to look for
72 * \param def default to return if file not found
73 * \return buf
74 *
75 */
76#ifndef NETSURF_GEM_RESPATH
77 #define NETSURF_GEM_RESPATH "./res/"
78#endif
79
80char * atari_find_resource(char *buf, const char *filename, const char *def)
81{
82 char *cdir = NULL;
83 char t[PATH_MAX];
84 NSLOG(netsurf, INFO, "%s (def: %s)", filename, def);
85 strcpy(t, NETSURF_GEM_RESPATH);
86 strcat(t, filename);
87 NSLOG(netsurf, INFO, "checking %s", (char *)&t);
88 if (gemdos_realpath(t, buf) != NULL) {
89 if (access(buf, R_OK) == 0) {
90 return buf;
91 }
92 }
93 strcpy(t, "./");
94 strcat(t, filename);
95 NSLOG(netsurf, INFO, "checking %s", (char *)&t);
96 if (gemdos_realpath(t, buf) != NULL) {
97 if (access(buf, R_OK) == 0) {
98 return buf;
99 }
100 }
101
102 cdir = getenv("HOME");
103 if (cdir != NULL) {
104 strcpy(t, cdir);
105 strcat(t, "/.netsurf/");
106 strcat(t, filename);
107 NSLOG(netsurf, INFO, "checking %s", (char *)&t);
108 if (gemdos_realpath(t, buf) != NULL) {
109 if (access(buf, R_OK) == 0)
110 return buf;
111 }
112 }
113
114 cdir = getenv("NETSURFRES");
115 if (cdir != NULL) {
116 if (gemdos_realpath(cdir, buf) != NULL) {
117 strcat(buf, "/");
118 strcat(buf, filename);
119 NSLOG(netsurf, INFO, "checking %s", (char *)&t);
120 if (access(buf, R_OK) == 0)
121 return buf;
122 }
123 }
124 if (def[0] == '~') {
125 snprintf(t, PATH_MAX, "%s%s", getenv("HOME"), def + 1);
126 NSLOG(netsurf, INFO, "checking %s", (char *)&t);
127 if (gemdos_realpath(t, buf) == NULL) {
128 strcpy(buf, t);
129 }
130 } else {
131 NSLOG(netsurf, INFO, "checking %s", (char *)def);
132 if (gemdos_realpath(def, buf) == NULL) {
133 strcpy(buf, def);
134 }
135 }
136
137 return buf;
138}
139
140/*
141 * Local Variables:
142 * c-basic-offset: 8
143 * End:
144 */
char * local_file_to_url(const char *filename)
Definition: findfile.c:29
char * atari_find_resource(char *buf, const char *filename, const char *def)
Definition: findfile.c:80
#define NETSURF_GEM_RESPATH
Locate a shared resource file by searching known places in order.
Definition: findfile.c:77
#define BACKSLASH
#define PATH_MAX
Definition: gui.h:31
Useful interned string pointers (interface).
#define FILE_SCHEME_PREFIX_LEN
File url prefix length.
Definition: corestrings.h:33
#define FILE_SCHEME_PREFIX
File url prefix.
Definition: corestrings.h:30
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
char * gemdos_realpath(const char *path, char *rpath)
Definition: osspec.c:107