
/*
 *	updated:	July 16. 2001
 *
 *	Copyright (c) by Collin R. Mulliner <palm@mulliner.org>
 *	http://www.mulliner.org/palm/
 *
 *	Feel free to use this function in any of your programms,
 *  as long as you give credit!
 *
 *	This function gets or sets the PalmOS UserName (the HostSync name)
 *  and UserID.
 *	
 *	set		    : true to write/false to read
 *	palm_name	: pointer to 41 bytes of mem, 
 *                UserName (max 40 chars) + the zero byte
 *	palm_id		: pointer to a UInt16, UserID
 */

//#include <PalmTypes.h>
//#include <SystemMgr.h>
//#include <MemoryMgr.h>
#include <PalmOS.h>

void get_set_palm_name ( Boolean set,
					char *palm_name,
					UInt16 * palm_id )
{
  DmOpenRef db_ref;
  void *res_ptr;
  MemHandle res_hand;
  UInt32 res_size;
  unsigned char *in_buffer = '\0', *out_buffer = '\0';
  UInt8 name_length = 0;
  UInt16 i;
  UInt16 res_index;
  unsigned char ins_name_length;

  // open prefs database
  db_ref = DmOpenDatabaseByTypeCreator ( 'pref', 'psys', dmModeReadWrite );
  // search for the name resource
  res_index = DmFindResource ( db_ref, 'psys', 4, NULL );	// it's a resource

  if ( res_index != -1 )
  {	// resource found
    res_hand = DmGetResource ( 'psys', 4 );
    res_size = MemHandleSize ( res_hand );
    res_ptr = MemHandleLock ( res_hand );

    in_buffer = MemPtrNew ( res_size );

    if ( set )
    {	// set it
      name_length = StrLen ( palm_name );
      out_buffer = MemPtrNew ( 44 + name_length + 1 );
    }
    MemMove ( in_buffer, res_ptr, res_size );
    MemHandleUnlock ( res_hand );

    if ( !set )
    {	// get it
      i = 1;
      while ( *( in_buffer + 42 + i ) != '\0' )
      {
	i++;
      }
      MemMove ( palm_name, in_buffer + 42, i + 1 );	// user name
      MemMove ( palm_id, in_buffer + 4, 2 );	// user id
    }

    if ( set )
    {	// set it
      DmResizeResource ( res_hand, 44 + name_length + 1 );
      res_ptr = MemHandleLock ( res_hand );

      MemMove ( out_buffer, in_buffer, 42 );
      MemMove ( out_buffer + 42, palm_name, name_length + 1 );

      ins_name_length = ( unsigned char ) name_length + 1;

      MemMove ( out_buffer + 38, &ins_name_length, 1 );

      MemMove ( out_buffer + 4, palm_id, 2 );
      // this is the user ID 

      DmWrite ( res_ptr, 0, out_buffer, 44 + name_length + 1 );
      MemHandleUnlock ( res_hand );
      MemPtrFree ( out_buffer );
    }

    DmReleaseResource ( res_hand );
    MemPtrFree ( in_buffer );
  }
  else
  {	// resource not found
    if ( !set )
    {	// get it
      //      *(palm_name) = '\0';
    }
    else
    {	// set it
      name_length = StrLen ( palm_name );
      res_hand = DmNewResource ( db_ref, 'psys', 4, 44 + name_length + 1 );
      res_ptr = MemHandleLock ( res_hand );
      out_buffer = MemPtrNew ( 44 + name_length + 1 );
      MemSet ( out_buffer, 0x00, 44 + name_length + 1 );
      MemMove ( out_buffer + 42, palm_name, name_length + 1 );

      ins_name_length = ( unsigned char ) name_length + 1;

      MemMove ( out_buffer + 38, &ins_name_length, 1 );

      MemMove ( out_buffer + 4, palm_id, 2 );
      // this is the user ID 

      DmWrite ( res_ptr, 0, out_buffer, 44 + name_length + 1 );
      MemHandleUnlock ( res_hand );
      MemPtrFree ( out_buffer );
      DmReleaseResource ( res_hand );
    }
  }
  DmCloseDatabase ( db_ref );
}
