From: simon Date: Sat, 1 Nov 2008 15:52:16 +0000 (+0000) Subject: Prepare to have a parametrisable path separator character. Currently X-Git-Url: https://git.distorted.org.uk/~mdw/sgt/agedu/commitdiff_plain/373a02e53778079f10f67bd33a948c4f6f2be6da Prepare to have a parametrisable path separator character. Currently the value of 'pathsep' never changes, but the code should now be ready to cope when it does. git-svn-id: svn://svn.tartarus.org/sgt/agedu@8241 cda61777-01e9-0310-a592-d414129be87e --- diff --git a/TODO b/TODO index 9b93ca2..1a8a7eb 100644 --- a/TODO +++ b/TODO @@ -56,8 +56,8 @@ Before it's non-embarrassingly releasable: * what do we do elsewhere about _GNU_SOURCE? - prepare a little in advance for a potential future Windows port: - + store the separator character in the index file when writing - it, and be prepared to cope on reading if it isn't a slash + + store the path separator character in the index file when + writing it, and load it back in when reading + store literal byte sizes in all the size fields, instead of Unixoid 512-byte sectors @@ -109,9 +109,16 @@ Future directions: - http://msdn.microsoft.com/en-us/library/ms724290.aspx suggest modern Windowses support atime-equivalents, so a Windows port is - possible in principle. Would need to modify the current structure - a lot, to abstract away (at least) memory-mapping of files, - details of disk scan procedure, networking for httpd. Unclear - what the right UI would be on Windows, too; command-line exactly - as now might be considered just a _little_ unfriendly. Or perhaps - not. + possible in principle. + + For a full Windows port, would need to modify the current + structure a lot, to abstract away (at least) memory-mapping of + files, details of disk scan procedure, networking for httpd. + Unclear what the right UI would be on Windows, too; + command-line exactly as now might be considered just a + _little_ unfriendly. Or perhaps not. + + Alternatively, a much easier approach would be to write a + Windows version of just the --scan-dump mode, which does a + filesystem scan via the Windows API and generates a valid + agedu dump file on standard output. Then one would simply feed + that over the network connection of one's choice to the rest + of agedu running on Unix as usual. diff --git a/agedu.c b/agedu.c index c88fdf5..d72c73d 100644 --- a/agedu.c +++ b/agedu.c @@ -31,6 +31,16 @@ #define lenof(x) (sizeof((x))/sizeof(*(x))) +/* + * Path separator. This global variable affects the behaviour of + * various parts of the code when they need to deal with path + * separators. The path separator appropriate to a particular data + * set is encoded in the index file storing that data set; data + * sets generated on Unix will of course have the default '/', but + * foreign data sets are conceivable and must be handled correctly. + */ +char pathsep = '/'; + void fatal(const char *fmt, ...) { va_list ap; @@ -86,7 +96,7 @@ static int gotdata(void *vctx, const char *pathname, const struct stat64 *st) * Filter based on wildcards. */ include = 1; - filename = strrchr(pathname, '/'); + filename = strrchr(pathname, pathsep); if (!filename) filename = pathname; else @@ -864,7 +874,7 @@ int main(int argc, char **argv) * Trim trailing slash, just in case. */ pathlen = strlen(querydir); - if (pathlen > 0 && querydir[pathlen-1] == '/') + if (pathlen > 0 && querydir[pathlen-1] == pathsep) querydir[--pathlen] = '\0'; text_query(mappedfile, querydir, textcutoff, 1); @@ -895,7 +905,7 @@ int main(int argc, char **argv) * Trim trailing slash, just in case. */ pathlen = strlen(querydir); - if (pathlen > 0 && querydir[pathlen-1] == '/') + if (pathlen > 0 && querydir[pathlen-1] == pathsep) querydir[--pathlen] = '\0'; xi = trie_before(mappedfile, querydir); diff --git a/html.c b/html.c index 47e4372..1daabe6 100644 --- a/html.c +++ b/html.c @@ -20,6 +20,8 @@ #define MAXCOLOUR 511 +extern char pathsep; + struct html { char *buf; size_t buflen, bufsize; @@ -442,7 +444,7 @@ char *html_query(const void *t, unsigned long index, */ htprintf(ctx, "

\n"); q = path; - for (p = strchr(path, '/'); p; p = strchr(p+1, '/')) { + for (p = strchr(path, pathsep); p; p = strchr(p+1, pathsep)) { int doing_href = 0; /* * See if this path prefix exists in the trie. If so, @@ -456,12 +458,12 @@ char *html_query(const void *t, unsigned long index, htprintf(ctx, "", href); doing_href = 1; } - *p = '/'; + *p = pathsep; htescape(ctx, q, p - q, 1); q = p + 1; if (doing_href) htprintf(ctx, ""); - htprintf(ctx, "/"); + htescape(ctx, q, p - q, 1); } htescape(ctx, q, strlen(q), 1); htprintf(ctx, "\n"); diff --git a/trie.c b/trie.c index f0b21df..7061c74 100644 --- a/trie.c +++ b/trie.c @@ -16,6 +16,8 @@ #define alignof(typ) ( offsetof(struct { char c; typ t; }, t) ) +extern char pathsep; + /* * Compare functions for pathnames. Returns the relative order of * the names, like strcmp; also passes back the offset of the @@ -23,9 +25,9 @@ */ static int trieccmp(unsigned char a, unsigned char b) { - a = (a == '\0' ? '\0' : a == '/' ? '\1' : a+1); - b = (b == '\0' ? '\0' : b == '/' ? '\1' : b+1); - return a - b; + a = (a == '\0' ? '\0' : a == pathsep ? '\1' : a+1); + b = (b == '\0' ? '\0' : b == pathsep ? '\1' : b+1); + return (int)a - (int)b; } static int triencmp(const char *a, size_t alen,