/* asc_core.c++ -- written by Alexis WILKE for Made to Order Software Corp. (c) 2009 */

/*

Copyright (c) 2009 Made to Order Software Corp.

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and
associated documentation files (the "Software"), to
deal in the Software without restriction, including
without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom
the Software is furnished to do so, subject to the
following conditions:

The above copyright notice and this permission notice
shall be included in all copies or substantial
portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

*/

#include	"sswf/libsswf_asc.h"
#include	<ctype.h>


namespace sswf
{
namespace asc
{
namespace details
{
extern const int files_count;
extern const char *files[];

bool compare_filenames(const char *a, const char *b)
{
	char	m, n;

	for(;;) {
		m = *a++;
		n = *b++;
		if(m == '\0' || n == '\0') {
			// true only if both are '\0'
			return m == '\0' && n == '\0';
		}
		if(m == '\\') {
			m = '/';
		}
		else if(n == '\\') {
			n = '/';
		}
#if defined(_WIN32) || defined(__APPLE_CC__)
		else {
			// ignore case in Win32 & Mac OS/X
			m = toupper(m);
			n = toupper(n);
		}
#endif
		if(m != n) {
			return false;
		}
	}
}

}

/** \brief Searches for an internal file.
 *
 * This function searches the list of internal files to find the
 * one specified as input.
 *
 * \param[in] filename   The filename to search
 *
 * \return The corresponding internal file or NULL if not found
 */
const char *get_file(const char *filename)
{
	int	i, max;

	// search the files array
	// even positions have the filename
	// odd positions have the content
	max = details::files_count * 2;
	for(i = 0; i < max; i += 2) {
		// TODO: how should we handle character case here?
		if(details::compare_filenames(details::files[i], filename)) {
//printf("Got file [%s]\n", filename);
			return details::files[i + 1];
		}
	}

	return NULL;
}


/** \brief Search for the specified file.
 *
 * This function searches for the file specified by filename in
 * our internal buffers. The internal buffers are composed of all
 * the system ASC files as defined under include/sswf/scripts/...
 *
 * \bug
 * The returned Input object is allocated by this function and
 * must be released by the caller.
 *
 * \param[in] filename   The name of the file to be retrieved
 *
 * \return A newly allocated StringInput object with the content
 * of the file if it exists, NULL otherwise
 */
as::Input *AscInputRetriever::Retrieve(const char *filename)
{
	const char	*data;
	as::StringInput	*in;
	as::String	wcode;

	data = get_file(filename);
	if(data != NULL) {
		in = new as::StringInput(filename);
		wcode.FromUTF8(data, strlen(data));
		in->Set(wcode.Get(), wcode.GetLength(), 1);
		return in;
	}

	return NULL;
}


};	// namespace asc
};	// namespace sswf
