;+
; Project     :	SOHO - CDS
;
; Name        :	CP_ADD_MNEMONIC
;
; Purpose     :	Adds a command mnemonic to the CDHS state database.
;
; Explanation : Receives a structure array containing entries for the CDHS state database.
;               Checks that pnumber and numberp are consistent but allows more than one 
;               pnumber if numberp=1 
;
; Use         : <cp_add_mnemonic, struct>
;
; Inputs      : struct = structure array of type st_cdhsstate containing command mnemonics and parameter values.                        
;                 struct.mnemonic = parameter mnemonic
;                 struct.date     = date values last changed
;                 struct.numberp  = no. of parameters associated with mnemonic
;                 struct.pnumber  = parameter number for this entry
;                 struct.load     = flag indicating whether value requires loading
;                 struct.delay    = delay in secs associated with command
;                 struct.active   = value for parameter
;                 struct.default  = default value for parameter
;                 struct.comment  = comment on parameter
;
; Opt. Outputs:	None.
;
; Keywords    : None.
;
; Calls       :	dbopen, dbbuild, dbclose.
;                
; Common      :	None.
;
; Restrictions:	None.
;
; Side effects:	None.
;
; Category    :	Command preparation.
;
; Prev. Hist. :	None.
;
; Written     :	Version 0.0, Martin Carter, RAL, 24/5/94
;
; Modified    :	Version 0.1, MKC, 14/8/95
;                 Added comment field to structure.
;               Version 0.2, MKC, 18/10/95
;                 Added check that pnumber and numberp are conistent.
;               Version 0.3, MKC, 19/12/95
;                 Added delay tag.
;                 Added special case if numberp=1.
;               Version 0.4, MKC, 2/1/96
;                 Added special case if numberp=0.
;
; Version     :	Version 0.4, 2/1/96
;-
;**********************************************************

PRO cp_add_mnemonic, struct

  ; set priviledge to update database

  !PRIV = 2

  ; check that pnumber and numberp are consistent.

  IF struct(0).numberp EQ 0 THEN count0=0 ELSE $
  IF struct(0).numberp EQ 1 THEN count0=0 ELSE $
     dummy = WHERE ( struct.pnumber GE struct(0).numberp, count0)
    
  dummy = WHERE ( struct.numberp NE struct(0).numberp, count1)

  IF (count0+count1) GT 0 THEN BEGIN

    ; display message 

    popup_msg, ['PNUMBER exception', 'found'], $
               title = 'WARNING MESSAGE', /modal, GROUP=group

    RETURN

  ENDIF

  ; open state database for updating

  dbopen, 'cdhsstate', 1

  ; create new entries in database

  FOR k = 0, N_ELEMENTS(struct)-1 DO $
    dbbuild, struct(k).date, struct(k).mnemonic, struct(k).pnumber, struct(k).numberp, $
             struct(k).load, struct(k).delay, struct(k).active, struct(k).default, struct.comment

  ; close database

  dbclose, 'cdhsstate'

  ; reset priviledges

  !PRIV = 1  

END


