File: bac_entry_to_string.c

package info (click to toggle)
tenmado 0.10-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,208 kB
  • sloc: ansic: 22,906; sh: 1,072; yacc: 321; makefile: 300; lex: 170
file content (190 lines) | stat: -rw-r--r-- 5,299 bytes parent folder | download | duplicates (6)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* $Id: bac_entry_to_string.c,v 1.24 2003/01/14 03:56:36 oohara Exp $ */

#include <stdio.h>
/* malloc */
#include <stdlib.h>
/* strdup, strlen */
#include <string.h>
/* time_t */
#include <time.h>
/* uid_t */
#include <sys/types.h>
/* CHAR_BIT */
#include <limits.h>
/* isprint */
#include <ctype.h>

#include "bac_entry_to_string.h"

char *
bac_entry_to_string(bac_entry *entry)
{
  int i;
  char *temp = NULL;
  char *temp2 = NULL;
  int length = 0;
  int length_used = 0;

  /* sanity check */
  if (entry == NULL)
  {
    fprintf(stderr, "bac_entry_to_string: entry is NULL\n");
    return NULL;
  }
  if (entry->klass != 0)
  {
    fprintf(stderr, "bac_entry_to_string: not supported score_entry_version "
            "(%d)\n", entry->klass);
    return NULL;
  }
  if ((((bac_entry_ver_0 *) entry)->user_name_available != 0)
      && (((bac_entry_ver_0 *) entry)->user_name == NULL))
  {
    fprintf(stderr, "bac_entry_to_string: entry->user_name is NULL\n");
    return NULL;
  }
  if (((bac_entry_ver_0 *) entry)->number_stage <= 0)
  {
    fprintf(stderr, "bac_entry_to_string: entry->number_stage is "
            "non-positive\n");
    return NULL;
  }
  if (((bac_entry_ver_0 *) entry)->score_stage == NULL)
  {
    fprintf(stderr, "bac_entry_to_string: entry->score_stage is NULL\n");
    return NULL;
  }

  if (isprint('\n'))
  {
    fprintf(stderr, "bac_entry_to_string: \\n is printable\n");
    return NULL;
  }
  if (isprint('\t'))
  {
    fprintf(stderr, "bac_entry_to_string: \\t is printable\n");
    return NULL;
  }

  /* entry_version */
  length += strlen("Entry-Version") + strlen("\t") + strlen("\n");
  length += CHAR_BIT * sizeof(int);
  /* stage_data_version */
  length += strlen("Stage-Data-Version") + strlen("\t") + strlen("\n");
  length += CHAR_BIT * sizeof(int);
  /* when */
  length += strlen("When") + strlen("\t") + strlen("\n");
  length += CHAR_BIT * sizeof(time_t);
  /* uid */
  length += strlen("Uid") + strlen("\t") + strlen("\n");
  length += CHAR_BIT * sizeof(uid_t);
  /* there is no field for user_name_available */
  if (((bac_entry_ver_0 *) entry)->user_name_available != 0)
  {
    /* user_name */
    length += strlen("User-Name") + strlen("\t") + strlen("\n");
    length += strlen(((bac_entry_ver_0 *) entry)->user_name);
  }
  /* score_sort */
  length += strlen("Score-Sort") + strlen("\t") + strlen("\n");
  length += CHAR_BIT * sizeof(int);
  /* score_total */
  length += strlen("Score-Total") + strlen("\t") + strlen("\n");
  length += CHAR_BIT * sizeof(int);
  /* number_stage */
  length += strlen("Number-Stage") + strlen("\t") + strlen("\n");
  length += CHAR_BIT * sizeof(int);
  /* score_stage */
  length += (strlen("Score-Stage") + strlen("\t") * 2 + strlen("\n"))
    * ((bac_entry_ver_0 *) entry)->number_stage;
  length += CHAR_BIT * sizeof(int) * 2
    * ((bac_entry_ver_0 *) entry)->number_stage;
  /* terminating \n */
  length += strlen("\n");
  /* trailing \0 */
  length += 1;

  temp = (char *) malloc(sizeof(char) * length);
  if (temp == NULL)
  {
    fprintf(stderr, "bac_entry_to_string: malloc failed\n");
    return NULL;
  }

  sprintf(temp, "Entry-Version\t%d\n"
           "Stage-Data-Version\t%d\n"
           "When\t%d\n"
           "Uid\t%d\n",
           0,
           ((bac_entry_ver_0 *) entry)->stage_data_version,
           (int) (((bac_entry_ver_0 *) entry)->when),
           (int) (((bac_entry_ver_0 *) entry)->uid));
  /* be paranoid */
  temp[length - 1] = '\0';
  length_used = strlen(temp);
  if (length_used >= length)
  {
    fprintf(stderr, "bac_entry_to_string: temp is too short\n");
    free(temp);
    return NULL;
  }
  if (((bac_entry_ver_0 *) entry)->user_name_available != 0)
  {
    sprintf(temp + length_used,
             "User-Name\t%s\n",
             ((bac_entry_ver_0 *) entry)->user_name);
    temp[length - 1] = '\0';
    length_used = strlen(temp);
    if (length_used >= length)
    {
      fprintf(stderr, "bac_entry_to_string: temp is too short\n");
      free(temp);
      return NULL;
    }
  }
  sprintf(temp + length_used,
           "Score-Sort\t%d\n"
           "Score-Total\t%d\n"
           "Number-Stage\t%d\n",
           ((bac_entry_ver_0 *) entry)->score_sort,
           ((bac_entry_ver_0 *) entry)->score_total,
           ((bac_entry_ver_0 *) entry)->number_stage);
  temp[length - 1] = '\0';
  for (i = 1; i <= ((bac_entry_ver_0 *) entry)->number_stage; i++)
  {
    length_used = strlen(temp);
    if (length_used >= length)
    {
      fprintf(stderr, "bac_entry_to_string: temp is too short\n");
      free(temp);
      return NULL;
    }
    sprintf(temp + length_used,
             "Score-Stage\t%d\t%d\n", i,
             ((bac_entry_ver_0 *) entry)->score_stage[i - 1]);
    temp[length - 1] = '\0';
  }
  
  length_used = strlen(temp);
  if (length_used + 1 >= length)
  {
    fprintf(stderr, "bac_entry_to_string: temp is too short\n");
    free(temp);
    return NULL;
  }
  temp[length_used] = '\n';
  temp[length_used + 1] = '\0';

  /* free unused memory */
  /* +2 (not +1) is for the trailing \0 */
  temp2 = (char *) realloc(temp, sizeof(char) * ((size_t) (length_used + 2)));
  if (temp2 == NULL)
  {
    fprintf(stderr, "bac_entry_to_string: realloc failed\n");
    free(temp);
    return NULL;
  }
  temp = temp2;

  return temp;
}