iohb.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  1. // (C) Copyright Jeremy Siek 2004
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. /*
  6. Fri Aug 15 16:29:47 EDT 1997
  7. Harwell-Boeing File I/O in C
  8. V. 1.0
  9. National Institute of Standards and Technology, MD.
  10. K.A. Remington
  11. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  12. NOTICE
  13. Permission to use, copy, modify, and distribute this software and
  14. its documentation for any purpose and without fee is hereby granted
  15. provided that the above copyright notice appear in all copies and
  16. that both the copyright notice and this permission notice appear in
  17. supporting documentation.
  18. Neither the Author nor the Institution (National Institute of Standards
  19. and Technology) make any representations about the suitability of this
  20. software for any purpose. This software is provided "as is" without
  21. expressed or implied warranty.
  22. ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  23. ---------------------
  24. INTERFACE DESCRIPTION
  25. ---------------------
  26. ---------------
  27. QUERY FUNCTIONS
  28. ---------------
  29. FUNCTION:
  30. int readHB_info(const char *filename, int *M, int *N, int *nz,
  31. char **Type, int *Nrhs)
  32. DESCRIPTION:
  33. The readHB_info function opens and reads the header information from
  34. the specified Harwell-Boeing file, and reports back the number of rows
  35. and columns in the stored matrix (M and N), the number of nonzeros in
  36. the matrix (nz), the 3-character matrix type(Type), and the number of
  37. right-hand-sides stored along with the matrix (Nrhs). This function
  38. is designed to retrieve basic size information which can be used to
  39. allocate arrays.
  40. FUNCTION:
  41. int readHB_header(FILE* in_file, char* Title, char* Key, char* Type,
  42. int* Nrow, int* Ncol, int* Nnzero, int* Nrhs,
  43. char* Ptrfmt, char* Indfmt, char* Valfmt, char* Rhsfmt,
  44. int* Ptrcrd, int* Indcrd, int* Valcrd, int* Rhscrd,
  45. char *Rhstype)
  46. DESCRIPTION:
  47. More detailed than the readHB_info function, readHB_header() reads from
  48. the specified Harwell-Boeing file all of the header information.
  49. ------------------------------
  50. DOUBLE PRECISION I/O FUNCTIONS
  51. ------------------------------
  52. FUNCTION:
  53. int readHB_newmat_double(const char *filename, int *M, int *N, *int nz,
  54. int **colptr, int **rowind, double**val)
  55. int readHB_mat_double(const char *filename, int *colptr, int *rowind,
  56. double*val)
  57. DESCRIPTION:
  58. This function opens and reads the specified file, interpreting its
  59. contents as a sparse matrix stored in the Harwell/Boeing standard
  60. format. (See readHB_aux_double to read auxillary vectors.)
  61. -- Values are interpreted as double precision numbers. --
  62. The "mat" function uses _pre-allocated_ vectors to hold the index and
  63. nonzero value information.
  64. The "newmat" function allocates vectors to hold the index and nonzero
  65. value information, and returns pointers to these vectors along with
  66. matrix dimension and number of nonzeros.
  67. FUNCTION:
  68. int readHB_aux_double(const char* filename, const char AuxType, double b[])
  69. int readHB_newaux_double(const char* filename, const char AuxType, double** b)
  70. DESCRIPTION:
  71. This function opens and reads from the specified file auxillary vector(s).
  72. The char argument Auxtype determines which type of auxillary vector(s)
  73. will be read (if present in the file).
  74. AuxType = 'F' right-hand-side
  75. AuxType = 'G' initial estimate (Guess)
  76. AuxType = 'X' eXact solution
  77. If Nrhs > 1, all of the Nrhs vectors of the given type are read and
  78. stored in column-major order in the vector b.
  79. The "newaux" function allocates a vector to hold the values retrieved.
  80. The "mat" function uses a _pre-allocated_ vector to hold the values.
  81. FUNCTION:
  82. int writeHB_mat_double(const char* filename, int M, int N,
  83. int nz, const int colptr[], const int rowind[],
  84. const double val[], int Nrhs, const double rhs[],
  85. const double guess[], const double exact[],
  86. const char* Title, const char* Key, const char* Type,
  87. char* Ptrfmt, char* Indfmt, char* Valfmt, char* Rhsfmt,
  88. const char* Rhstype)
  89. DESCRIPTION:
  90. The writeHB_mat_double function opens the named file and writes the specified
  91. matrix and optional auxillary vector(s) to that file in Harwell-Boeing
  92. format. The format arguments (Ptrfmt,Indfmt,Valfmt, and Rhsfmt) are
  93. character strings specifying "Fortran-style" output formats -- as they
  94. would appear in a Harwell-Boeing file. They are used to produce output
  95. which is as close as possible to what would be produced by Fortran code,
  96. but note that "D" and "P" edit descriptors are not supported.
  97. If NULL, the following defaults will be used:
  98. Ptrfmt = Indfmt = "(8I10)"
  99. Valfmt = Rhsfmt = "(4E20.13)"
  100. -----------------------
  101. CHARACTER I/O FUNCTIONS
  102. -----------------------
  103. FUNCTION:
  104. int readHB_mat_char(const char* filename, int colptr[], int rowind[],
  105. char val[], char* Valfmt)
  106. int readHB_newmat_char(const char* filename, int* M, int* N, int* nonzeros,
  107. int** colptr, int** rowind, char** val, char** Valfmt)
  108. DESCRIPTION:
  109. This function opens and reads the specified file, interpreting its
  110. contents as a sparse matrix stored in the Harwell/Boeing standard
  111. format. (See readHB_aux_char to read auxillary vectors.)
  112. -- Values are interpreted as char strings. --
  113. (Used to translate exact values from the file into a new storage format.)
  114. The "mat" function uses _pre-allocated_ arrays to hold the index and
  115. nonzero value information.
  116. The "newmat" function allocates char arrays to hold the index
  117. and nonzero value information, and returns pointers to these arrays
  118. along with matrix dimension and number of nonzeros.
  119. FUNCTION:
  120. int readHB_aux_char(const char* filename, const char AuxType, char b[])
  121. int readHB_newaux_char(const char* filename, const char AuxType, char** b,
  122. char** Rhsfmt)
  123. DESCRIPTION:
  124. This function opens and reads from the specified file auxillary vector(s).
  125. The char argument Auxtype determines which type of auxillary vector(s)
  126. will be read (if present in the file).
  127. AuxType = 'F' right-hand-side
  128. AuxType = 'G' initial estimate (Guess)
  129. AuxType = 'X' eXact solution
  130. If Nrhs > 1, all of the Nrhs vectors of the given type are read and
  131. stored in column-major order in the vector b.
  132. The "newaux" function allocates a character array to hold the values
  133. retrieved.
  134. The "mat" function uses a _pre-allocated_ array to hold the values.
  135. FUNCTION:
  136. int writeHB_mat_char(const char* filename, int M, int N,
  137. int nz, const int colptr[], const int rowind[],
  138. const char val[], int Nrhs, const char rhs[],
  139. const char guess[], const char exact[],
  140. const char* Title, const char* Key, const char* Type,
  141. char* Ptrfmt, char* Indfmt, char* Valfmt, char* Rhsfmt,
  142. const char* Rhstype)
  143. DESCRIPTION:
  144. The writeHB_mat_char function opens the named file and writes the specified
  145. matrix and optional auxillary vector(s) to that file in Harwell-Boeing
  146. format. The format arguments (Ptrfmt,Indfmt,Valfmt, and Rhsfmt) are
  147. character strings specifying "Fortran-style" output formats -- as they
  148. would appear in a Harwell-Boeing file. Valfmt and Rhsfmt must accurately
  149. represent the character representation of the values stored in val[]
  150. and rhs[].
  151. If NULL, the following defaults will be used for the integer vectors:
  152. Ptrfmt = Indfmt = "(8I10)"
  153. Valfmt = Rhsfmt = "(4E20.13)"
  154. */
  155. /*---------------------------------------------------------------------*/
  156. /* If zero-based indexing is desired, _SP_base should be set to 0 */
  157. /* This will cause indices read from H-B files to be decremented by 1 */
  158. /* and indices written to H-B files to be incremented by 1 */
  159. /* <<< Standard usage is _SP_base = 1 >>> */
  160. #ifndef _SP_base
  161. #define _SP_base 1
  162. #endif
  163. /*---------------------------------------------------------------------*/
  164. #include "iohb.h"
  165. #include<stdio.h>
  166. #include<stdlib.h>
  167. #include<string.h>
  168. #include<math.h>
  169. char* substr(const char* S, const int pos, const int len);
  170. void upcase(char* S);
  171. void IOHBTerminate(const char* message);
  172. int readHB_info(const char* filename, int* M, int* N, int* nz, char** Type,
  173. int* Nrhs)
  174. {
  175. /****************************************************************************/
  176. /* The readHB_info function opens and reads the header information from */
  177. /* the specified Harwell-Boeing file, and reports back the number of rows */
  178. /* and columns in the stored matrix (M and N), the number of nonzeros in */
  179. /* the matrix (nz), and the number of right-hand-sides stored along with */
  180. /* the matrix (Nrhs). */
  181. /* */
  182. /* For a description of the Harwell Boeing standard, see: */
  183. /* Duff, et al., ACM TOMS Vol.15, No.1, March 1989 */
  184. /* */
  185. /* ---------- */
  186. /* **CAVEAT** */
  187. /* ---------- */
  188. /* ** If the input file does not adhere to the H/B format, the ** */
  189. /* ** results will be unpredictable. ** */
  190. /* */
  191. /****************************************************************************/
  192. FILE *in_file;
  193. int Ptrcrd, Indcrd, Valcrd, Rhscrd;
  194. int Nrow, Ncol, Nnzero;
  195. char* mat_type;
  196. char Title[73], Key[9], Rhstype[4];
  197. char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21];
  198. mat_type = *Type;
  199. if ( mat_type == NULL ) IOHBTerminate("Insufficient memory for mat_typen");
  200. if ( (in_file = fopen( filename, "r")) == NULL ) {
  201. fprintf(stderr,"Error: Cannot open file: %s\n",filename);
  202. return 0;
  203. }
  204. readHB_header(in_file, Title, Key, mat_type, &Nrow, &Ncol, &Nnzero, Nrhs,
  205. Ptrfmt, Indfmt, Valfmt, Rhsfmt,
  206. &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
  207. fclose(in_file);
  208. *Type = mat_type;
  209. *(*Type+3) = (char) NULL;
  210. *M = Nrow;
  211. *N = Ncol;
  212. *nz = Nnzero;
  213. if (Rhscrd == 0) {*Nrhs = 0;}
  214. /* In verbose mode, print some of the header information: */
  215. /*
  216. if (verbose == 1)
  217. {
  218. printf("Reading from Harwell-Boeing file %s (verbose on)...\n",filename);
  219. printf(" Title: %s\n",Title);
  220. printf(" Key: %s\n",Key);
  221. printf(" The stored matrix is %i by %i with %i nonzeros.\n",
  222. *M, *N, *nz );
  223. printf(" %i right-hand--side(s) stored.\n",*Nrhs);
  224. }
  225. */
  226. return 1;
  227. }
  228. int readHB_header(FILE* in_file, char* Title, char* Key, char* Type,
  229. int* Nrow, int* Ncol, int* Nnzero, int* Nrhs,
  230. char* Ptrfmt, char* Indfmt, char* Valfmt, char* Rhsfmt,
  231. int* Ptrcrd, int* Indcrd, int* Valcrd, int* Rhscrd,
  232. char *Rhstype)
  233. {
  234. /*************************************************************************/
  235. /* Read header information from the named H/B file... */
  236. /*************************************************************************/
  237. int Totcrd,Neltvl,Nrhsix;
  238. char line[BUFSIZ];
  239. /* First line: */
  240. fgets(line, BUFSIZ, in_file);
  241. if ( sscanf(line,"%*s") < 0 )
  242. IOHBTerminate("iohb.c: Null (or blank) first line of HB file.\n");
  243. (void) sscanf(line, "%72c%8[^\n]", Title, Key);
  244. *(Key+8) = (char) NULL;
  245. *(Title+72) = (char) NULL;
  246. /* Second line: */
  247. fgets(line, BUFSIZ, in_file);
  248. if ( sscanf(line,"%*s") < 0 )
  249. IOHBTerminate("iohb.c: Null (or blank) second line of HB file.\n");
  250. if ( sscanf(line,"%i",&Totcrd) != 1) Totcrd = 0;
  251. if ( sscanf(line,"%*i%i",Ptrcrd) != 1) *Ptrcrd = 0;
  252. if ( sscanf(line,"%*i%*i%i",Indcrd) != 1) *Indcrd = 0;
  253. if ( sscanf(line,"%*i%*i%*i%i",Valcrd) != 1) *Valcrd = 0;
  254. if ( sscanf(line,"%*i%*i%*i%*i%i",Rhscrd) != 1) *Rhscrd = 0;
  255. /* Third line: */
  256. fgets(line, BUFSIZ, in_file);
  257. if ( sscanf(line,"%*s") < 0 )
  258. IOHBTerminate("iohb.c: Null (or blank) third line of HB file.\n");
  259. if ( sscanf(line, "%3c", Type) != 1)
  260. IOHBTerminate("iohb.c: Invalid Type info, line 3 of Harwell-Boeing file.\n");
  261. upcase(Type);
  262. if ( sscanf(line,"%*3c%i",Nrow) != 1) *Nrow = 0 ;
  263. if ( sscanf(line,"%*3c%*i%i",Ncol) != 1) *Ncol = 0 ;
  264. if ( sscanf(line,"%*3c%*i%*i%i",Nnzero) != 1) *Nnzero = 0 ;
  265. if ( sscanf(line,"%*3c%*i%*i%*i%i",&Neltvl) != 1) Neltvl = 0 ;
  266. /* Fourth line: */
  267. fgets(line, BUFSIZ, in_file);
  268. if ( sscanf(line,"%*s") < 0 )
  269. IOHBTerminate("iohb.c: Null (or blank) fourth line of HB file.\n");
  270. if ( sscanf(line, "%16c",Ptrfmt) != 1)
  271. IOHBTerminate("iohb.c: Invalid format info, line 4 of Harwell-Boeing file.\n");
  272. if ( sscanf(line, "%*16c%16c",Indfmt) != 1)
  273. IOHBTerminate("iohb.c: Invalid format info, line 4 of Harwell-Boeing file.\n");
  274. if ( sscanf(line, "%*16c%*16c%20c",Valfmt) != 1)
  275. IOHBTerminate("iohb.c: Invalid format info, line 4 of Harwell-Boeing file.\n");
  276. sscanf(line, "%*16c%*16c%*20c%20c",Rhsfmt);
  277. *(Ptrfmt+16) = (char) NULL;
  278. *(Indfmt+16) = (char) NULL;
  279. *(Valfmt+20) = (char) NULL;
  280. *(Rhsfmt+20) = (char) NULL;
  281. /* (Optional) Fifth line: */
  282. if (*Rhscrd != 0 )
  283. {
  284. fgets(line, BUFSIZ, in_file);
  285. if ( sscanf(line,"%*s") < 0 )
  286. IOHBTerminate("iohb.c: Null (or blank) fifth line of HB file.\n");
  287. if ( sscanf(line, "%3c", Rhstype) != 1)
  288. IOHBTerminate("iohb.c: Invalid RHS type information, line 5 of Harwell-Boeing file.\n");
  289. if ( sscanf(line, "%*3c%i", Nrhs) != 1) *Nrhs = 0;
  290. if ( sscanf(line, "%*3c%*i%i", &Nrhsix) != 1) Nrhsix = 0;
  291. }
  292. return 1;
  293. }
  294. int readHB_mat_double(const char* filename, int colptr[], int rowind[],
  295. double val[])
  296. {
  297. /****************************************************************************/
  298. /* This function opens and reads the specified file, interpreting its */
  299. /* contents as a sparse matrix stored in the Harwell/Boeing standard */
  300. /* format and creating compressed column storage scheme vectors to hold */
  301. /* the index and nonzero value information. */
  302. /* */
  303. /* ---------- */
  304. /* **CAVEAT** */
  305. /* ---------- */
  306. /* Parsing real formats from Fortran is tricky, and this file reader */
  307. /* does not claim to be foolproof. It has been tested for cases when */
  308. /* the real values are printed consistently and evenly spaced on each */
  309. /* line, with Fixed (F), and Exponential (E or D) formats. */
  310. /* */
  311. /* ** If the input file does not adhere to the H/B format, the ** */
  312. /* ** results will be unpredictable. ** */
  313. /* */
  314. /****************************************************************************/
  315. FILE *in_file;
  316. int i,j,ind,col,offset,count,last,Nrhs;
  317. int Ptrcrd, Indcrd, Valcrd, Rhscrd;
  318. int Nrow, Ncol, Nnzero, Nentries;
  319. int Ptrperline, Ptrwidth, Indperline, Indwidth;
  320. int Valperline, Valwidth, Valprec;
  321. int Valflag; /* Indicates 'E','D', or 'F' float format */
  322. char* ThisElement;
  323. char Title[73], Key[8], Type[4], Rhstype[4];
  324. char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21];
  325. char line[BUFSIZ];
  326. if ( (in_file = fopen( filename, "r")) == NULL ) {
  327. fprintf(stderr,"Error: Cannot open file: %s\n",filename);
  328. return 0;
  329. }
  330. readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs,
  331. Ptrfmt, Indfmt, Valfmt, Rhsfmt,
  332. &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
  333. /* Parse the array input formats from Line 3 of HB file */
  334. ParseIfmt(Ptrfmt,&Ptrperline,&Ptrwidth);
  335. ParseIfmt(Indfmt,&Indperline,&Indwidth);
  336. if ( Type[0] != 'P' ) { /* Skip if pattern only */
  337. ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);
  338. }
  339. /* Read column pointer array: */
  340. offset = 1-_SP_base; /* if base 0 storage is declared (via macro definition), */
  341. /* then storage entries are offset by 1 */
  342. ThisElement = (char *) malloc(Ptrwidth+1);
  343. if ( ThisElement == NULL ) IOHBTerminate("Insufficient memory for ThisElement.");
  344. *(ThisElement+Ptrwidth) = (char) NULL;
  345. count=0;
  346. for (i=0;i<Ptrcrd;i++)
  347. {
  348. fgets(line, BUFSIZ, in_file);
  349. if ( sscanf(line,"%*s") < 0 )
  350. IOHBTerminate("iohb.c: Null (or blank) line in pointer data region of HB file.\n");
  351. col = 0;
  352. for (ind = 0;ind<Ptrperline;ind++)
  353. {
  354. if (count > Ncol) break;
  355. strncpy(ThisElement,line+col,Ptrwidth);
  356. /* ThisElement = substr(line,col,Ptrwidth); */
  357. colptr[count] = atoi(ThisElement)-offset;
  358. count++; col += Ptrwidth;
  359. }
  360. }
  361. free(ThisElement);
  362. /* Read row index array: */
  363. ThisElement = (char *) malloc(Indwidth+1);
  364. if ( ThisElement == NULL ) IOHBTerminate("Insufficient memory for ThisElement.");
  365. *(ThisElement+Indwidth) = (char) NULL;
  366. count = 0;
  367. for (i=0;i<Indcrd;i++)
  368. {
  369. fgets(line, BUFSIZ, in_file);
  370. if ( sscanf(line,"%*s") < 0 )
  371. IOHBTerminate("iohb.c: Null (or blank) line in index data region of HB file.\n");
  372. col = 0;
  373. for (ind = 0;ind<Indperline;ind++)
  374. {
  375. if (count == Nnzero) break;
  376. strncpy(ThisElement,line+col,Indwidth);
  377. /* ThisElement = substr(line,col,Indwidth); */
  378. rowind[count] = atoi(ThisElement)-offset;
  379. count++; col += Indwidth;
  380. }
  381. }
  382. free(ThisElement);
  383. /* Read array of values: */
  384. if ( Type[0] != 'P' ) { /* Skip if pattern only */
  385. if ( Type[0] == 'C' ) Nentries = 2*Nnzero;
  386. else Nentries = Nnzero;
  387. ThisElement = (char *) malloc(Valwidth+1);
  388. if ( ThisElement == NULL ) IOHBTerminate("Insufficient memory for ThisElement.");
  389. *(ThisElement+Valwidth) = (char) NULL;
  390. count = 0;
  391. for (i=0;i<Valcrd;i++)
  392. {
  393. fgets(line, BUFSIZ, in_file);
  394. if ( sscanf(line,"%*s") < 0 )
  395. IOHBTerminate("iohb.c: Null (or blank) line in value data region of HB file.\n");
  396. if (Valflag == 'D') {
  397. while( strchr(line,'D') ) *strchr(line,'D') = 'E';
  398. /* *strchr(Valfmt,'D') = 'E'; */
  399. }
  400. col = 0;
  401. for (ind = 0;ind<Valperline;ind++)
  402. {
  403. if (count == Nentries) break;
  404. strncpy(ThisElement,line+col,Valwidth);
  405. /*ThisElement = substr(line,col,Valwidth);*/
  406. if ( Valflag != 'F' && strchr(ThisElement,'E') == NULL ) {
  407. /* insert a char prefix for exp */
  408. last = strlen(ThisElement);
  409. for (j=last+1;j>=0;j--) {
  410. ThisElement[j] = ThisElement[j-1];
  411. if ( ThisElement[j] == '+' || ThisElement[j] == '-' ) {
  412. ThisElement[j-1] = Valflag;
  413. break;
  414. }
  415. }
  416. }
  417. val[count] = atof(ThisElement);
  418. count++; col += Valwidth;
  419. }
  420. }
  421. free(ThisElement);
  422. }
  423. fclose(in_file);
  424. return 1;
  425. }
  426. int readHB_newmat_double(const char* filename, int* M, int* N, int* nonzeros,
  427. int** colptr, int** rowind, double** val)
  428. {
  429. int Nrhs;
  430. char *Type;
  431. readHB_info(filename, M, N, nonzeros, &Type, &Nrhs);
  432. *colptr = (int *)malloc((*N+1)*sizeof(int));
  433. if ( *colptr == NULL ) IOHBTerminate("Insufficient memory for colptr.\n");
  434. *rowind = (int *)malloc(*nonzeros*sizeof(int));
  435. if ( *rowind == NULL ) IOHBTerminate("Insufficient memory for rowind.\n");
  436. if ( Type[0] == 'C' ) {
  437. /*
  438. fprintf(stderr, "Warning: Reading complex data from HB file %s.\n",filename);
  439. fprintf(stderr, " Real and imaginary parts will be interlaced in val[].\n");
  440. */
  441. /* Malloc enough space for real AND imaginary parts of val[] */
  442. *val = (double *)malloc(*nonzeros*sizeof(double)*2);
  443. if ( *val == NULL ) IOHBTerminate("Insufficient memory for val.\n");
  444. } else {
  445. if ( Type[0] != 'P' ) {
  446. /* Malloc enough space for real array val[] */
  447. *val = (double *)malloc(*nonzeros*sizeof(double));
  448. if ( *val == NULL ) IOHBTerminate("Insufficient memory for val.\n");
  449. }
  450. } /* No val[] space needed if pattern only */
  451. return readHB_mat_double(filename, *colptr, *rowind, *val);
  452. }
  453. int readHB_aux_double(const char* filename, const char AuxType, double b[])
  454. {
  455. /****************************************************************************/
  456. /* This function opens and reads the specified file, placing auxillary */
  457. /* vector(s) of the given type (if available) in b. */
  458. /* Return value is the number of vectors successfully read. */
  459. /* */
  460. /* AuxType = 'F' full right-hand-side vector(s) */
  461. /* AuxType = 'G' initial Guess vector(s) */
  462. /* AuxType = 'X' eXact solution vector(s) */
  463. /* */
  464. /* ---------- */
  465. /* **CAVEAT** */
  466. /* ---------- */
  467. /* Parsing real formats from Fortran is tricky, and this file reader */
  468. /* does not claim to be foolproof. It has been tested for cases when */
  469. /* the real values are printed consistently and evenly spaced on each */
  470. /* line, with Fixed (F), and Exponential (E or D) formats. */
  471. /* */
  472. /* ** If the input file does not adhere to the H/B format, the ** */
  473. /* ** results will be unpredictable. ** */
  474. /* */
  475. /****************************************************************************/
  476. FILE *in_file;
  477. int i,j,n,maxcol,start,stride,col,last,linel;
  478. int Ptrcrd, Indcrd, Valcrd, Rhscrd;
  479. int Nrow, Ncol, Nnzero, Nentries;
  480. int Nrhs, nvecs, rhsi;
  481. int Rhsperline, Rhswidth, Rhsprec;
  482. int Rhsflag;
  483. char *ThisElement;
  484. char Title[73], Key[9], Type[4], Rhstype[4];
  485. char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21];
  486. char line[BUFSIZ];
  487. if ((in_file = fopen( filename, "r")) == NULL) {
  488. fprintf(stderr,"Error: Cannot open file: %s\n",filename);
  489. return 0;
  490. }
  491. readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs,
  492. Ptrfmt, Indfmt, Valfmt, Rhsfmt,
  493. &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
  494. if (Nrhs <= 0)
  495. {
  496. fprintf(stderr, "Warn: Attempt to read auxillary vector(s) when none are present.\n");
  497. return 0;
  498. }
  499. if (Rhstype[0] != 'F' )
  500. {
  501. fprintf(stderr,"Warn: Attempt to read auxillary vector(s) which are not stored in Full form.\n");
  502. fprintf(stderr," Rhs must be specified as full. \n");
  503. return 0;
  504. }
  505. /* If reading complex data, allow for interleaved real and imaginary values. */
  506. if ( Type[0] == 'C' ) {
  507. Nentries = 2*Nrow;
  508. } else {
  509. Nentries = Nrow;
  510. }
  511. nvecs = 1;
  512. if ( Rhstype[1] == 'G' ) nvecs++;
  513. if ( Rhstype[2] == 'X' ) nvecs++;
  514. if ( AuxType == 'G' && Rhstype[1] != 'G' ) {
  515. fprintf(stderr, "Warn: Attempt to read auxillary Guess vector(s) when none are present.\n");
  516. return 0;
  517. }
  518. if ( AuxType == 'X' && Rhstype[2] != 'X' ) {
  519. fprintf(stderr, "Warn: Attempt to read auxillary eXact solution vector(s) when none are present.\n");
  520. return 0;
  521. }
  522. ParseRfmt(Rhsfmt, &Rhsperline, &Rhswidth, &Rhsprec,&Rhsflag);
  523. maxcol = Rhsperline*Rhswidth;
  524. /* Lines to skip before starting to read RHS values... */
  525. n = Ptrcrd + Indcrd + Valcrd;
  526. for (i = 0; i < n; i++)
  527. fgets(line, BUFSIZ, in_file);
  528. /* start - number of initial aux vector entries to skip */
  529. /* to reach first vector requested */
  530. /* stride - number of aux vector entries to skip between */
  531. /* requested vectors */
  532. if ( AuxType == 'F' ) start = 0;
  533. else if ( AuxType == 'G' ) start = Nentries;
  534. else start = (nvecs-1)*Nentries;
  535. stride = (nvecs-1)*Nentries;
  536. fgets(line, BUFSIZ, in_file);
  537. linel= strchr(line,'\n')-line;
  538. col = 0;
  539. /* Skip to initial offset */
  540. for (i=0;i<start;i++) {
  541. if ( col >= ( maxcol<linel?maxcol:linel ) ) {
  542. fgets(line, BUFSIZ, in_file);
  543. linel= strchr(line,'\n')-line;
  544. col = 0;
  545. }
  546. col += Rhswidth;
  547. }
  548. if (Rhsflag == 'D') {
  549. while( strchr(line,'D') ) *strchr(line,'D') = 'E';
  550. }
  551. /* Read a vector of desired type, then skip to next */
  552. /* repeating to fill Nrhs vectors */
  553. ThisElement = (char *) malloc(Rhswidth+1);
  554. if ( ThisElement == NULL ) IOHBTerminate("Insufficient memory for ThisElement.");
  555. *(ThisElement+Rhswidth) = (char) NULL;
  556. for (rhsi=0;rhsi<Nrhs;rhsi++) {
  557. for (i=0;i<Nentries;i++) {
  558. if ( col >= ( maxcol<linel?maxcol:linel ) ) {
  559. fgets(line, BUFSIZ, in_file);
  560. linel= strchr(line,'\n')-line;
  561. if (Rhsflag == 'D') {
  562. while( strchr(line,'D') ) *strchr(line,'D') = 'E';
  563. }
  564. col = 0;
  565. }
  566. strncpy(ThisElement,line+col,Rhswidth);
  567. /*ThisElement = substr(line, col, Rhswidth);*/
  568. if ( Rhsflag != 'F' && strchr(ThisElement,'E') == NULL ) {
  569. /* insert a char prefix for exp */
  570. last = strlen(ThisElement);
  571. for (j=last+1;j>=0;j--) {
  572. ThisElement[j] = ThisElement[j-1];
  573. if ( ThisElement[j] == '+' || ThisElement[j] == '-' ) {
  574. ThisElement[j-1] = Rhsflag;
  575. break;
  576. }
  577. }
  578. }
  579. b[i] = atof(ThisElement);
  580. col += Rhswidth;
  581. }
  582. /* Skip any interleaved Guess/eXact vectors */
  583. for (i=0;i<stride;i++) {
  584. if ( col >= ( maxcol<linel?maxcol:linel ) ) {
  585. fgets(line, BUFSIZ, in_file);
  586. linel= strchr(line,'\n')-line;
  587. col = 0;
  588. }
  589. col += Rhswidth;
  590. }
  591. }
  592. free(ThisElement);
  593. fclose(in_file);
  594. return Nrhs;
  595. }
  596. int readHB_newaux_double(const char* filename, const char AuxType, double** b)
  597. {
  598. int Nrhs,M,N,nonzeros;
  599. char *Type;
  600. readHB_info(filename, &M, &N, &nonzeros, &Type, &Nrhs);
  601. if ( Nrhs <= 0 ) {
  602. fprintf(stderr,"Warn: Requested read of aux vector(s) when none are present.\n");
  603. return 0;
  604. } else {
  605. if ( Type[0] == 'C' ) {
  606. fprintf(stderr, "Warning: Reading complex aux vector(s) from HB file %s.",filename);
  607. fprintf(stderr, " Real and imaginary parts will be interlaced in b[].");
  608. *b = (double *)malloc(M*Nrhs*sizeof(double)*2);
  609. if ( *b == NULL ) IOHBTerminate("Insufficient memory for rhs.\n");
  610. return readHB_aux_double(filename, AuxType, *b);
  611. } else {
  612. *b = (double *)malloc(M*Nrhs*sizeof(double));
  613. if ( *b == NULL ) IOHBTerminate("Insufficient memory for rhs.\n");
  614. return readHB_aux_double(filename, AuxType, *b);
  615. }
  616. }
  617. }
  618. int writeHB_mat_double(const char* filename, int M, int N,
  619. int nz, const int colptr[], const int rowind[],
  620. const double val[], int Nrhs, const double rhs[],
  621. const double guess[], const double exact[],
  622. const char* Title, const char* Key, const char* Type,
  623. char* Ptrfmt, char* Indfmt, char* Valfmt, char* Rhsfmt,
  624. const char* Rhstype)
  625. {
  626. /****************************************************************************/
  627. /* The writeHB function opens the named file and writes the specified */
  628. /* matrix and optional right-hand-side(s) to that file in Harwell-Boeing */
  629. /* format. */
  630. /* */
  631. /* For a description of the Harwell Boeing standard, see: */
  632. /* Duff, et al., ACM TOMS Vol.15, No.1, March 1989 */
  633. /* */
  634. /****************************************************************************/
  635. FILE *out_file;
  636. int i,j,entry,offset,acount,linemod;
  637. int totcrd, ptrcrd, indcrd, valcrd, rhscrd;
  638. int nvalentries, nrhsentries;
  639. int Ptrperline, Ptrwidth, Indperline, Indwidth;
  640. int Rhsperline, Rhswidth, Rhsprec;
  641. int Rhsflag;
  642. int Valperline, Valwidth, Valprec;
  643. int Valflag; /* Indicates 'E','D', or 'F' float format */
  644. char pformat[16],iformat[16],vformat[19],rformat[19];
  645. if ( Type[0] == 'C' ) {
  646. nvalentries = 2*nz;
  647. nrhsentries = 2*M;
  648. } else {
  649. nvalentries = nz;
  650. nrhsentries = M;
  651. }
  652. if ( filename != NULL ) {
  653. if ( (out_file = fopen( filename, "w")) == NULL ) {
  654. fprintf(stderr,"Error: Cannot open file: %s\n",filename);
  655. return 0;
  656. }
  657. } else out_file = stdout;
  658. if ( Ptrfmt == NULL ) Ptrfmt = "(8I10)";
  659. ParseIfmt(Ptrfmt,&Ptrperline,&Ptrwidth);
  660. sprintf(pformat,"%%%dd",Ptrwidth);
  661. ptrcrd = (N+1)/Ptrperline;
  662. if ( (N+1)%Ptrperline != 0) ptrcrd++;
  663. if ( Indfmt == NULL ) Indfmt = Ptrfmt;
  664. ParseIfmt(Indfmt,&Indperline,&Indwidth);
  665. sprintf(iformat,"%%%dd",Indwidth);
  666. indcrd = nz/Indperline;
  667. if ( nz%Indperline != 0) indcrd++;
  668. if ( Type[0] != 'P' ) { /* Skip if pattern only */
  669. if ( Valfmt == NULL ) Valfmt = "(4E20.13)";
  670. ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);
  671. if (Valflag == 'D') *strchr(Valfmt,'D') = 'E';
  672. if (Valflag == 'F')
  673. sprintf(vformat,"%% %d.%df",Valwidth,Valprec);
  674. else
  675. sprintf(vformat,"%% %d.%dE",Valwidth,Valprec);
  676. valcrd = nvalentries/Valperline;
  677. if ( nvalentries%Valperline != 0) valcrd++;
  678. } else valcrd = 0;
  679. if ( Nrhs > 0 ) {
  680. if ( Rhsfmt == NULL ) Rhsfmt = Valfmt;
  681. ParseRfmt(Rhsfmt,&Rhsperline,&Rhswidth,&Rhsprec, &Rhsflag);
  682. if (Rhsflag == 'F')
  683. sprintf(rformat,"%% %d.%df",Rhswidth,Rhsprec);
  684. else
  685. sprintf(rformat,"%% %d.%dE",Rhswidth,Rhsprec);
  686. if (Rhsflag == 'D') *strchr(Rhsfmt,'D') = 'E';
  687. rhscrd = nrhsentries/Rhsperline;
  688. if ( nrhsentries%Rhsperline != 0) rhscrd++;
  689. if ( Rhstype[1] == 'G' ) rhscrd+=rhscrd;
  690. if ( Rhstype[2] == 'X' ) rhscrd+=rhscrd;
  691. rhscrd*=Nrhs;
  692. } else rhscrd = 0;
  693. totcrd = 4+ptrcrd+indcrd+valcrd+rhscrd;
  694. /* Print header information: */
  695. fprintf(out_file,"%-72s%-8s\n%14d%14d%14d%14d%14d\n",Title, Key, totcrd,
  696. ptrcrd, indcrd, valcrd, rhscrd);
  697. fprintf(out_file,"%3s%11s%14d%14d%14d\n",Type," ", M, N, nz);
  698. fprintf(out_file,"%-16s%-16s%-20s", Ptrfmt, Indfmt, Valfmt);
  699. if ( Nrhs != 0 ) {
  700. /* Print Rhsfmt on fourth line and */
  701. /* optional fifth header line for auxillary vector information: */
  702. fprintf(out_file,"%-20s\n%-14s%d\n",Rhsfmt,Rhstype,Nrhs);
  703. } else fprintf(out_file,"\n");
  704. offset = 1-_SP_base; /* if base 0 storage is declared (via macro definition), */
  705. /* then storage entries are offset by 1 */
  706. /* Print column pointers: */
  707. for (i=0;i<N+1;i++)
  708. {
  709. entry = colptr[i]+offset;
  710. fprintf(out_file,pformat,entry);
  711. if ( (i+1)%Ptrperline == 0 ) fprintf(out_file,"\n");
  712. }
  713. if ( (N+1) % Ptrperline != 0 ) fprintf(out_file,"\n");
  714. /* Print row indices: */
  715. for (i=0;i<nz;i++)
  716. {
  717. entry = rowind[i]+offset;
  718. fprintf(out_file,iformat,entry);
  719. if ( (i+1)%Indperline == 0 ) fprintf(out_file,"\n");
  720. }
  721. if ( nz % Indperline != 0 ) fprintf(out_file,"\n");
  722. /* Print values: */
  723. if ( Type[0] != 'P' ) { /* Skip if pattern only */
  724. for (i=0;i<nvalentries;i++)
  725. {
  726. fprintf(out_file,vformat,val[i]);
  727. if ( (i+1)%Valperline == 0 ) fprintf(out_file,"\n");
  728. }
  729. if ( nvalentries % Valperline != 0 ) fprintf(out_file,"\n");
  730. /* If available, print right hand sides,
  731. guess vectors and exact solution vectors: */
  732. acount = 1;
  733. linemod = 0;
  734. if ( Nrhs > 0 ) {
  735. for (i=0;i<Nrhs;i++)
  736. {
  737. for ( j=0;j<nrhsentries;j++ ) {
  738. fprintf(out_file,rformat,rhs[j]);
  739. if ( acount++%Rhsperline == linemod ) fprintf(out_file,"\n");
  740. }
  741. if ( acount%Rhsperline != linemod ) {
  742. fprintf(out_file,"\n");
  743. linemod = (acount-1)%Rhsperline;
  744. }
  745. rhs += nrhsentries;
  746. if ( Rhstype[1] == 'G' ) {
  747. for ( j=0;j<nrhsentries;j++ ) {
  748. fprintf(out_file,rformat,guess[j]);
  749. if ( acount++%Rhsperline == linemod ) fprintf(out_file,"\n");
  750. }
  751. if ( acount%Rhsperline != linemod ) {
  752. fprintf(out_file,"\n");
  753. linemod = (acount-1)%Rhsperline;
  754. }
  755. guess += nrhsentries;
  756. }
  757. if ( Rhstype[2] == 'X' ) {
  758. for ( j=0;j<nrhsentries;j++ ) {
  759. fprintf(out_file,rformat,exact[j]);
  760. if ( acount++%Rhsperline == linemod ) fprintf(out_file,"\n");
  761. }
  762. if ( acount%Rhsperline != linemod ) {
  763. fprintf(out_file,"\n");
  764. linemod = (acount-1)%Rhsperline;
  765. }
  766. exact += nrhsentries;
  767. }
  768. }
  769. }
  770. }
  771. if ( fclose(out_file) != 0){
  772. fprintf(stderr,"Error closing file in writeHB_mat_double().\n");
  773. return 0;
  774. } else return 1;
  775. }
  776. int readHB_mat_char(const char* filename, int colptr[], int rowind[],
  777. char val[], char* Valfmt)
  778. {
  779. /****************************************************************************/
  780. /* This function opens and reads the specified file, interpreting its */
  781. /* contents as a sparse matrix stored in the Harwell/Boeing standard */
  782. /* format and creating compressed column storage scheme vectors to hold */
  783. /* the index and nonzero value information. */
  784. /* */
  785. /* ---------- */
  786. /* **CAVEAT** */
  787. /* ---------- */
  788. /* Parsing real formats from Fortran is tricky, and this file reader */
  789. /* does not claim to be foolproof. It has been tested for cases when */
  790. /* the real values are printed consistently and evenly spaced on each */
  791. /* line, with Fixed (F), and Exponential (E or D) formats. */
  792. /* */
  793. /* ** If the input file does not adhere to the H/B format, the ** */
  794. /* ** results will be unpredictable. ** */
  795. /* */
  796. /****************************************************************************/
  797. FILE *in_file;
  798. int i,j,ind,col,offset,count,last;
  799. int Nrow,Ncol,Nnzero,Nentries,Nrhs;
  800. int Ptrcrd, Indcrd, Valcrd, Rhscrd;
  801. int Ptrperline, Ptrwidth, Indperline, Indwidth;
  802. int Valperline, Valwidth, Valprec;
  803. int Valflag; /* Indicates 'E','D', or 'F' float format */
  804. char* ThisElement;
  805. char line[BUFSIZ];
  806. char Title[73], Key[8], Type[4], Rhstype[4];
  807. char Ptrfmt[17], Indfmt[17], Rhsfmt[21];
  808. if ( (in_file = fopen( filename, "r")) == NULL ) {
  809. fprintf(stderr,"Error: Cannot open file: %s\n",filename);
  810. return 0;
  811. }
  812. readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs,
  813. Ptrfmt, Indfmt, Valfmt, Rhsfmt,
  814. &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
  815. /* Parse the array input formats from Line 3 of HB file */
  816. ParseIfmt(Ptrfmt,&Ptrperline,&Ptrwidth);
  817. ParseIfmt(Indfmt,&Indperline,&Indwidth);
  818. if ( Type[0] != 'P' ) { /* Skip if pattern only */
  819. ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);
  820. if (Valflag == 'D') {
  821. *strchr(Valfmt,'D') = 'E';
  822. }
  823. }
  824. /* Read column pointer array: */
  825. offset = 1-_SP_base; /* if base 0 storage is declared (via macro definition), */
  826. /* then storage entries are offset by 1 */
  827. ThisElement = (char *) malloc(Ptrwidth+1);
  828. if ( ThisElement == NULL ) IOHBTerminate("Insufficient memory for ThisElement.");
  829. *(ThisElement+Ptrwidth) = (char) NULL;
  830. count=0;
  831. for (i=0;i<Ptrcrd;i++)
  832. {
  833. fgets(line, BUFSIZ, in_file);
  834. if ( sscanf(line,"%*s") < 0 )
  835. IOHBTerminate("iohb.c: Null (or blank) line in pointer data region of HB file.\n");
  836. col = 0;
  837. for (ind = 0;ind<Ptrperline;ind++)
  838. {
  839. if (count > Ncol) break;
  840. strncpy(ThisElement,line+col,Ptrwidth);
  841. /*ThisElement = substr(line,col,Ptrwidth);*/
  842. colptr[count] = atoi(ThisElement)-offset;
  843. count++; col += Ptrwidth;
  844. }
  845. }
  846. free(ThisElement);
  847. /* Read row index array: */
  848. ThisElement = (char *) malloc(Indwidth+1);
  849. if ( ThisElement == NULL ) IOHBTerminate("Insufficient memory for ThisElement.");
  850. *(ThisElement+Indwidth) = (char) NULL;
  851. count = 0;
  852. for (i=0;i<Indcrd;i++)
  853. {
  854. fgets(line, BUFSIZ, in_file);
  855. if ( sscanf(line,"%*s") < 0 )
  856. IOHBTerminate("iohb.c: Null (or blank) line in index data region of HB file.\n");
  857. col = 0;
  858. for (ind = 0;ind<Indperline;ind++)
  859. {
  860. if (count == Nnzero) break;
  861. strncpy(ThisElement,line+col,Indwidth);
  862. /*ThisElement = substr(line,col,Indwidth);*/
  863. rowind[count] = atoi(ThisElement)-offset;
  864. count++; col += Indwidth;
  865. }
  866. }
  867. free(ThisElement);
  868. /* Read array of values: AS CHARACTERS*/
  869. if ( Type[0] != 'P' ) { /* Skip if pattern only */
  870. if ( Type[0] == 'C' ) Nentries = 2*Nnzero;
  871. else Nentries = Nnzero;
  872. ThisElement = (char *) malloc(Valwidth+1);
  873. if ( ThisElement == NULL ) IOHBTerminate("Insufficient memory for ThisElement.");
  874. *(ThisElement+Valwidth) = (char) NULL;
  875. count = 0;
  876. for (i=0;i<Valcrd;i++)
  877. {
  878. fgets(line, BUFSIZ, in_file);
  879. if ( sscanf(line,"%*s") < 0 )
  880. IOHBTerminate("iohb.c: Null (or blank) line in value data region of HB file.\n");
  881. if (Valflag == 'D') {
  882. while( strchr(line,'D') ) *strchr(line,'D') = 'E';
  883. }
  884. col = 0;
  885. for (ind = 0;ind<Valperline;ind++)
  886. {
  887. if (count == Nentries) break;
  888. ThisElement = &val[count*Valwidth];
  889. strncpy(ThisElement,line+col,Valwidth);
  890. /*strncpy(ThisElement,substr(line,col,Valwidth),Valwidth);*/
  891. if ( Valflag != 'F' && strchr(ThisElement,'E') == NULL ) {
  892. /* insert a char prefix for exp */
  893. last = strlen(ThisElement);
  894. for (j=last+1;j>=0;j--) {
  895. ThisElement[j] = ThisElement[j-1];
  896. if ( ThisElement[j] == '+' || ThisElement[j] == '-' ) {
  897. ThisElement[j-1] = Valflag;
  898. break;
  899. }
  900. }
  901. }
  902. count++; col += Valwidth;
  903. }
  904. }
  905. }
  906. return 1;
  907. }
  908. int readHB_newmat_char(const char* filename, int* M, int* N, int* nonzeros, int** colptr,
  909. int** rowind, char** val, char** Valfmt)
  910. {
  911. FILE *in_file;
  912. int Nrhs;
  913. int Ptrcrd, Indcrd, Valcrd, Rhscrd;
  914. int Valperline, Valwidth, Valprec;
  915. int Valflag; /* Indicates 'E','D', or 'F' float format */
  916. char Title[73], Key[9], Type[4], Rhstype[4];
  917. char Ptrfmt[17], Indfmt[17], Rhsfmt[21];
  918. if ((in_file = fopen( filename, "r")) == NULL) {
  919. fprintf(stderr,"Error: Cannot open file: %s\n",filename);
  920. return 0;
  921. }
  922. *Valfmt = (char *)malloc(21*sizeof(char));
  923. if ( *Valfmt == NULL ) IOHBTerminate("Insufficient memory for Valfmt.");
  924. readHB_header(in_file, Title, Key, Type, M, N, nonzeros, &Nrhs,
  925. Ptrfmt, Indfmt, (*Valfmt), Rhsfmt,
  926. &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
  927. fclose(in_file);
  928. ParseRfmt(*Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);
  929. *colptr = (int *)malloc((*N+1)*sizeof(int));
  930. if ( *colptr == NULL ) IOHBTerminate("Insufficient memory for colptr.\n");
  931. *rowind = (int *)malloc(*nonzeros*sizeof(int));
  932. if ( *rowind == NULL ) IOHBTerminate("Insufficient memory for rowind.\n");
  933. if ( Type[0] == 'C' ) {
  934. /*
  935. fprintf(stderr, "Warning: Reading complex data from HB file %s.\n",filename);
  936. fprintf(stderr, " Real and imaginary parts will be interlaced in val[].\n");
  937. */
  938. /* Malloc enough space for real AND imaginary parts of val[] */
  939. *val = (char *)malloc(*nonzeros*Valwidth*sizeof(char)*2);
  940. if ( *val == NULL ) IOHBTerminate("Insufficient memory for val.\n");
  941. } else {
  942. if ( Type[0] != 'P' ) {
  943. /* Malloc enough space for real array val[] */
  944. *val = (char *)malloc(*nonzeros*Valwidth*sizeof(char));
  945. if ( *val == NULL ) IOHBTerminate("Insufficient memory for val.\n");
  946. }
  947. } /* No val[] space needed if pattern only */
  948. return readHB_mat_char(filename, *colptr, *rowind, *val, *Valfmt);
  949. }
  950. int readHB_aux_char(const char* filename, const char AuxType, char b[])
  951. {
  952. /****************************************************************************/
  953. /* This function opens and reads the specified file, placing auxilary */
  954. /* vector(s) of the given type (if available) in b : */
  955. /* Return value is the number of vectors successfully read. */
  956. /* */
  957. /* AuxType = 'F' full right-hand-side vector(s) */
  958. /* AuxType = 'G' initial Guess vector(s) */
  959. /* AuxType = 'X' eXact solution vector(s) */
  960. /* */
  961. /* ---------- */
  962. /* **CAVEAT** */
  963. /* ---------- */
  964. /* Parsing real formats from Fortran is tricky, and this file reader */
  965. /* does not claim to be foolproof. It has been tested for cases when */
  966. /* the real values are printed consistently and evenly spaced on each */
  967. /* line, with Fixed (F), and Exponential (E or D) formats. */
  968. /* */
  969. /* ** If the input file does not adhere to the H/B format, the ** */
  970. /* ** results will be unpredictable. ** */
  971. /* */
  972. /****************************************************************************/
  973. FILE *in_file;
  974. int i,j,n,maxcol,start,stride,col,last,linel,nvecs,rhsi;
  975. int Nrow, Ncol, Nnzero, Nentries,Nrhs;
  976. int Ptrcrd, Indcrd, Valcrd, Rhscrd;
  977. int Rhsperline, Rhswidth, Rhsprec;
  978. int Rhsflag;
  979. char Title[73], Key[9], Type[4], Rhstype[4];
  980. char Ptrfmt[17], Indfmt[17], Valfmt[21], Rhsfmt[21];
  981. char line[BUFSIZ];
  982. char *ThisElement;
  983. if ((in_file = fopen( filename, "r")) == NULL) {
  984. fprintf(stderr,"Error: Cannot open file: %s\n",filename);
  985. return 0;
  986. }
  987. readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs,
  988. Ptrfmt, Indfmt, Valfmt, Rhsfmt,
  989. &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
  990. if (Nrhs <= 0)
  991. {
  992. fprintf(stderr, "Warn: Attempt to read auxillary vector(s) when none are present.\n");
  993. return 0;
  994. }
  995. if (Rhstype[0] != 'F' )
  996. {
  997. fprintf(stderr,"Warn: Attempt to read auxillary vector(s) which are not stored in Full form.\n");
  998. fprintf(stderr," Rhs must be specified as full. \n");
  999. return 0;
  1000. }
  1001. /* If reading complex data, allow for interleaved real and imaginary values. */
  1002. if ( Type[0] == 'C' ) {
  1003. Nentries = 2*Nrow;
  1004. } else {
  1005. Nentries = Nrow;
  1006. }
  1007. nvecs = 1;
  1008. if ( Rhstype[1] == 'G' ) nvecs++;
  1009. if ( Rhstype[2] == 'X' ) nvecs++;
  1010. if ( AuxType == 'G' && Rhstype[1] != 'G' ) {
  1011. fprintf(stderr, "Warn: Attempt to read auxillary Guess vector(s) when none are present.\n");
  1012. return 0;
  1013. }
  1014. if ( AuxType == 'X' && Rhstype[2] != 'X' ) {
  1015. fprintf(stderr, "Warn: Attempt to read auxillary eXact solution vector(s) when none are present.\n");
  1016. return 0;
  1017. }
  1018. ParseRfmt(Rhsfmt, &Rhsperline, &Rhswidth, &Rhsprec,&Rhsflag);
  1019. maxcol = Rhsperline*Rhswidth;
  1020. /* Lines to skip before starting to read RHS values... */
  1021. n = Ptrcrd + Indcrd + Valcrd;
  1022. for (i = 0; i < n; i++)
  1023. fgets(line, BUFSIZ, in_file);
  1024. /* start - number of initial aux vector entries to skip */
  1025. /* to reach first vector requested */
  1026. /* stride - number of aux vector entries to skip between */
  1027. /* requested vectors */
  1028. if ( AuxType == 'F' ) start = 0;
  1029. else if ( AuxType == 'G' ) start = Nentries;
  1030. else start = (nvecs-1)*Nentries;
  1031. stride = (nvecs-1)*Nentries;
  1032. fgets(line, BUFSIZ, in_file);
  1033. linel= strchr(line,'\n')-line;
  1034. if ( sscanf(line,"%*s") < 0 )
  1035. IOHBTerminate("iohb.c: Null (or blank) line in auxillary vector data region of HB file.\n");
  1036. col = 0;
  1037. /* Skip to initial offset */
  1038. for (i=0;i<start;i++) {
  1039. col += Rhswidth;
  1040. if ( col >= ( maxcol<linel?maxcol:linel ) ) {
  1041. fgets(line, BUFSIZ, in_file);
  1042. linel= strchr(line,'\n')-line;
  1043. if ( sscanf(line,"%*s") < 0 )
  1044. IOHBTerminate("iohb.c: Null (or blank) line in auxillary vector data region of HB file.\n");
  1045. col = 0;
  1046. }
  1047. }
  1048. if (Rhsflag == 'D') {
  1049. while( strchr(line,'D') ) *strchr(line,'D') = 'E';
  1050. }
  1051. /* Read a vector of desired type, then skip to next */
  1052. /* repeating to fill Nrhs vectors */
  1053. for (rhsi=0;rhsi<Nrhs;rhsi++) {
  1054. for (i=0;i<Nentries;i++) {
  1055. if ( col >= ( maxcol<linel?maxcol:linel ) ) {
  1056. fgets(line, BUFSIZ, in_file);
  1057. linel= strchr(line,'\n')-line;
  1058. if ( sscanf(line,"%*s") < 0 )
  1059. IOHBTerminate("iohb.c: Null (or blank) line in auxillary vector data region of HB file.\n");
  1060. if (Rhsflag == 'D') {
  1061. while( strchr(line,'D') ) *strchr(line,'D') = 'E';
  1062. }
  1063. col = 0;
  1064. }
  1065. ThisElement = &b[i*Rhswidth];
  1066. strncpy(ThisElement,line+col,Rhswidth);
  1067. if ( Rhsflag != 'F' && strchr(ThisElement,'E') == NULL ) {
  1068. /* insert a char prefix for exp */
  1069. last = strlen(ThisElement);
  1070. for (j=last+1;j>=0;j--) {
  1071. ThisElement[j] = ThisElement[j-1];
  1072. if ( ThisElement[j] == '+' || ThisElement[j] == '-' ) {
  1073. ThisElement[j-1] = Rhsflag;
  1074. break;
  1075. }
  1076. }
  1077. }
  1078. col += Rhswidth;
  1079. }
  1080. b+=Nentries*Rhswidth;
  1081. /* Skip any interleaved Guess/eXact vectors */
  1082. for (i=0;i<stride;i++) {
  1083. col += Rhswidth;
  1084. if ( col >= ( maxcol<linel?maxcol:linel ) ) {
  1085. fgets(line, BUFSIZ, in_file);
  1086. linel= strchr(line,'\n')-line;
  1087. if ( sscanf(line,"%*s") < 0 )
  1088. IOHBTerminate("iohb.c: Null (or blank) line in auxillary vector data region of HB file.\n");
  1089. col = 0;
  1090. }
  1091. }
  1092. }
  1093. fclose(in_file);
  1094. return Nrhs;
  1095. }
  1096. int readHB_newaux_char(const char* filename, const char AuxType, char** b, char** Rhsfmt)
  1097. {
  1098. FILE *in_file;
  1099. int Ptrcrd, Indcrd, Valcrd, Rhscrd;
  1100. int Nrow,Ncol,Nnzero,Nrhs;
  1101. int Rhsperline, Rhswidth, Rhsprec;
  1102. int Rhsflag;
  1103. char Title[73], Key[9], Type[4], Rhstype[4];
  1104. char Ptrfmt[17], Indfmt[17], Valfmt[21];
  1105. if ((in_file = fopen( filename, "r")) == NULL) {
  1106. fprintf(stderr,"Error: Cannot open file: %s\n",filename);
  1107. return 0;
  1108. }
  1109. *Rhsfmt = (char *)malloc(21*sizeof(char));
  1110. if ( *Rhsfmt == NULL ) IOHBTerminate("Insufficient memory for Rhsfmt.");
  1111. readHB_header(in_file, Title, Key, Type, &Nrow, &Ncol, &Nnzero, &Nrhs,
  1112. Ptrfmt, Indfmt, Valfmt, (*Rhsfmt),
  1113. &Ptrcrd, &Indcrd, &Valcrd, &Rhscrd, Rhstype);
  1114. fclose(in_file);
  1115. if ( Nrhs == 0 ) {
  1116. fprintf(stderr,"Warn: Requested read of aux vector(s) when none are present.\n");
  1117. return 0;
  1118. } else {
  1119. ParseRfmt(*Rhsfmt,&Rhsperline,&Rhswidth,&Rhsprec,&Rhsflag);
  1120. if ( Type[0] == 'C' ) {
  1121. fprintf(stderr, "Warning: Reading complex aux vector(s) from HB file %s.",filename);
  1122. fprintf(stderr, " Real and imaginary parts will be interlaced in b[].");
  1123. *b = (char *)malloc(Nrow*Nrhs*Rhswidth*sizeof(char)*2);
  1124. if ( *b == NULL ) IOHBTerminate("Insufficient memory for rhs.\n");
  1125. return readHB_aux_char(filename, AuxType, *b);
  1126. } else {
  1127. *b = (char *)malloc(Nrow*Nrhs*Rhswidth*sizeof(char));
  1128. if ( *b == NULL ) IOHBTerminate("Insufficient memory for rhs.\n");
  1129. return readHB_aux_char(filename, AuxType, *b);
  1130. }
  1131. }
  1132. }
  1133. int writeHB_mat_char(const char* filename, int M, int N,
  1134. int nz, const int colptr[], const int rowind[],
  1135. const char val[], int Nrhs, const char rhs[],
  1136. const char guess[], const char exact[],
  1137. const char* Title, const char* Key, const char* Type,
  1138. char* Ptrfmt, char* Indfmt, char* Valfmt, char* Rhsfmt,
  1139. const char* Rhstype)
  1140. {
  1141. /****************************************************************************/
  1142. /* The writeHB function opens the named file and writes the specified */
  1143. /* matrix and optional right-hand-side(s) to that file in Harwell-Boeing */
  1144. /* format. */
  1145. /* */
  1146. /* For a description of the Harwell Boeing standard, see: */
  1147. /* Duff, et al., ACM TOMS Vol.15, No.1, March 1989 */
  1148. /* */
  1149. /****************************************************************************/
  1150. FILE *out_file;
  1151. int i,j,acount,linemod,entry,offset;
  1152. int totcrd, ptrcrd, indcrd, valcrd, rhscrd;
  1153. int nvalentries, nrhsentries;
  1154. int Ptrperline, Ptrwidth, Indperline, Indwidth;
  1155. int Rhsperline, Rhswidth, Rhsprec;
  1156. int Rhsflag;
  1157. int Valperline = 1, Valwidth, Valprec;
  1158. int Valflag; /* Indicates 'E','D', or 'F' float format */
  1159. char pformat[16],iformat[16],vformat[19],rformat[19];
  1160. if ( Type[0] == 'C' ) {
  1161. nvalentries = 2*nz;
  1162. nrhsentries = 2*M;
  1163. } else {
  1164. nvalentries = nz;
  1165. nrhsentries = M;
  1166. }
  1167. if ( filename != NULL ) {
  1168. if ( (out_file = fopen( filename, "w")) == NULL ) {
  1169. fprintf(stderr,"Error: Cannot open file: %s\n",filename);
  1170. return 0;
  1171. }
  1172. } else out_file = stdout;
  1173. if ( Ptrfmt == NULL ) Ptrfmt = "(8I10)";
  1174. ParseIfmt(Ptrfmt,&Ptrperline,&Ptrwidth);
  1175. sprintf(pformat,"%%%dd",Ptrwidth);
  1176. if ( Indfmt == NULL ) Indfmt = Ptrfmt;
  1177. ParseIfmt(Indfmt,&Indperline,&Indwidth);
  1178. sprintf(iformat,"%%%dd",Indwidth);
  1179. if ( Type[0] != 'P' ) { /* Skip if pattern only */
  1180. if ( Valfmt == NULL ) Valfmt = "(4E20.13)";
  1181. ParseRfmt(Valfmt,&Valperline,&Valwidth,&Valprec,&Valflag);
  1182. sprintf(vformat,"%%%ds",Valwidth);
  1183. }
  1184. ptrcrd = (N+1)/Ptrperline;
  1185. if ( (N+1)%Ptrperline != 0) ptrcrd++;
  1186. indcrd = nz/Indperline;
  1187. if ( nz%Indperline != 0) indcrd++;
  1188. valcrd = nvalentries/Valperline;
  1189. if ( nvalentries%Valperline != 0) valcrd++;
  1190. if ( Nrhs > 0 ) {
  1191. if ( Rhsfmt == NULL ) Rhsfmt = Valfmt;
  1192. ParseRfmt(Rhsfmt,&Rhsperline,&Rhswidth,&Rhsprec, &Rhsflag);
  1193. sprintf(rformat,"%%%ds",Rhswidth);
  1194. rhscrd = nrhsentries/Rhsperline;
  1195. if ( nrhsentries%Rhsperline != 0) rhscrd++;
  1196. if ( Rhstype[1] == 'G' ) rhscrd+=rhscrd;
  1197. if ( Rhstype[2] == 'X' ) rhscrd+=rhscrd;
  1198. rhscrd*=Nrhs;
  1199. } else rhscrd = 0;
  1200. totcrd = 4+ptrcrd+indcrd+valcrd+rhscrd;
  1201. /* Print header information: */
  1202. fprintf(out_file,"%-72s%-8s\n%14d%14d%14d%14d%14d\n",Title, Key, totcrd,
  1203. ptrcrd, indcrd, valcrd, rhscrd);
  1204. fprintf(out_file,"%3s%11s%14d%14d%14d\n",Type," ", M, N, nz);
  1205. fprintf(out_file,"%-16s%-16s%-20s", Ptrfmt, Indfmt, Valfmt);
  1206. if ( Nrhs != 0 ) {
  1207. /* Print Rhsfmt on fourth line and */
  1208. /* optional fifth header line for auxillary vector information: */
  1209. fprintf(out_file,"%-20s\n%-14s%d\n",Rhsfmt,Rhstype,Nrhs);
  1210. } else fprintf(out_file,"\n");
  1211. offset = 1-_SP_base; /* if base 0 storage is declared (via macro definition), */
  1212. /* then storage entries are offset by 1 */
  1213. /* Print column pointers: */
  1214. for (i=0;i<N+1;i++)
  1215. {
  1216. entry = colptr[i]+offset;
  1217. fprintf(out_file,pformat,entry);
  1218. if ( (i+1)%Ptrperline == 0 ) fprintf(out_file,"\n");
  1219. }
  1220. if ( (N+1) % Ptrperline != 0 ) fprintf(out_file,"\n");
  1221. /* Print row indices: */
  1222. for (i=0;i<nz;i++)
  1223. {
  1224. entry = rowind[i]+offset;
  1225. fprintf(out_file,iformat,entry);
  1226. if ( (i+1)%Indperline == 0 ) fprintf(out_file,"\n");
  1227. }
  1228. if ( nz % Indperline != 0 ) fprintf(out_file,"\n");
  1229. /* Print values: */
  1230. if ( Type[0] != 'P' ) { /* Skip if pattern only */
  1231. for (i=0;i<nvalentries;i++)
  1232. {
  1233. fprintf(out_file,vformat,val+i*Valwidth);
  1234. if ( (i+1)%Valperline == 0 ) fprintf(out_file,"\n");
  1235. }
  1236. if ( nvalentries % Valperline != 0 ) fprintf(out_file,"\n");
  1237. /* Print right hand sides: */
  1238. acount = 1;
  1239. linemod=0;
  1240. if ( Nrhs > 0 ) {
  1241. for (j=0;j<Nrhs;j++) {
  1242. for (i=0;i<nrhsentries;i++)
  1243. {
  1244. fprintf(out_file,rformat,rhs+i*Rhswidth);
  1245. if ( acount++%Rhsperline == linemod ) fprintf(out_file,"\n");
  1246. }
  1247. if ( acount%Rhsperline != linemod ) {
  1248. fprintf(out_file,"\n");
  1249. linemod = (acount-1)%Rhsperline;
  1250. }
  1251. if ( Rhstype[1] == 'G' ) {
  1252. for (i=0;i<nrhsentries;i++)
  1253. {
  1254. fprintf(out_file,rformat,guess+i*Rhswidth);
  1255. if ( acount++%Rhsperline == linemod ) fprintf(out_file,"\n");
  1256. }
  1257. if ( acount%Rhsperline != linemod ) {
  1258. fprintf(out_file,"\n");
  1259. linemod = (acount-1)%Rhsperline;
  1260. }
  1261. }
  1262. if ( Rhstype[2] == 'X' ) {
  1263. for (i=0;i<nrhsentries;i++)
  1264. {
  1265. fprintf(out_file,rformat,exact+i*Rhswidth);
  1266. if ( acount++%Rhsperline == linemod ) fprintf(out_file,"\n");
  1267. }
  1268. if ( acount%Rhsperline != linemod ) {
  1269. fprintf(out_file,"\n");
  1270. linemod = (acount-1)%Rhsperline;
  1271. }
  1272. }
  1273. }
  1274. }
  1275. }
  1276. if ( fclose(out_file) != 0){
  1277. fprintf(stderr,"Error closing file in writeHB_mat_char().\n");
  1278. return 0;
  1279. } else return 1;
  1280. }
  1281. int ParseIfmt(char* fmt, int* perline, int* width)
  1282. {
  1283. /*************************************************/
  1284. /* Parse an *integer* format field to determine */
  1285. /* width and number of elements per line. */
  1286. /*************************************************/
  1287. char *tmp;
  1288. if (fmt == NULL ) {
  1289. *perline = 0; *width = 0; return 0;
  1290. }
  1291. upcase(fmt);
  1292. tmp = strchr(fmt,'(');
  1293. tmp = substr(fmt,tmp - fmt + 1, strchr(fmt,'I') - tmp - 1);
  1294. *perline = atoi(tmp);
  1295. tmp = strchr(fmt,'I');
  1296. tmp = substr(fmt,tmp - fmt + 1, strchr(fmt,')') - tmp - 1);
  1297. return *width = atoi(tmp);
  1298. }
  1299. int ParseRfmt(char* fmt, int* perline, int* width, int* prec, int* flag)
  1300. {
  1301. /*************************************************/
  1302. /* Parse a *real* format field to determine */
  1303. /* width and number of elements per line. */
  1304. /* Also sets flag indicating 'E' 'F' 'P' or 'D' */
  1305. /* format. */
  1306. /*************************************************/
  1307. char* tmp;
  1308. char* tmp2;
  1309. char* tmp3;
  1310. int len;
  1311. if (fmt == NULL ) {
  1312. *perline = 0;
  1313. *width = 0;
  1314. flag = NULL;
  1315. return 0;
  1316. }
  1317. upcase(fmt);
  1318. if (strchr(fmt,'(') != NULL) fmt = strchr(fmt,'(');
  1319. if (strchr(fmt,')') != NULL) {
  1320. tmp2 = strchr(fmt,')');
  1321. while ( strchr(tmp2+1,')') != NULL ) {
  1322. tmp2 = strchr(tmp2+1,')');
  1323. }
  1324. *(tmp2+1) = (int) NULL;
  1325. }
  1326. if (strchr(fmt,'P') != NULL) /* Remove any scaling factor, which */
  1327. { /* affects output only, not input */
  1328. if (strchr(fmt,'(') != NULL) {
  1329. tmp = strchr(fmt,'P');
  1330. if ( *(++tmp) == ',' ) tmp++;
  1331. tmp3 = strchr(fmt,'(')+1;
  1332. len = tmp-tmp3;
  1333. tmp2 = tmp3;
  1334. while ( *(tmp2+len) != (int) NULL ) {
  1335. *tmp2=*(tmp2+len);
  1336. tmp2++;
  1337. }
  1338. *(strchr(fmt,')')+1) = (int) NULL;
  1339. }
  1340. }
  1341. if (strchr(fmt,'E') != NULL) {
  1342. *flag = 'E';
  1343. } else if (strchr(fmt,'D') != NULL) {
  1344. *flag = 'D';
  1345. } else if (strchr(fmt,'F') != NULL) {
  1346. *flag = 'F';
  1347. } else {
  1348. fprintf(stderr,"Real format %s in H/B file not supported.\n",fmt);
  1349. return 0;
  1350. }
  1351. tmp = strchr(fmt,'(');
  1352. tmp = substr(fmt,tmp - fmt + 1, strchr(fmt,*flag) - tmp - 1);
  1353. *perline = atoi(tmp);
  1354. tmp = strchr(fmt,*flag);
  1355. if ( strchr(fmt,'.') ) {
  1356. *prec = atoi( substr( fmt, strchr(fmt,'.') - fmt + 1, strchr(fmt,')') - strchr(fmt,'.')-1) );
  1357. tmp = substr(fmt,tmp - fmt + 1, strchr(fmt,'.') - tmp - 1);
  1358. } else {
  1359. tmp = substr(fmt,tmp - fmt + 1, strchr(fmt,')') - tmp - 1);
  1360. }
  1361. return *width = atoi(tmp);
  1362. }
  1363. char* substr(const char* S, const int pos, const int len)
  1364. {
  1365. int i;
  1366. char *SubS;
  1367. if ( pos+len <= strlen(S)) {
  1368. SubS = (char *)malloc(len+1);
  1369. if ( SubS == NULL ) IOHBTerminate("Insufficient memory for SubS.");
  1370. for (i=0;i<len;i++) SubS[i] = S[pos+i];
  1371. SubS[len] = (char) NULL;
  1372. } else {
  1373. SubS = NULL;
  1374. }
  1375. return SubS;
  1376. }
  1377. #include<ctype.h>
  1378. void upcase(char* S)
  1379. {
  1380. /* Convert S to uppercase */
  1381. int i,len;
  1382. if ( S == NULL ) return;
  1383. len = strlen(S);
  1384. for (i=0;i< len;i++)
  1385. S[i] = toupper(S[i]);
  1386. }
  1387. void IOHBTerminate(const char* message)
  1388. {
  1389. fprintf(stderr,"%s",message);
  1390. exit(1);
  1391. }