Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

idep_namea.cxx

Go to the documentation of this file.
00001 // idep_namea.c
00002 #include "idep_namearray.h"
00003 
00004 #include <memory.h>     // memcpy()
00005 #include <string.h>     // strlen()
00006 #include <iostream>
00007 #include <cassert>
00008 
00009                 // -*-*-*- static functions -*-*-*-
00010  
00011 enum { START_SIZE = 1, GROW_FACTOR = 2 };
00012  
00013 static char *newStrCpy(const char *oldStr)
00014 {
00015     int size = strlen(oldStr) + 1;
00016     char *newStr = new char[size];
00017     assert(newStr);
00018     memcpy(newStr, oldStr, size);
00019     return newStr;
00020 }
00021  
00022                 // -*-*-*- idep_NameArray -*-*-*-
00023  
00024 idep_NameArray::idep_NameArray(int maxEntriesHint)
00025 : d_size(maxEntriesHint > 0 ? maxEntriesHint : START_SIZE)
00026 , d_length(0)
00027 {
00028     d_array_p = new char *[d_size];
00029 } 
00030 
00031 idep_NameArray::~idep_NameArray()
00032 {                                     
00033     for (int i = 0; i < d_length; ++i) {
00034         delete [] d_array_p[i];
00035     }
00036     delete [] d_array_p;
00037 }
00038 
00039 int idep_NameArray::append(const char *name) 
00040 {
00041     if (d_length >= d_size) {
00042         int oldSize = d_size;
00043         d_size *= GROW_FACTOR;
00044         char **tmp = d_array_p;
00045         d_array_p = new char *[d_size];
00046         assert (d_array_p);
00047         memcpy (d_array_p, tmp, oldSize * sizeof *d_array_p);
00048         delete [] tmp;
00049     }
00050     assert(d_length < d_size);
00051     d_array_p[d_length++] = newStrCpy(name);
00052     return d_length - 1;
00053 }
00054 
00055 const char *idep_NameArray::operator[] (int i) const
00056 {
00057     return i < d_length && i >= 0 ? d_array_p[i] : 0;
00058 }
00059 
00060 int idep_NameArray::length() const
00061 {
00062     return d_length;
00063 }
00064 
00065 std::ostream& operator<<(std::ostream& out, const idep_NameArray& array) 
00066 {
00067     int fieldWidth = 10;
00068     int maxIndex = array.length() - 1;
00069     assert (sizeof (long int) >= 4);
00070     long int x = 1000 * 1000 * 1000;    // requires 4-byte integer.
00071     while (fieldWidth > 1 && 0 == maxIndex / x) {
00072         --fieldWidth;  
00073         x /= 10;
00074     }
00075 
00076     for (int i = 0; i < array.length(); ++i) {
00077         out.width(fieldWidth);
00078         out << i << ". " << array[i] << std::endl;
00079     }
00080 
00081     return out;
00082 }
00083 

Generated on Mon Feb 15 11:06:48 2010 for loon by  doxygen 1.3.9.1