/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2009.
     ssss    gggg x   x         Steven Goodwin. All Rights Reserved.
    
This version has been released under the GNU Public License v2.0.

Please see the license files for details.

File: sgx/src/utils/core/textloader.cpp

*/

#include "sgx/core/core.hpp"
#include "sgx/filesystem/filesystem.hpp"
#include "utils/core/textloader.hpp"

tBOOL SGXTextLoader::loadResource(const sgxString &filename) {
CSGXFile file(filename);

	 if (!file.open()) {
		 return FALSE;
	}

	 sgxString str;
	 SGXStringEntry *pCurrent = NULL;
	 tBOOL lateHashBinding = FALSE;
	 tUINT32 lastID = 0;
	 
	 while(!file.isEOF()) {
		 file.readLine(str);
		 sgxString midstr = str.substr(str.find(':') + 1);
		 //sgxTrace("read %s, mid=%s", str.c_str(), midstr.c_str());

		 switch(str[0]) {
			 case ':':
				 pCurrent = new SGXStringEntry();	// we fill in the id/hash later
				 lateHashBinding = TRUE;
				 break;
			 case 'I':
			 case 'i':
				 pCurrent = new SGXStringEntry();
				 lastID = sgxAtoi(midstr);
				 SGXText::addStringEntry(lastID, pCurrent);
				 lateHashBinding = FALSE;
				 break;
			 case 'H':
			 case 'h':
				 pCurrent = new SGXStringEntry();
				 pCurrent->vSingle = midstr;
				 SGXText::addStringEntry(midstr, pCurrent);
				 lateHashBinding = FALSE;
				 break;
			 case '+':	// fill in existing entry, and post-increment
				 pCurrent->vSingle = midstr;
				 pCurrent = new SGXStringEntry();
				 SGXText::addStringEntry(++lastID, pCurrent);
				 lateHashBinding = FALSE;
				 break;
			 case 'S':
			 case 's':
				 pCurrent->vSingle = midstr;
				 if (lateHashBinding) {
					 SGXText::addStringEntry(midstr, pCurrent);
				 }
				 break;
			 case 'P':
			 case 'p':
				 pCurrent->vPlural = midstr;
				 break;
			 default:
				 pCurrent->vOthers[sgxAtoi(str)] = midstr;
				 break;
		}
	}
#if 0
	// enumerate
	sgxHashMap<tUINT32, SGXStringEntry *>::iterator it = (*byID).begin();
	for(;it!=(*byID).end();++it) {
		sgxTrace("%ld : %s\n", (*it).first, (*it).second->vSingle.c_str());
	}
#endif
	return TRUE;
}
