Open64 (mfef90, whirl2f, and IR tools)
TAG: version-openad; SVN changeset: 916
|
00001 /* 00002 00003 Copyright (C) 2000, 2001 Silicon Graphics, Inc. All Rights Reserved. 00004 00005 This program is free software; you can redistribute it and/or modify it 00006 under the terms of version 2 of the GNU General Public License as 00007 published by the Free Software Foundation. 00008 00009 This program is distributed in the hope that it would be useful, but 00010 WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00012 00013 Further, this software is distributed without any warranty that it is 00014 free of the rightful claim of any third person regarding infringement 00015 or the like. Any license provided herein, whether implied or 00016 otherwise, applies only to this software file. Patent licenses, if 00017 any, provided herein do not apply to combinations of this program with 00018 other software, or any other product whatsoever. 00019 00020 You should have received a copy of the GNU General Public License along 00021 with this program; if not, write the Free Software Foundation, Inc., 59 00022 Temple Place - Suite 330, Boston MA 02111-1307, USA. 00023 00024 Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pky, 00025 Mountain View, CA 94043, or: 00026 00027 http://www.sgi.com 00028 00029 For further information regarding this notice, see: 00030 00031 http://oss.sgi.com/projects/GenInfo/NoticeExplan 00032 00033 */ 00034 00035 00036 /* -*-Mode: c++;-*- (Tell emacs to use c++ mode) */ 00037 00038 #include <errno.h> // for sys_errlist 00039 #include <stdio.h> // for put/get and print/read 00040 #include <libgen.h> // for basename() 00041 #include <sys/stat.h> 00042 #include "anl_common.h" // For ANL_DIAGNOSTICS, etc. 00043 #include "anl_file_mngr.h" 00044 00045 00046 // Avoid errors due to uses of "int" in stdio.h macros. 00047 // 00048 #undef int 00049 00050 00051 // =================== Static Utilities =================== 00052 // ======================================================== 00053 00054 const INT ANL_FILE_MNGR::_obuf_size; 00055 00056 00057 // =============== Hidden Member Functions ================ 00058 // ======================================================== 00059 00060 00061 void 00062 ANL_FILE_MNGR::_Concat(char *buf, 00063 INT max_chars, 00064 const char *string[], 00065 INT num_strings) 00066 { 00067 INT char_cntr = 0; 00068 00069 for (INT i=0; i < num_strings; i++) 00070 { 00071 const char *s = string[i]; 00072 00073 for (INT j=0; char_cntr < max_chars && s[j] != '\0'; j++) 00074 buf[char_cntr++] = s[j]; 00075 } 00076 if (char_cntr < max_chars) 00077 buf[char_cntr] = '\0'; 00078 else 00079 buf[max_chars-1] = '\0'; 00080 } // ANL_FILE_MNGR::_Concat 00081 00082 00083 UINT64 00084 ANL_FILE_MNGR::_Get_Decimal_Number(INT ch) 00085 { 00086 // Returns UINT64_MAX if illegal character 00087 // 00088 switch (ch) 00089 { 00090 case '0': 00091 return 0; 00092 case '1': 00093 return 1; 00094 case '2': 00095 return 2; 00096 case '3': 00097 return 3; 00098 case '4': 00099 return 4; 00100 case '5': 00101 return 5; 00102 case '6': 00103 return 6; 00104 case '7': 00105 return 7; 00106 case '8': 00107 return 8; 00108 case '9': 00109 return 9; 00110 default: 00111 return UINT64_MAX; // illegal character 00112 } 00113 } // ANL_FILE_MNGR::_Get_Decimal_Number 00114 00115 00116 UINT64 00117 ANL_FILE_MNGR::_Get_Hex_Number(INT ch) 00118 { 00119 // Returns UINT64_MAX if illegal character 00120 // 00121 switch (ch) 00122 { 00123 case 'a': 00124 return 10; 00125 case 'b': 00126 return 11; 00127 case 'c': 00128 return 12; 00129 case 'd': 00130 return 13; 00131 case 'e': 00132 return 14; 00133 case 'f': 00134 return 15; 00135 case 'A': 00136 return 10; 00137 case 'B': 00138 return 11; 00139 case 'C': 00140 return 12; 00141 case 'D': 00142 return 13; 00143 case 'E': 00144 return 14; 00145 case 'F': 00146 return 15; 00147 case '0': 00148 return 0; 00149 case '1': 00150 return 1; 00151 case '2': 00152 return 2; 00153 case '3': 00154 return 3; 00155 case '4': 00156 return 4; 00157 case '5': 00158 return 5; 00159 case '6': 00160 return 6; 00161 case '7': 00162 return 7; 00163 case '8': 00164 return 8; 00165 case '9': 00166 return 9; 00167 default: 00168 return UINT64_MAX; // illegal character 00169 } 00170 } // ANL_FILE_MNGR::_Get_Hex_Number 00171 00172 00173 BOOL 00174 ANL_FILE_MNGR::_Exists(const char *name) 00175 { 00176 INT st; 00177 struct stat sbuf; 00178 st = stat(name, &sbuf); 00179 if (st == -1 && (errno == ENOENT || errno == ENOTDIR)) 00180 return FALSE; 00181 else 00182 return TRUE; 00183 } // ANL_FILE_MNGR::_Exists 00184 00185 00186 void 00187 ANL_FILE_MNGR::_General_Check(BOOL c, const char *proc_name, const char *msg) 00188 { 00189 if (!c) 00190 { 00191 char strbuf[500]; 00192 const char *strlist[5] = {proc_name, msg, " (", _name, ")"}; 00193 _Concat(strbuf, 500, strlist, 5); 00194 _diag->Warning(strbuf); 00195 } 00196 } // ANL_FILE_MNGR::_General_Check 00197 00198 00199 void 00200 ANL_FILE_MNGR::_Not_Open_Check(const char *proc_name, const char *to_be_opened) 00201 { 00202 if (File_Is_Open()) 00203 { 00204 char strbuf[500]; 00205 const char *strlist[5] = {proc_name, 00206 " will close unexpected open file ", 00207 _name, 00208 " and then open. ", 00209 to_be_opened}; 00210 _Concat(strbuf, 500, strlist, 5); 00211 _diag->Warning(strbuf); 00212 Close_File(); 00213 } 00214 } // ANL_FILE_MNGR::_Not_Open_Check 00215 00216 00217 void 00218 ANL_FILE_MNGR::_Is_Open_Check(const char *proc_name) 00219 { 00220 if (!File_Is_Open()) 00221 { 00222 char strbuf[500]; 00223 const char *strlist[2] = {proc_name, 00224 " expected a file to have been opened"}; 00225 _Concat(strbuf, 500, strlist, 2); 00226 _diag->Error(strbuf); 00227 } 00228 } // ANL_FILE_MNGR::_Is_Open_Check 00229 00230 00231 void 00232 ANL_FILE_MNGR::_Overwrite_Warning(const char *proc_name, const char *filename) 00233 { 00234 if (_Exists(filename)) 00235 { 00236 char strbuf[500]; 00237 const char *strlist[4] = {filename, 00238 " will be overwritten (by ", 00239 proc_name, 00240 ")"}; 00241 _Concat(strbuf, 500, strlist, 4); 00242 _diag->Warning(strbuf); 00243 } 00244 } // ANL_FILE_MNGR::_Overwrite_Warning 00245 00246 00247 void ANL_FILE_MNGR::_Write_Obuf() 00248 { 00249 INT status; 00250 00251 _obuf[_next_obuf] = '\0'; 00252 status = fputs(_obuf, _file); 00253 _next_obuf = 0; 00254 _General_Check(status != EOF, 00255 "ANL_FILE_MNGR::_Write_Obuf", 00256 "cannot write to file"); 00257 } // ANL_FILE_MNGR::_Write_Obuf 00258 00259 00260 // =============== Public Member Functions ================ 00261 // ======================================================== 00262 00263 void 00264 ANL_FILE_MNGR::Open_Read(const char *name) 00265 { 00266 _Not_Open_Check("ANL_FILE_MNGR::Open_Read", name); 00267 _file = fopen(name, "r"); 00268 _Is_Open_Check("ANL_FILE_MNGR::Open_Read"); 00269 _name = name; 00270 _next_ch = getc(_file); 00271 _General_Check(!ferror(_file), 00272 "ANL_FILE_MNGR::Open_Read", 00273 "cannot read first character in file"); 00274 } // ANL_FILE_MNGR::Open_Read 00275 00276 00277 void 00278 ANL_FILE_MNGR::Open_Create(const char *name) 00279 { 00280 _Not_Open_Check("ANL_FILE_MNGR::Open_Create", name); 00281 _Overwrite_Warning("ANL_FILE_MNGR::Open_Create", name); 00282 _file = fopen(name, "w"); 00283 _Is_Open_Check("ANL_FILE_MNGR::Open_Create"); 00284 _name = name; 00285 } // ANL_FILE_MNGR::Open_Create 00286 00287 00288 void 00289 ANL_FILE_MNGR::Open_Append(const char *name) 00290 { 00291 _Not_Open_Check("ANL_FILE_MNGR::Open_Append", name); 00292 _file = fopen(name, "a"); 00293 _Is_Open_Check("ANL_FILE_MNGR::Open_Append"); 00294 _name = name; 00295 } // ANL_FILE_MNGR::Open_Append 00296 00297 00298 void 00299 ANL_FILE_MNGR::Close_File() 00300 { 00301 INT32 status; 00302 00303 if (_next_obuf > 0) 00304 _Write_Obuf(); 00305 status = fclose(_file); 00306 _General_Check(status == 0, 00307 "ANL_FILE_MNGR::Close_File", 00308 "cannot close file"); 00309 _file = NULL; 00310 _name = NULL; 00311 } // ANL_FILE_MNGR::Close_File 00312 00313 00314 void 00315 ANL_FILE_MNGR::Close_And_Remove_File() 00316 { 00317 INT32 status; 00318 00319 if (_next_obuf > 0) 00320 _Write_Obuf(); 00321 status = fclose(_file); 00322 _General_Check(status == 0, 00323 "ANL_FILE_MNGR::Close_File", 00324 "cannot close file"); 00325 remove(_name); 00326 _file = NULL; 00327 _name = NULL; 00328 } // ANL_FILE_MNGR::Close_And_Remove_File 00329 00330 00331 char 00332 ANL_FILE_MNGR::Read_Char() 00333 { 00334 INT32 ch; 00335 00336 _General_Check(!End_Of_File(), 00337 "ANL_FILE_MNGR::Read_Char", 00338 "attempt to read beyond the end of the file"); 00339 ch = _next_ch; 00340 _next_ch = getc(_file); 00341 return ch; 00342 } // ANL_FILE_MNGR::Read_Char 00343 00344 00345 UINT64 00346 ANL_FILE_MNGR::Read_Uint64(BOOL as_hex) 00347 { 00348 INT32 num_digits = 0; 00349 UINT64 result = 0; 00350 UINT64 digit; 00351 00352 _General_Check(!End_Of_File(), 00353 "ANL_FILE_MNGR::Read_Uint64", 00354 "attempt to read beyond the end of the file"); 00355 if (as_hex) 00356 { 00357 if (_next_ch == '0') 00358 { 00359 _next_ch = getc(_file); 00360 if (_next_ch == 'x' || _next_ch == 'X') 00361 _next_ch = getc(_file); 00362 else 00363 num_digits++; 00364 } 00365 00366 for (digit = _Get_Hex_Number(_next_ch); 00367 digit != UINT64_MAX; 00368 digit = _Get_Hex_Number(_next_ch)) 00369 { 00370 _next_ch = getc(_file); 00371 result = result*16 + digit; 00372 num_digits++; 00373 } 00374 _General_Check(num_digits > 0 && num_digits <= 17, 00375 "ANL_FILE_MNGR::Read_Uint64", 00376 "unexpected syntax"); 00377 } 00378 else 00379 { 00380 for (digit = _Get_Decimal_Number(_next_ch); 00381 digit != UINT64_MAX; 00382 digit = _Get_Decimal_Number(_next_ch)) 00383 { 00384 _next_ch = getc(_file); 00385 result = result*10 + digit; 00386 num_digits++; 00387 } 00388 _General_Check(num_digits > 0 && num_digits <= 17, 00389 "ANL_FILE_MNGR::Read_Uint64", 00390 "unexpected syntax"); 00391 } 00392 00393 return result; 00394 } // ANL_FILE_MNGR::Read_Uint64 00395 00396 00397 void 00398 ANL_FILE_MNGR::Write_Char(char c) 00399 { 00400 if (_next_obuf >= _obuf_size) 00401 _Write_Obuf(); 00402 00403 _obuf[_next_obuf++] = c; 00404 } // ANL_FILE_MNGR::Write_Char 00405 00406 00407 void 00408 ANL_FILE_MNGR::Write_String(const char *s) 00409 { 00410 if (s != NULL) 00411 for (const char *p = s; *p != '\0'; p++) 00412 { 00413 if (_next_obuf >= _obuf_size) 00414 _Write_Obuf(); 00415 _obuf[_next_obuf++] = *p; 00416 } 00417 } // ANL_FILE_MNGR::Write_String