-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhelperlib.c
152 lines (129 loc) · 3.74 KB
/
helperlib.c
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
#include "helperlib.h"
#define MAX_LEN 2000
void print_system_error(char *message)
{
perror(message);
exit(1);
}
void print_user_error(char *message, char *detail)
{
fprintf(stderr, "%s: %s\n", message, detail);
exit(1);
}
/* The comparison scheme is case-insensitive */
int find_str_index(char *strArr[], int numStr, char *str)
{
int index;
for(index = 0; index < numStr; index++)
{
if(strcasecmp(strArr[index], str) == 0)
return index;
}
return -1;
}
/* The comparison scheme is case-sensitive */
int find_occurrence_index(char *str, char character, int occurence)
{
int index, curr_occur;
for(index = 0, curr_occur = 0; str[index] != '\0'; index++)
if(str[index] == character)
{
curr_occur++;
if(curr_occur == occurence)
return index;
}
return -1;
}
/* copy_str_dynamic(...)
*
* Copies the content of the input string to new dynamically allocated memory slots.
*
* Returns a pointer to the beginning of the new memory slots.
* Returns NULL if there is an exception in allocation or copying.
*/
char * copy_str_dynamic(char * originalStr)
{
if(originalStr == NULL)
return NULL;
/* NOTE: Make 1 more SLOT of '\0' !!! */
char * newStrPointer = (char *) malloc((strlen(originalStr) + 1) * sizeof(char));
if(newStrPointer == NULL)
{
printf("error with malloc\n");
return NULL;
}
strcpy(newStrPointer, originalStr);
return newStrPointer;
}
/* count_occurence(...)
*
* Returns the number of occurences of the input character in the given string.
*
* The comparison scheme is case-sensitive.
*/
int count_occurence(char *str, char character)
{
int num = 0, index;
if(str != NULL)
{
for(index = 0; str[index] != '\0'; index++)
if(str[index] == character)
num++;
}
return num;
}
/* read_whole_file(...)
*
* Copies the whole content of a file to a dynamically allocated memory slots.
*
* Returns a pointer to the beginning of the content. Upon sucess, *error_code = 0.
* Returns NULL if there is an error when opening the file or allocating mmemory. If so,
* *error_code = 1 if there is an error opening the file.
* *error_code = 2 if there is an error allocating memory
*
* Note: Users have to free the memory allocated for *error_message themselves
*/
char * read_whole_file(char * relative_path, int *error_code)
{
char *buffer;
int numByte;
printf("Go to function read_whole_file\n");
/* For POSIX conforming system, "r" and "rb" are the same */
FILE *fin = fopen(relative_path, "rb");
if(fin == NULL)
{
*error_code = 1;
return NULL;
}
printf("Valid path OK\n");
/* Set the file position indicator to the end of the stream */
fseek(fin, 0, SEEK_END);
/* Determine the total number of bytes of the stream */
numByte = ftell(fin);
/* Set the file position indicator to the beginning of the stream */
rewind(fin);
printf("Determine size = %d: OK\n", numByte);
buffer = (char *) malloc((numByte + 1) * sizeof(char));
if(buffer == NULL)
{
*error_code = 2;
return NULL;
}
fread(buffer, numByte, 1, fin);
buffer[numByte] = '\0';
printf("Length buffer = %d vs Byte = %d\n", strlen(buffer), numByte);
printf("Read OK\n");
fclose(fin);
printf("Close file OK\n");
/* Upon success, *error_code = 0 */
*error_code = 0;
printf("Totally OK?\n");
puts(buffer);
return buffer;
}
char * generate_html_error_page(char * header, char * error_message)
{
char buffer[MAX_LEN + 1];
sprintf(buffer, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n<title>%s</title>\n</head>\n<body><h1>%s</h1><p>%s</p></body></html>", header, header, error_message);
return copy_str_dynamic(buffer);
}