1 //////////////////////////////////////////////////////////////////////////////
2 //Copyright 2008
3 //  Andrew Gacek, Steven Holte, Gopalan Nadathur, Xiaochu Qi, Zach Snow
4 //////////////////////////////////////////////////////////////////////////////
5 // This file is part of Teyjus.                                             //
6 //                                                                          //
7 // Teyjus is free software: you can redistribute it and/or modify           //
8 // it under the terms of the GNU General Public License as published by     //
9 // the Free Software Foundation, either version 3 of the License, or        //
10 // (at your option) any later version.                                      //
11 //                                                                          //
12 // Teyjus is distributed in the hope that it will be useful,                //
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of           //
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
15 // GNU General Public License for more details.                             //
16 //                                                                          //
17 // You should have received a copy of the GNU General Public License        //
18 // along with Teyjus.  If not, see <http://www.gnu.org/licenses/>.          //
19 //////////////////////////////////////////////////////////////////////////////
20 
21 #include <stdlib.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include "util.h"
25 
26 
27 /**************************************************************************/
28 /* Space allocation                                                       */
29 /**************************************************************************/
30 /* allocate space of n bytes*/
UTIL_malloc(size_t n)31 void* UTIL_malloc(size_t n)
32 {
33     void* ptr = (void*)malloc(n);
34     if (ptr) return ptr;
35     printf("Error : cannot allocate space\n");
36     exit(1);
37 
38 }
39 
40 
41 /* allocate space for a string of given size */
UTIL_mallocStr(size_t size)42 char* UTIL_mallocStr(size_t size)
43 {
44     char* ptr = (char*)malloc(sizeof(char)*(size + 1));
45     if (ptr) return ptr;
46 
47     printf("Error : cannot allocate space\n");
48     exit(1);
49 }
50 
51 
52 /**************************************************************************/
53 /* string operation                                                       */
54 /**************************************************************************/
55 /* Append two strings */
UTIL_appendStr(char * str1,char * str2)56 char* UTIL_appendStr(char* str1, char* str2)
57 {
58   size_t length = strlen(str1) + strlen(str2);
59   char* ptr = UTIL_mallocStr(length + 1);
60 
61   strcpy(ptr, str1);
62   strcat(ptr, str2);
63 
64   return ptr;
65 }
66 
67 
68 //convert lower case letters in a string to upper case ones
UTIL_upperCase(char * str)69 char* UTIL_upperCase(char* str)
70 {
71     char *newstr, *tmp;
72     newstr = strdup(str);
73     tmp = newstr;
74     while ((*tmp) != '\0'){
75         if ((97 <= (int)*tmp) && ((int)*tmp <= 122))
76             *tmp = (char)((int)*tmp - 32);
77         tmp++;
78     }
79     return newstr;
80 }
81 
82 //convert to lower cases
UTIL_lowerCase(char * str)83 char* UTIL_lowerCase(char* str)
84 {
85     char *newstr, *tmp;
86     newstr = strdup(str);
87     tmp = newstr;
88     while ((*tmp) != '\0'){
89         if ((65 <= (int)*tmp) && ((int)*tmp) <= 90)
90             *tmp = (char)((int)*tmp + 32);
91         tmp++;
92     }
93     return newstr;
94 }
95 
96 //covert an non-negtive integer to string
UTIL_itoa(int num)97 char* UTIL_itoa(int num)
98 {
99     char *str = UTIL_mallocStr(33);
100     sprintf(str, "%d", num);
101     return str;
102 }
103 
104 
105 /**************************************************************************/
106 /* file operation                                                         */
107 /**************************************************************************/
108 
109 /* open file in read mode */
UTIL_fopenR(char * filename)110 FILE* UTIL_fopenR(char* filename)
111 {
112     FILE* filePtr = fopen(filename, "r");
113     if (filePtr) return filePtr;
114 
115     printf("Error : cannot open input file %s\n", filename);
116     exit(1);
117 }
118 
119 
120 /* open file in write mode */
UTIL_fopenW(char * filename)121 FILE* UTIL_fopenW(char* filename)
122 {
123     FILE* filePtr = fopen(filename, "w");
124     if (filePtr) return filePtr;
125 
126     printf("Error : cannot open output file %s\n", filename);
127     exit(1);
128 }
129 
130 /* close file */
UTIL_fclose(FILE * file)131 void UTIL_fclose(FILE* file)
132 {
133     fclose(file);
134 }
135 
136