/* 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>
#include <sys/types.h>

/* API sections:
 *    10. Structures				:: User-defined data structures
 *    13. Constants				:: User-defined constants/enums
 */


/* ---------------------------------------------------------------Structures */
   unsigned long DefineStruct( name ){
   // overwrites existing if present
      disasm_env.flags |= DB_MOD;
      return(0);
   }
   int DefineStructMember( name, type ){
   // overwrites existing if present 
      disasm_env.flags |= DB_MOD;
      return(0);
   }
   int GetStruct( unsigned long id, struct structure *s) {
   // fills structure 
      return(0);
   }
   unsigned long GetStructID(char *name){
      return(0);
   }
   int GetStructMember( struct struct_member *m) {
   // m.id and m.order must be filled before calling this
      return(0);
   }
   int ApplyStructure(long rva, unsigned long id) {
   // This basically just sets addr.structre to s
      disasm_env.flags |= DB_MOD;
      return(0);
   }
   int DelStruct( unsigned long id){
      disasm_env.flags |= DB_MOD;
   // s = structure ID 
      return(0);
   }
   int DelStructMember( unsigned long id){
      disasm_env.flags |= DB_MOD;
   // s = struct_member id
      return(0);
   }
/* ---------------------------------------------------------------Constants */
   int DefineConstant( char *name, int value){
   /* redefines existing */
		struct constant c;
		int exists = 0;

		if ( d_keyfind(CONSTANT_NAME, name) == S_OKAY) {
			d_recread(&c);
			exists = 1;
		} else {
			c.id = ++seq.constant;
			strncpy(c.name, name, 31);
			c.name[31] = 0;
		}
		c.value = value;
		if (exists) d_recwrite(&c);
		else (d_fillnew(CONSTANT, &c));

		if (db_status != S_OKAY) return(SetLastError(dbError()) );
		return(1);
   }

	int GetConstant(int value, char buf[32]){
		struct constant c;
		
		if ( d_keyfind(VALUE, &value) == S_OKAY) {
			d_recread(&c);
			strcpy(buf, c.name);
			return(c.id);
		}
      return(SetLastError(5120));
	}

	int GetConstantNext(char buf[32]){
		struct constant c;
		
		if ( d_keynext(VALUE) == S_OKAY) {
			d_recread(&c);
			strcpy(buf, c.name);
			return(c.id);
		}
      return(0);
	}
		

   int GetConstantName( unsigned long id, char buf[32]){
   // get name into buf[32]
		struct constant c;
		
		if ( d_keyfind(CONSTANT_ID, &id) == S_OKAY) {
			d_recread(&c);
			strcpy(buf, c.name);
			return(strlen(c.name));
		} 
      return(SetLastError(5100));
   }

   unsigned long GetConstantID( char *name){
		/* constants are unique by name/id, but not by value */
		struct constant c;
		
		if ( d_keyfind(CONSTANT_NAME, name) == S_OKAY) {
			d_recread(&c);
			return(c.id);
		}
      return(SetLastError(5110));
   }

   int DelConstant( unsigned long id){
		if ( d_keyfind(CONSTANT_ID, &id) == S_OKAY) {
			d_delete();
			return(1);
		} 
		return(SetLastError(5100)); 
   }
