/* -*- Mode: C -*-
 *
 * $Header: /home/gene/library/website/docsrc/sdkskel/src/RCS/msgmap.c,v 395.1 2008/04/20 17:25:51 gene Exp $
 *
 * Copyright (c) 2006 Gene Michael Stover.  All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

#include "this.h"

/*
 */
MsgMap *
MSGMAP_Create ()
{
  MsgMap *map;

  map = (MsgMap *) xmalloc (sizeof *map);
  if (map != NULL) {
    bzero (map, sizeof *map);
  } else {
    printf ("\n%s:%d: xmalloc of %u chars failed", __FILE__, __LINE__,
            (unsigned) (sizeof *map));
  }
  return map;
}

/*
 */
WNDPROC
MSGMAP_Find (WindowMessage msg, MsgMap *map)
{
  MSGMAP_MessageTuple *x = NULL;
  size_t i = 0;

  if (map != NULL) {
    if (map->lst != NULL) {
      while (i < map->count && x == NULL) {
        if (map->lst[i].msg == msg) x = &map->lst[i];
        ++i;
      }
      if (x != NULL) {
        ++x->count;
      }
    } else {
      LOG_TraceMoo (__FILE__, __LINE__, "MSGMAP_Find");
    }
  } else {
    LOG_TraceMoo (__FILE__, __LINE__, "MSGMAP_Find");
  }
  return x ? x->wndproc : NULL;
}

/*
 */
void
MSGMAP_Insert (WindowMessage msg, WNDPROC wndproc, MsgMap *map)
{
  size_t sz;
  FILE *fp;

  fp = LOG_Open ();
  ++map->count;
  sz = map->count * (sizeof *map->lst);
  map->lst = (MSGMAP_MessageTuple *) xrealloc (map->lst, sz);
  map->lst[map->count-1].msg = msg;
  map->lst[map->count-1].wndproc = wndproc;
  map->lst[map->count-1].count = 0;
  if (fp != NULL) {
    fprintf (fp, "\n(\"%s\" %d", __FILE__, __LINE__);
    fprintf (fp, " map->count %lu", (unsigned long) map->count);
    fprintf (fp, ")");
  }
  if (fp != NULL) fclose (fp);
}

/*
 * Return NULL.
 */
MsgMap *MSGMAP_Destroy (MsgMap *map)
{
  if (map != NULL) {
    map->lst = xfree (map->lst);
    map = (MsgMap *) xfree (map);
  }
  return NULL;
}

/* --- end of file --- */
