1 /*  readAIJ.c  */
2 
3 #include "../InpMtx.h"
4 
5 /*--------------------------------------------------------------------*/
6 int
main(int argc,char * argv[])7 main ( int argc, char *argv[] )
8 /*
9    -------------------------------
10    read in Dave Young format
11    construct a InpMtx object and
12    write it out to a file
13 
14    created -- 97oct17, cca
15    -------------------------------
16 */
17 {
18 char     *inFileName, *outFileName ;
19 double   ent ;
20 InpMtx   *inpmtx ;
21 FILE     *inputFile, *msgFile ;
22 int      icol, irow, jcol, jrow,
23          msglvl, ncol, nentInRow, nrow, rc ;
24 int      *ivec1, *ivec2 ;
25 
26 if ( argc != 5 ) {
27    fprintf(stdout,
28   "\n\n usage : readAIJ2 msglvl msgFile inputFile outFile "
29    "\n    msglvl    -- message level"
30    "\n    msgFile   -- message file"
31    "\n    inputFile -- input file for a(i,j) entries"
32    "\n       the first line must be \"nrow ncol \""
33    "\n          next lines are \"irow nentInRow\""
34    "\n                         \"jcol entry \""
35    "\n                         ..."
36    "\n                         \"jcol entry \""
37    "\n          ..." "\n       endif"
38    "\n    outFile -- output file, must be *.inpmtxf or *.inpmtxb"
39    "\n") ;
40    return(0) ;
41 }
42 msglvl = atoi(argv[1]) ;
43 if ( strcmp(argv[2], "stdout") == 0 ) {
44    msgFile = stdout ;
45 } else if ( (msgFile = fopen(argv[2], "a")) == NULL ) {
46    fprintf(stderr, "\n fatal error in %s"
47            "\n unable to open file %s\n",
48            argv[0], argv[2]) ;
49    return(-1) ;
50 }
51 inFileName  = argv[3] ;
52 outFileName = argv[4] ;
53 fprintf(msgFile,
54         "\n readAIJ "
55         "\n msglvl    -- %d"
56         "\n msgFile   -- %s"
57         "\n inputFile -- %s"
58         "\n outFile   -- %s"
59         "\n",
60         msglvl, argv[2], inFileName, outFileName ) ;
61 fflush(msgFile) ;
62 /*
63    ----------------------------
64    open the input file and read
65    #rows #columns
66    ----------------------------
67 */
68 if ( (inputFile = fopen(inFileName, "r")) == NULL ) {
69    fprintf(stderr, "\n fatal error in %s"
70            "\n unable to open file %s\n",
71            argv[0], inFileName) ;
72    return(-1) ;
73 }
74 rc = fscanf(inputFile, "%d %d", &nrow, &ncol) ;
75 if ( rc != 2 ) {
76    fprintf(stderr, "\n fatal error in %s"
77            "\n %d of 2 fields read on first line of file %s",
78            argv[0], rc, inFileName) ;
79    return(-1) ;
80 }
81 if ( msglvl > 1 ) {
82    fprintf(msgFile, "\n\n read in nrow = %d, ncol = %d", nrow, ncol) ;
83    fflush(msgFile) ;
84 }
85 /*
86    --------------------------------------------------
87    initialize the object
88    set coordType = INPMTX_BY_ROWS --> row coordinates
89    --------------------------------------------------
90 */
91 inpmtx = InpMtx_new() ;
92 InpMtx_init(inpmtx, INPMTX_BY_ROWS, SPOOLES_REAL, 10*nrow, 0) ;
93 /*
94    -------------------------------------------------
95    read in the entries and load them into the object
96    -------------------------------------------------
97 */
98 for ( irow = 0 ; irow < nrow ; irow++ ) {
99    rc = fscanf(inputFile, "%d %d", &jrow, &nentInRow) ;
100    if ( rc != 2 ) {
101       fprintf(stderr, "\n fatal error in %s"
102               "\n %d of 2 fields read on entry %d of file %s",
103               argv[0], rc, irow, inFileName) ;
104       return(-1) ;
105    }
106    for ( icol = 0 ; icol < nentInRow ; icol++ ) {
107       rc = fscanf(inputFile, "%d %lf", &jcol, &ent) ;
108       if ( rc != 2 ) {
109          fprintf(stderr, "\n fatal error in %s"
110                  "\n %d of 2 fields read on entry %d of file %s",
111                  argv[0], rc, irow, inFileName) ;
112          return(-1) ;
113       }
114       InpMtx_inputRealEntry(inpmtx, jrow-1, jcol-1, ent) ;
115    }
116 }
117 /*
118    -----------------------------
119    sort and compress the entries
120    -----------------------------
121 */
122 InpMtx_changeStorageMode(inpmtx, 3) ;
123 if ( msglvl > 1 ) {
124    fprintf(msgFile, "\n\n sorted, compressed and vector form") ;
125    InpMtx_writeForHumanEye(inpmtx, msgFile) ;
126    fflush(msgFile) ;
127 }
128 /*
129    ---------------------------
130    write out the InpMtx object
131    ---------------------------
132 */
133 if ( strcmp(outFileName, "none") != 0 ) {
134    rc = InpMtx_writeToFile(inpmtx, outFileName) ;
135    fprintf(msgFile,
136            "\n return value %d from InpMtx_writeToFile(%p,%s)",
137            rc, inpmtx, outFileName) ;
138 }
139 /*
140    ---------------------
141    free the working data
142    ---------------------
143 */
144 InpMtx_free(inpmtx) ;
145 
146 fprintf(msgFile, "\n") ;
147 fclose(msgFile) ;
148 
149 return(1) ; }
150 
151 /*--------------------------------------------------------------------*/
152