home *** CD-ROM | disk | FTP | other *** search
/ Large Pack of OldSkool DOS MOD Trackers / goattracker_2.68.zip / src / asm / namedbuf.c < prev    next >
C/C++ Source or Header  |  2008-04-01  |  3KB  |  123 lines

  1. /*
  2.  * Copyright (c) 2005 Magnus Lind.
  3.  *
  4.  * This software is provided 'as-is', without any express or implied warranty.
  5.  * In no event will the authors be held liable for any damages arising from
  6.  * the use of this software.
  7.  *
  8.  * Permission is granted to anyone to use this software, alter it and re-
  9.  * distribute it freely for any non-commercial, non-profit purpose subject to
  10.  * the following restrictions:
  11.  *
  12.  *   1. The origin of this software must not be misrepresented; you must not
  13.  *   claim that you wrote the original software. If you use this software in a
  14.  *   product, an acknowledgment in the product documentation would be
  15.  *   appreciated but is not required.
  16.  *
  17.  *   2. Altered source versions must be plainly marked as such, and must not
  18.  *   be misrepresented as being the original software.
  19.  *
  20.  *   3. This notice may not be removed or altered from any distribution.
  21.  *
  22.  *   4. The names of this software and/or it's copyright holders may not be
  23.  *   used to endorse or promote products derived from this software without
  24.  *   specific prior written permission.
  25.  *
  26.  */
  27.  
  28. #include "namedbuf.h"
  29. #include "log.h"
  30. #include "vec.h"
  31. #include "membufio.h"
  32.  
  33. #include <stdlib.h>
  34.  
  35.  
  36. struct sbe
  37. {
  38.     const char *name;
  39.     struct membuf mb[1];
  40. };
  41.  
  42. static struct vec s_sbe_table[1];
  43.  
  44. static int sbe_cmp(const void *a, const void *b)
  45. {
  46.     struct sbe *sbe_a;
  47.     struct sbe *sbe_b;
  48.     int val;
  49.  
  50.     sbe_a = (struct sbe*)a;
  51.     sbe_b = (struct sbe*)b;
  52.  
  53.     val = strcmp(sbe_a->name, sbe_b->name);
  54.  
  55.     return val;
  56. }
  57.  
  58. void sbe_free(struct sbe *e)
  59. {
  60.     membuf_free(e->mb);
  61. }
  62.  
  63. void named_buffer_init()
  64. {
  65.     vec_init(s_sbe_table, sizeof(struct sbe));
  66. }
  67.  
  68. void named_buffer_free()
  69. {
  70.     typedef void cb_free(void *a);
  71.  
  72.     vec_free(s_sbe_table, (cb_free*)sbe_free);
  73. }
  74.  
  75. struct membuf *new_named_buffer(const char *name)
  76. {
  77.     int pos;
  78.     struct sbe e[1];
  79.     struct sbe *ep;
  80.     struct membuf *mp;
  81.  
  82.     /* name is already strdup:ped */
  83.     e->name = name;
  84.     pos = vec_find(s_sbe_table, sbe_cmp, e);
  85.     if(pos >= 0)
  86.     {
  87.         /* found */
  88.         LOG(LOG_ERROR, ("buffer already exists.\n"));
  89.         exit(-1);
  90.     }
  91.     membuf_init(e->mb);
  92.     ep = vec_insert(s_sbe_table, -(pos + 2), e);
  93.     mp = ep->mb;
  94.  
  95.     return mp;
  96. }
  97.  
  98. struct membuf *get_named_buffer(const char *name)
  99. {
  100.     int pos;
  101.     struct sbe e[1];
  102.     struct sbe *ep;
  103.     struct membuf *mp;
  104.  
  105.     /* name is already strdup:ped */
  106.     e->name = name;
  107.     pos = vec_find(s_sbe_table, sbe_cmp, e);
  108.     if(pos >= 0)
  109.     {
  110.         /* found */
  111.         ep = vec_get(s_sbe_table, pos);
  112.     }
  113.     else
  114.     {
  115.         membuf_init(e->mb);
  116.         read_file(name, e->mb);
  117.         ep = vec_insert(s_sbe_table, -(pos + 2), e);
  118.     }
  119.     mp = ep->mb;
  120.  
  121.     return mp;
  122. }
  123.