Thread vector in C? (22 answers)
Opened by renee at 2005-05-04 13:10

esskar
 2005-05-05 03:48
#11166 #11166
User since
2003-08-04
7321 Artikel
ModeratorIn

user image
habs nicht getestet oder kompiliert; ist aber vielleicht ein Anfang mit dem du arbeiten kannst!

Code: (dl )
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
#define max(x, y) ( (x) > (y) ? (x) : (y) )

typedef struct tagString {
   char *Buffer;
   unsigned int Length;
   unsigned int Size;
} String;

String *str_create(const char *str) {
   String *retval = (String *)malloc(sizeof(String));

   memset(retval, 0, sizeof(String));
   if(str != NULL) {
       retval->Length = strlen(str);
       retval->Size = max(retval->Length + 1, 64);
       retval->Buffer = (char *)malloc(retval->Size);
       strncpy(retval->Buffer, str, retval->Size);
   }

   return retval;
}

String *str_destroy(String *str) {
   if(str != NULL) {
       if(str->Buffer != NULL) free(str->Buffer);
       free(str);
   }

   return NULL;
}

String *str_concate(String *str, const char *buffer) {
   if(str != NULL && str->Buffer != NULL && buffer != NULL) {
       int len = strlen(buffer);
       if(str->Length + len >= str->Size) {
          str->Size += max(len + 1, 64);
          str->Buffer = (char *)realloc(str->Buffer, str->Size);
       }
       strncat(str->Buffer, buffer, str->Size);
   }

   return str;
}

typedef struct tagStringVector {
  String **Array;
  unsigned int Length;
  unsigned int Size;
} StringVector;

StringVector *strvec_create() {
  StringVector *retval = (StringVector *)malloc(sizeof(StringVector));

  memset(retval, 0, sizeof(StringVector));

  return retval;
}

StringVector *strvec_destroy(StringVector *strvec) {
  if(strvec != NULL) {
     unsigned int i;
     for(i = 0; i < strvec->Length; ++i) {
        strvec->Array[i] = str_destroy(strvec->Array[i]);
     }
     if(strvec->Array != NULL) free(strvec->Array);
     free(strvec);
  }
  return NULL;
}

void strvec_push(StringVector *strvec, const char *buffer) {
   String *str = str_create(buffer);

   if(strvec->Length >= strvec->Size) {
       strvec->Size += 8;
       if(strvec->Array == NULL)
           strvec->Array = (String **)malloc(
               strvec->Size * sizeof(String *)
           );
       else
           strvec->Array = (String **)realloc(
               strvec->Array,
               strvec->Size * sizeof(String *)
           );
   }

   strvec->Array[strvec->Length++] = str;
}

String *strvec_str_at(StringVector *strvec, unsigned int idx) {
   if(strvec && strvec->Length > idx)
       return strvec->Array[idx];
   else
       return NULL;
}

const char *strvec_ptr_at(StringVector *strvec, unsigned int idx) {
   String *str = strvec_str_at(strvec, idx);
   if(str != NULL)
       return str->Buffer;
   else
       return NULL;
}


Fragen?\n\n

<!--EDIT|esskar|1115659358-->

View full thread vector in C?