librnr  1.14.5
RoadNarrows Robotics Common Library 1
example_dlist.c File Reference

Example of a librnr "derived" DListVoid doubly-linked lists. More...

#include <stdio.h>
#include <string.h>
#include "rnr/rnrconfig.h"
#include "rnr/new.h"
#include "example_dlist.h"

Go to the source code of this file.

Functions

static Zoo_TNewZooAnimal (const char *sSciName, const char *sPetName)
 Allocate new zoo addition. More...
 
int main (int argc, char *argv[])
 Example main. More...
 

Variables

const char * AnimalCatalog [][2]
 Animal catalog of the world. More...
 
static char * FlatEarthZooDeal [][2]
 Zoo we will "buy". More...
 
static Zid = 0
 zoo id
 

Detailed Description

Example of a librnr "derived" DListVoid doubly-linked lists.

LastChangedDate
2010-03-24 10:19:36 -0600 (Wed, 24 Mar 2010)
Rev
307
Author
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)

Definition in file example_dlist.c.

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Example main.

Parameters
argcCommand-line argument count.
argvCommand-line argument list.
Exit Status:
Program exits with 0 success, > 0 on failure.

Definition at line 129 of file example_dlist.c.

References DListZooDataCmp(), DListZooDataDelete(), DListZooDataPrint(), FlatEarthZooDeal, Zoo_T::m_sPetName, NewZooAnimal(), and NULL.

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
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
#define NULL
null pointer
Definition: rnrconfig.h:199
char * m_sPetName
zoo animal pet (pr) name
Definition: example_dlist.h:60
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
static Zoo_T* NewZooAnimal ( const char *  sSciName,
const char *  sPetName 
)
static

Allocate new zoo addition.

Parameters
sSciNameScientific name.
sPetNamePet name.
Returns
New zoo node.

Definition at line 90 of file example_dlist.c.

References AnimalCatalog, Zoo_T::m_nZid, Zoo_T::m_sComName, Zoo_T::m_sPetName, Zoo_T::m_sSciName, NEW, new_strdup(), NULL, and Zid.

Referenced by main().

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 }
Zoo Type.
Definition: example_dlist.h:55
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
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
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

Variable Documentation

const char* AnimalCatalog[][2]
Initial value:
=
{
{"elephas maximus", "Asiatic elephant"},
{"loxodonta africana", "African bush elephant"},
{"python sebae", "African rock python"},
{"alligator mississippiensis", "American alligator"},
{"ramphastos toco", "Toco toucan"},
{"lasiorhinus latifrons", "southern hairy-nosed wombat"},
{"rhinoceros sondaicus", "Javan rhinoceros"},
}
#define NULL
null pointer
Definition: rnrconfig.h:199

Animal catalog of the world.

Definition at line 55 of file example_dlist.c.

Referenced by NewZooAnimal().

char* FlatEarthZooDeal[][2]
static
Initial value:
=
{
{"Shoes", "alligator mississippiensis"},
{"Fluffy", "loxodonta africana"},
{"Snuggles", "python sebae"},
{"Wild Bill", "ramphastos toco"},
{"The Prez", "lasiorhinus latifrons"},
}
#define NULL
null pointer
Definition: rnrconfig.h:199

Zoo we will "buy".

Definition at line 70 of file example_dlist.c.

Referenced by main().