/* api.c : part of The Bastard disassembly environment ========================G
 *
 * Contains  the bulk of the internal code -- specifically, the API routines
 * provided for internal and scripting use. Functions should be in the same
 * order as in api.h
 */
#include <bastard.h>


/* API Sections:
 *
 *     7. Strings					:: Creating/manipulating NAME objects
 */


/* ---------------------------------------------------------------Strings */
   int GetString(long rva, char *buf, int len) {
		/* returns length of string, *not* bytes written */
		struct string s;
		
		if (d_keyfind(STRING_RVA, &rva) != S_OKAY)
			return(SetLastError(5000));
		d_recread(&s);
		strncpy( buf, s.text, len);		
      return(s.length);
	}

   int DefineString(long rva, char *text) {
		struct string s;

		s.rva = rva;
		strncpy(s.text, text, 255);
		s.text[255] = 0;
		s.length = strlen(s.text);
		if (d_keyfind(STRING_RVA, &rva) == S_OKAY)
			d_recwrite(&s);
		else if (d_fillnew(STRING, &s) != S_OKAY)
			return(SetLastError(dbError()) );
		return(s.length);
   }

   int DumpStrings() {
		struct string s;

		d_keyfrst(STRING_RVA);
		while (db_status == S_OKAY) {
			d_recread(&s);
			printf("%s\n", s.text);
			d_keynext(STRING_RVA);
		}
		return(1);
	}
