1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "main.h"
/************ Implementation of std::string<char> in C ***********/
/********** Constructor/Destructor ***********/
void String_constructor (struct String *this)
{
printf ("%p: String_constructor\n", this);
this->elements = malloc (1);
strcpy (this->elements, "");
}
void String_copyConstructor (struct String *this, const struct String *o)
{
printf ("%p: String_copyConstructor: from %p\n", this, o);
this->elements = malloc (strlen (o->elements) + 1);
strcpy (this->elements, o->elements);
}
void String_destructor (const struct String *this)
{
printf ("%p: String_destructor\n", this);
free (this->elements);
}
/********** Capacity ***********/
int String_size (const struct String *this)
{
return strlen (this->elements);
}
/********** Element access ***********/
const char *String_get (const struct String *this)
{
return this->elements;
}
/********** Modifier ***********/
void String_assign (struct String *this, const char *str)
{
printf ("%p: String_assign: %s\n", this, str);
this->elements = (char *)realloc (this->elements, strlen(str) + 1);
strcpy (this->elements, str);
}
/************ Implementation of std::vector<std::String> in C ***********/
/********** Constructor/Destructor ***********/
void StringVector_constructor (struct StringVector *this)
{
printf ("%p: StringVector_constructor\n", this);
this->elements = malloc (1);
this->size = 0;
this->capacity = 1;
}
void StringVector_destructor (const struct StringVector *this)
{
size_t i;
printf ("%p: StringVector_destructor\n", this);
for (i = 0; i < this->size; i++)
String_destructor (&this->elements[i]);
free (this->elements);
}
/********** Modifier ***********/
size_t StringVector_push_back (struct StringVector *this,
const struct String *string)
{
size_t i;
struct String *relocatedElements;
printf ("%p: StringVector_push_back\n", this);
if (this->size == this->capacity) {
printf ("%p: StringVector_push_back: re-allocating\n", this);
this->capacity = 2 * this->capacity;
relocatedElements = (struct String *)
malloc (this->capacity * sizeof (struct String));
for (i = 0; i < this->size; i++)
String_copyConstructor (&relocatedElements[i],
&this->elements[i]);
for (i = 0; i < this->size; i++)
String_destructor (&this->elements[i]);
free (this->elements);
this->elements = relocatedElements;
printf ("%p: StringVector_push_back: re-allocation complete\n",
this);
}
String_copyConstructor (&this->elements[this->size], string);
this->size++;
}
/********** Element access ***********/
const struct String *StringVector_getElement (const struct StringVector *this,
size_t i)
{
return &this->elements[i];
}
/********** Capacity ***********/
size_t StringVector_size (const struct StringVector *this)
{
return this->size;
}
/********************* Main program ***********************/
void showTime (const struct StringVector *vector)
{
size_t i;
for (i = 0; i < StringVector_size (vector); i++)
printf ("%s\n",
String_get (StringVector_getElement (vector, i)));
}
int main (void)
{
struct StringVector stringVector;
StringVector_constructor (&stringVector);
struct String str1;
String_constructor (&str1);
String_assign (&str1, "Hi");
StringVector_push_back (&stringVector, &str1);
String_destructor (&str1);
struct String str2;
String_constructor (&str2);
String_assign (&str2, "Hello");
StringVector_push_back (&stringVector, &str2);
String_destructor (&str2);
showTime (&stringVector);
StringVector_destructor (&stringVector);
return 0;
}
|