-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.c
More file actions
92 lines (80 loc) · 2.14 KB
/
common.c
File metadata and controls
92 lines (80 loc) · 2.14 KB
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
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "easyhpc.h"
void* xmalloc (size_t size)
{
void* ptr = malloc (size);
/* Abort if the allocation failed. */
if (ptr == NULL)
abort ();
else
return ptr;
}
void* xrealloc (void* ptr, size_t size)
{
ptr = realloc (ptr, size);
/* Abort if the allocation failed. */
if (ptr == NULL)
abort ();
else
return ptr;
}
char* xstrdup (const char* s)
{
char* copy = strdup (s);
/* Abort if the allocation failed. */
if (copy == NULL)
abort ();
else
return copy;
}
void system_error (const char* operation)
{
/* Generate an error message for errno. */
error (operation, strerror (errno));
}
void error (const char* cause, const char* message)
{
/* Print an error message to stderr. */
fprintf (stderr, "%s: error: (%s) %s\n", "easyhpc", cause, message);
/* End the program. */
exit (1);
}
/**********************************************************************************
* @author: Forrest Ling
* @brief: printf with string array *str_ary[] in format with %s
***********************************************************************************/
int prn_fmt_ary ( char * tar, char * fmt, char ** str_ary )
{
int ret = 0;
int off = 0; // fmt off
int i = 0; // str_ary off
while ( fmt[off] != '\0' ) {
if ( fmt[off] == '%' && fmt[off+1] == 's' ) // if fmt has "%s"
{
if ( str_ary[i] != NULL )
ret += sprintf ( tar + ret, "%s", str_ary[i++] );
off += 2 ;
} else {
tar[ret++] = fmt[off++] ;
}
}
tar[ret++] = '\0';
return ret;
}
/**********************************************************************************
* @author: Forrest Ling
* @brief: convert string into str array with seperator
**********************************************************************************/
int str_to_ary ( char ** str_ary, char * str, char * sep )
{
int i = 0;
str_ary[i] = strtok (str, sep);
while (str_ary[i] != NULL) {
str_ary[++i] = strtok(NULL, sep);
};
return ++i;
}