lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


2011/2/28 Michal Kolodziejczyk <miko@wp.pl>
> It works for me as described (after adding package.cpath). Is your
> foo.lua in somedir/lib/foo.lua or somedir/system/lib/foo.lua? Your
> example is not consistent, and this may be the reason it is not working
> for you.

Yes, it's in system/lib/foo.lua. I noticed that typo when I've already
sent the mail, sorry.

>
> BTW, you are mixing capitals (Foo.lua vs foo.lua) - this may work on
> windows by accident, but is not portalble.

Capital 'F' = begin of sentence.
>
> That is the point: if it is so trivial it don't need to be added by
> default (to the core lua). Nothing stops you from building your own
> framework. Imagine there are tons of functions which are trivial to
> write - should each of them be part of Lua by default?
>
I'm sorry, but what kind of logic is that? the require() function is
clearly part of the core interpreter, and nearly every single
interpreter (e.g., python, ruby. perl) out there checks the relative
path to the called script first, and then the relative path calculated
from the module in then importpath.


Adding this functionality requires far more than just hacking the
core... and adding even more module dependencies just to fix one
fairly trivial change? I hope you're not being serious about that.

As for the 'dirname' function, it *is* trivial to implement:

    std::string dirname(std::string source)
    {
        source.erase(std::find(source.rbegin(), source.rend(),
'/').base(), source.end());
        return source;
    }

Or in C:

	char *
	dirname(const char *path)
	{
			static char dname[MAXPATHLEN];
			size_t len;
			const char *endp;

			/* Empty or NULL string gets treated as "." */
			if (path == NULL || *path == '\0') {
					dname[0] = '.';
					dname[1] = '\0';
					return (dname);
			}

			/* Strip any trailing slashes */
			endp = path + strlen(path) - 1;
			while (endp > path && *endp == '/')
					endp--;

			/* Find the start of the dir */
			while (endp > path && *endp != '/')
					endp--;

			/* Either the dir is "/" or there are no slashes */
			if (endp == path) {
					dname[0] = *endp == '/' ? '/' : '.';
					dname[1] = '\0';
					return (dname);
			} else {
					/* Move forward past the separating slashes */
					do {
							endp--;
					} while (endp > path && *endp == '/');
			}

			len = endp - path + 1;
			if (len >= sizeof(dname)) {
					errno = ENAMETOOLONG;
					return (NULL);
			}
			memcpy(dname, path, len);
			dname[len] = '\0';
			return (dname);
	}

Just add some #ifdefs for the slash, and voila.
I seriously fail to see how this could _possibly_ result in bloat. But
please point me to the bloat, and I'll show you a non-bloated
solution.