;+
; Project     :	SOHO - CDS
;
; Name        :	CP_GET_ALL
;
; Purpose     :	Retrieves all current command mnemonics and parameter values from the CDHS state database.
;
; Explanation : Gets the latest values for all entries in the CDHS state database.
;               Extracts all entries with unique command mnemonic and parameter values. 
;               Returns a structure array containing the latest values associated with these entries.
;               If no entries found then returns a structure with command mnemonic : " Unknown" and 
;               displays a popup message.
;
; Use         : < st = cp_get_all >
;
; Inputs      : None
;
; Opt. Inputs : None.
;
; Outputs     : 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    : QUIET : suppresses popup message
;               GROUP : group for popup message box.
;
; Calls       :	dbopen, db_info, dbfind, dbext, dbclose, rem_fst.
;                
; 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.
;                 Changed active and default to LONGs.
;               Version 0.2, MKC, 19/12/95
;                 Added delay tag.
;
; Version     :	Version 0.2, 19/12/95
;-
;**********************************************************

FUNCTION cp_get_all, QUIET=quiet, GROUP=group

  ; set up structure 

  struct = { st_cdhsstate, date:'', mnemonic:'Unknown', pnumber:0, numberp:0, $
                           load:0, delay:0, active:0L, default:0L, comment:'' }

  ; open state database 

  dbopen, 'cdhsstate'

  ; check if table exists already

  ; check database has some entries
  ; else dbfind will flag error

  nentries = db_info ( 'entries' )

  IF nentries(0) EQ 0 THEN BEGIN

    ; display message 

    IF NOT KEYWORD_SET(quiet) THEN $      
      popup_msg, ['No entries', $
                  'found'], $
                  title = 'WARNING MESSAGE', /modal, GROUP=group

    RETURN, struct

  ENDIF

  ; extract entries
  ; NB mnemonic may have more than one parameter associated with it

  list = 1 + LINDGEN ( nentries(0) )

  dbext, list, 'MNEMONIC,PNUMBER', mnemonic, pnumber

  ; find latest unique mnemonics ( allowing for different pnumbers )

  inds = rem_fst ( mnemonic + STRING(pnumber) )

  mnemonic = mnemonic(inds)

  pnumber  = pnumber(inds)

  ; set up structure array

  struct = REPLICATE ( struct, N_ELEMENTS(inds) )

  ; extract unique entries
  ; NB cannot use structure directly since scalars passed by value
  ;    results are arrays

  dbext, list(inds), 'DATE,NUMBERP,LOAD,DELAY,ACTIVE,DEFAULT,COMMENT',$
                      date,numberp,load,delay,active,default,comment

  ; close database

  dbclose, 'cdhsstate'

  ; NB This works because items are all scalars

  IF N_ELEMENTS(date) EQ 1 THEN BEGIN

    inds = 0 
    mnemonic = mnemonic(inds)
    date     = date(inds)
    pnumber  = pnumber(inds)
    numberp  = numberp(inds)
    load     = load(inds)
    delay    = delay(inds)
    active   = active(inds)
    default  = default(inds)
    comment  = comment(inds)

  ENDIF

  ; return values in parameter order

  struct.mnemonic = mnemonic
  struct.date     = date
  struct.pnumber  = pnumber
  struct.numberp  = numberp
  struct.load     = load
  struct.delay    = delay
  struct.active   = active
  struct.default  = default
  struct.comment  = comment

  RETURN, struct
  
END


