IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 16902


Ignore:
Timestamp:
Mar 9, 2008, 11:05:03 AM (18 years ago)
Author:
eugene
Message:

fix the open/close of multiple streams

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/lib.shell/gprint.c

    r16901 r16902  
    9191}
    9292
     93// close if necessary, set to file (may be NULL)
     94void gprintCloseFile (gpStream *stream, FILE *file) {
     95
     96  int i, Nmatch;
     97
     98  // do not close the file if the new file is the same
     99  if (stream[0].file == file) return;
     100
     101  // do not close the file if the old one is NULL
     102  if (stream[0].file == NULL) {
     103    stream[0].file = file;   
     104    return;
     105  }
     106
     107  // check the special cases
     108  if (stream[0].file == stdout) {
     109    stream[0].file = file;
     110    return;
     111  }
     112  if (stream[0].file == stderr) {
     113    stream[0].file = file;
     114    return;
     115  }
     116
     117  // must we close the existing file? if still being used, then no
     118  Nmatch = 0;
     119  for (i = 0; i < Nstreams; i++) {
     120    if (stream == &streams[i]) continue;
     121    if (streams[i].file == stream[0].file) Nmatch ++;
     122  }
     123  if (Nmatch == 0) {
     124    fflush (stream[0].file);
     125    fclose (stream[0].file);
     126  }
     127
     128  stream[0].file = file;
     129  return;
     130}
     131
    93132void gprintSetBuffer (gpDest dest) {
    94133
     
    97136  stream = gprintGetStream (dest);
    98137
    99   /* if we have an open file: flush it, close it, null it */
    100   if (stream[0].file != NULL) {
    101     fflush (stream[0].file);
    102     if (stream[0].file == stdout) goto skip_close;
    103     if (stream[0].file == stderr) goto skip_close;
    104     fclose (stream[0].file);
    105 
    106   skip_close:
    107     stream[0].file = NULL;
    108   }
     138  // close the existing file (if needed), set to NULL
     139  gprintCloseFile (stream, NULL);
     140
     141  assert (stream[0].buffer);
     142  FlushIOBuffer (stream[0].buffer);
    109143 
    110   if (stream[0].buffer == NULL) {
    111     ALLOCATE (stream[0].buffer, IOBuffer, 1);
    112     InitIOBuffer (stream[0].buffer, 64);
    113   } else {
    114     FlushIOBuffer (stream[0].buffer);
    115   }
    116  
    117   /* this element may be redundant with the NULL state of file and buffer */
    118144  stream[0].mode = GP_BUFF;
    119145}
     
    152178  FILE *file;
    153179
     180  assert (stream[0].buffer);
     181
    154182  /* if we have an open buffer, free it and null it */
    155   if (stream[0].buffer != NULL) {
    156     FreeIOBuffer (stream[0].buffer);
    157     free (stream[0].buffer);
    158     stream[0].buffer = NULL;
     183  if (stream[0].buffer[0].Nbuffer) {
     184    // XXX we drop what was on the buffer, send it to the old or the new file?
     185    FlushIOBuffer (stream[0].buffer);
    159186  }
    160187
     
    167194  }
    168195
    169   /* we allow the user to set stdout to ERR and stderr to LOG if they want */
     196  // we allow the user to set stdout to ERR and stderr to LOG if they want
    170197  if (!strcmp (stream[0].name, "stdout")) {
    171198    stream[0].file = stdout;
     
    177204  }
    178205 
    179   // open the file : use a local FILE pointer. only close old pointer if no one else is
    180   // using it
     206  // open the file. only close old pointer if no one else is using it
    181207  file = fopen (stream[0].name, "a");
    182208  if (file == NULL) {
     
    188214  }
    189215
    190   /* check the special cases */
    191   if (stream[0].file == stdout) {
    192     stream[0].file = file;
    193     return;
    194   }
    195   if (stream[0].file == stderr) {
    196     stream[0].file = file;
    197     return;
    198   }
    199 
    200   /* must we close the existing file? */
    201   if (stream[0].file != NULL) {
    202     Nmatch = 0;
    203     for (i = 0; i < Nstreams; i++) {
    204       if (streams[i].file == stream[0].file) Nmatch ++;
    205     }
    206     if (file == stream[0].file) Nmatch ++;
    207     if (Nmatch == 1) {
    208       fflush (stream[0].file);
    209       fclose (stream[0].file);
    210     }
    211     stream[0].file = file;
    212   }
     216  // close the existing file (if needed), set to new file
     217  gprintCloseFile (stream, file);
    213218  return;
    214219}
     
    237242
    238243  va_start (argp, format);
    239  
    240   // gprint (GP_ERR, "GP_FILE: %d\n", GP_FILE);
    241   // gprint (GP_ERR, "GP_BUFF: %d\n", GP_BUFF);
    242   // gprint (GP_ERR, "mode: %d\n", stream[0].mode);
    243244
    244245  if (stream[0].mode == GP_FILE) {
    245     // gprint (GP_ERR, "printing to FILE\n");
    246246    status = vfprintf (stream[0].file, format, argp);
    247247    if (status < 0) {
     
    249249    }
    250250  } else {
    251     // gprint (GP_ERR, "printing to BUFFER\n");
    252251    vPrintIOBuffer (stream[0].buffer, format, argp);
    253252  }
     
    267266    fwrite (buffer, size, N, stream[0].file);
    268267  } else {
     268    // XXX can we not use exising IOBuffer APIs here?
    269269    outbuff = stream[0].buffer;
    270270    Nbyte = size * N;
Note: See TracChangeset for help on using the changeset viewer.