librnr  1.14.5
RoadNarrows Robotics Common Library 1
example_dlist.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Common Library 1
4 //
5 // File: example_dlist.c
6 //
7 /*! \file
8  *
9  * $LastChangedDate: 2010-03-24 10:19:36 -0600 (Wed, 24 Mar 2010) $
10  * $Rev: 307 $
11  *
12  * \brief Example of a librnr "derived" DListVoid doubly-linked lists.
13  *
14  * \author Robin Knight (robin.knight@roadnarrows.com)
15  *
16  * \pkgcopyright{2007-2018,RoadNarrows LLC.,http://www.roadnarrows.com}
17  */
18 // Permission is hereby granted, without written agreement and without
19 // license or royalty fees, to use, copy, modify, and distribute this
20 // software and its documentation for any purpose, provided that
21 // (1) The above copyright notice and the following two paragraphs
22 // appear in all copies of the source code and (2) redistributions
23 // including binaries reproduces these notices in the supporting
24 // documentation. Substantial modifications to this software may be
25 // copyrighted by their authors and need not follow the licensing terms
26 // described here, provided that the new terms are clearly indicated in
27 // all files where they apply.
28 //
29 // IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
30 // OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
31 // PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
32 // DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
33 // EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
34 // THE POSSIBILITY OF SUCH DAMAGE.
35 //
36 // THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
37 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
38 // FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
39 // "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
40 // PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
41 //
42 ////////////////////////////////////////////////////////////////////////////////
43 
44 #include <stdio.h>
45 #include <string.h>
46 
47 #include "rnr/rnrconfig.h"
48 #include "rnr/new.h"
49 
50 #include "example_dlist.h"
51 
52 /*!
53  * \brief Animal catalog of the world.
54  */
55 const char *AnimalCatalog[][2] =
56 {
57  {"elephas maximus", "Asiatic elephant"},
58  {"loxodonta africana", "African bush elephant"},
59  {"python sebae", "African rock python"},
60  {"alligator mississippiensis", "American alligator"},
61  {"ramphastos toco", "Toco toucan"},
62  {"lasiorhinus latifrons", "southern hairy-nosed wombat"},
63  {"rhinoceros sondaicus", "Javan rhinoceros"},
64  {NULL, NULL}
65 };
66 
67 /*!
68  * \brief Zoo we will "buy".
69  */
70 static char *FlatEarthZooDeal[][2] =
71 {
72  {"Shoes", "alligator mississippiensis"},
73  {"Fluffy", "loxodonta africana"},
74  {"Snuggles", "python sebae"},
75  {"Wild Bill", "ramphastos toco"},
76  {"The Prez", "lasiorhinus latifrons"},
77  {NULL, NULL}
78 };
79 
80 static Zid = 0; ///< zoo id
81 
82 /*!
83  * \brief Allocate new zoo addition.
84  *
85  * \param sSciName Scientific name.
86  * \param sPetName Pet name.
87  *
88  * \return New zoo node.
89  */
90 static Zoo_T *NewZooAnimal(const char *sSciName, const char *sPetName)
91 {
92  const char **sCat;
93  Zoo_T *pZooAni;
94 
95  pZooAni = NEW(Zoo_T);
96  pZooAni->m_nZid = Zid++;
97  pZooAni->m_sPetName = new_strdup(sPetName);
98 
99  sCat = AnimalCatalog[0];
100 
101  // find animal species in catalog
102  for(sCat=AnimalCatalog[0]; sCat[0]!=NULL; sCat++)
103  {
104  if( !strcmp(sCat[0], sSciName) )
105  {
106  // assign type
107  pZooAni->m_sSciName = sCat[0];
108  pZooAni->m_sComName = sCat[1];
109  return pZooAni;
110  }
111  }
112 
113  // unknown animal type
114  pZooAni->m_sSciName = "???";
115  pZooAni->m_sComName = "???";
116 
117  return pZooAni;
118 }
119 
120 /*!
121  * \brief Example main.
122  *
123  * \param argc Command-line argument count.
124  * \param argv Command-line argument list.
125  *
126  * \par Exit Status:
127  * Program exits with 0 success, \h_gt 0 on failure.
128  */
129 int main(int argc, char *argv[])
130 {
131  DListZoo_T *pMyZoo;
132  Zoo_T *pMyAni;
133  Zoo_T zooMatch;
134  DListZooIter_T zooIter;
135  int i;
136 
137  // buy empty zoo
138  pMyZoo = DListZooNew(DListZooDataCmp, DListZooDataDelete);
139 
140  printf("I bought an empty zoo.\n");
141  DListZooPrint(pMyZoo, DListZooDataPrint, stdout);
142 
143  // purchase animals from zoo in FlatEarth, NE.
144  for(i=0; FlatEarthZooDeal[i][0]!=NULL; ++i)
145  {
146  pMyAni = NewZooAnimal(FlatEarthZooDeal[i][1], FlatEarthZooDeal[i][0]);
147  DListZooAppend(pMyZoo, pMyAni);
148  }
149 
150  printf("\nI bought some animals from the zoo in Flat Earth, NE.\n");
151  DListZooPrint(pMyZoo, DListZooDataPrint, stdout);
152 
153  // Fluffy dies
154  zooMatch.m_sPetName = "Fluffy";
155  DListZooDeleteNode(pMyZoo, DListZooFindNode(pMyZoo, &zooMatch));
156 
157  printf("\nAlas, Fluffy died.\n");
158  DListZooPrint(pMyZoo, DListZooDataPrint, stdout);
159 
160  // purchase "eMouse" from kid down the block.
161  pMyAni = NewZooAnimal("elephas maximus", "eMouse");
162  DListZooPrepend(pMyZoo, pMyAni);
163 
164  printf("\nBut I got a new primo 'phant from the kid down the block.\n");
165  DListZooPrint(pMyZoo, DListZooDataPrint, stdout);
166 
167  // give vitamin shots to the beasts
168  printf("\nTime to give my menagerie their yearly vitamin shots.\n");
169  for(pMyAni=DListZooIterDataFirst(pMyZoo, &zooIter);
170  pMyAni!=NULL;
171  pMyAni=DListZooIterDataNext(&zooIter))
172  {
173  printf(" injected %s\n", pMyAni->m_sPetName);
174  }
175 
176  // intelligent procreation
177  pMyAni = NewZooAnimal("lasiorhinus latifrons", "wittlewomby1");
178  DListZooInsert(pMyZoo, 4, pMyAni);
179  pMyAni = NewZooAnimal("lasiorhinus latifrons", "wittlewomby2");
180  DListZooInsert(pMyZoo, 4, pMyAni);
181 
182  printf("\nI guess \"The Prez\" wasn't male. "
183  "Pass out the bubble gum cigars!\n");
184  DListZooPrint(pMyZoo, DListZooDataPrint, stdout);
185 
186  // let my animals go free
187  DListZooDeleteAllNodes(pMyZoo);
188 
189  printf("\nI decided to let my animals go free - in Alaska.\n");
190  DListZooPrint(pMyZoo, DListZooDataPrint, stdout);
191 
192  DListZooDelete(pMyZoo);
193 
194  printf("\nGoodbye.\n");
195 
196  return 0;
197 }
static int DListZooDataCmp(const Zoo_T *pData1, const Zoo_T *pData2)
Node zoo data comparator callback.
Definition: example_dlist.h:74
Zoo Type.
Definition: example_dlist.h:55
int main(int argc, char *argv[])
Example main.
static Zoo_T * NewZooAnimal(const char *sSciName, const char *sPetName)
Allocate new zoo addition.
Definition: example_dlist.c:90
static void DListZooDataDelete(Zoo_T *pData)
Node zoo data delete callback.
Definition: example_dlist.h:84
char * new_strdup(const char *s)
Duplicate a string.
Definition: new.c:176
const char * m_sComName
zoo animal common name
Definition: example_dlist.h:59
#define NULL
null pointer
Definition: rnrconfig.h:199
const char * AnimalCatalog[][2]
Animal catalog of the world.
Definition: example_dlist.c:55
Memory allocation and deallocation declarations.
static Zid
zoo id
Definition: example_dlist.c:80
char * m_sPetName
zoo animal pet (pr) name
Definition: example_dlist.h:60
#define NEW(T)
Allocate new type.
Definition: new.h:49
Example of a "derived" DListVoid doubly-linked lists.
RoadNarrows Robotics common configuration file.
static char * FlatEarthZooDeal[][2]
Zoo we will "buy".
Definition: example_dlist.c:70
static void DListZooDataPrint(FILE *fp, Zoo_T *pData)
Node zoo data print callback.
Definition: example_dlist.h:96
const char * m_sSciName
zoo animal scientific name
Definition: example_dlist.h:58
int m_nZid
zoo animal unique id
Definition: example_dlist.h:57