Copyright © 1997 The California Institute of Technology
All rights reserved.

example2.cpp


  1 /*
  2 ** File
  3 **      example2.cpp
  4 **      Copyright 1997, The California Institute of Technology
  5 **
  6 ** Usage
  7 **      example2 <file type> '<file name expression>'
  8 **
  9 ** Function
 10 **      List a set of files matching the file name expression. If the user
 11 **      request that we get the file, display it.
 12 **
 13 ** Creator
 14 **      J. Rector
 15 **
 16 ** Created
 17 **      June, 1997
 18 **
 19 ** History
 20 **
 21 */
 22 
 23 #include "Fei.h"
 24 #include <stdio.h>
 25 
 26 // Prototypes
 27 void getFile (Fei& fei, const char *fileName);
 28 
 29 // Made static so we can use them from multiple methods.
 30 static char msg[256];
 31 static int status = FEI_OK;
 32 
 33 
 34 int main (int argc, char *argv[])
 35 {
 36 
 37    if (argc < 2)
 38    {
 39       fprintf (stderr, "\n%s <file type> '<file name expression>'\n\n", argv[0]);
 40       return (FEI_INFO);
 41    }
 42    const char *fileType = argv[1];
 43    const char *fileNameExpr = argv[2];
 44 
 45    // Create two Fei objects - one to list files and the other to get them.
 46    // Connect to the file type using both Fei objects and then list the
 47    // requested files for that file type.
 48    Fei list;
 49    Fei get;
 50 
 51    if (status = list.connect (fileType))
 52       return (status);
 53    if (status = get.connect (fileType))
 54       return (status);
 55 
 56    if (status = list.list (fileNameExpr))
 57          return (status);
 58 
 59    fprintf (stderr, "\nRespond with the letter 'g' to display the\n");
 60    fprintf (stderr, "contents of the listed file. Press <enter> or\n");
 61    fprintf (stderr, "<return> to skip the file.\n\n");
 62    FeiProfile *profile;
 63    int c;
 64    bool foundFiles = false;
 65    while ((profile = list.result ()) != (FeiProfile *)NULL)
 66    {
 67       foundFiles = true;
 68       if (profile->getStatus () != FEI_OK)
 69       {
 70          sprintf (msg, "List result error\n");
 71          feiMsg().record (msg, profile->getStatus());
 72       }
 73       else
 74       {
 75          fprintf (stdout, "%s :", profile->getProfileString ());
 76          feiMsg().record (msg, FEI_INFO);
 77          if ((c = fgetc (stdin)) == EOF)
 78          {
 79             feiMsg().record ("User requested return.\n", FEI_INFO);
 80             return (FEI_OK);
 81          }
 82          fflush (stdin);
 83          if (c == 'g')
 84             getFile (get, profile->getFileName());
 85       }
 86       delete profile;
 87    }
 88 
 89    // Print a message if we couldn't match the file expression.
 90    if (!foundFiles)
 91       feiMsg().record ("No files found.\n", FEI_INFO);
 92 
 93    // Disconnect actually happens when the object's destructors are called.
 94    feiMsg().record ("\nDisconnecting from Fei.\n", FEI_INFO);
 95    return (0);
 96 }
 97 
 98 /*
 99 ** Get the file using the supplied Fei object. Since the 'localPath' is
100 ** FEI_IN_MEMORY, the content of the file is returned as part of the
101 ** file's profile record. Print the contents of the file and delete the
102 ** profile.
103 */
104 void getFile (Fei& fei, const char *fileName)
105 {
106    if (status = fei.get (fileName, FEI_IN_MEMORY))
107       return;
108 
109    FeiProfile *profile;
110    while ((profile = fei.result ()) != (FeiProfile *)NULL)
111    {
112       if (profile->getStatus () != FEI_OK)
113          feiMsg().record ("Can't get file.\n", profile->getStatus());
114       else
115       {
116          fwrite (profile->getAddress(), profile->getSize(), 1, stdout);
117          fputc ('\n', stdout);
118          fflush (stdout);
119       }
120       delete profile;
121    }
122 }


example2.cpp