1 /* 2 ** File 3 ** example1.cpp 4 ** Copyright 1997, The California Institute of Technology 5 ** 6 ** Usage 7 ** example1 <file type> <file name expression>... 8 ** 9 ** Function 10 ** Add the files named on the command line to FEI. Then retrieve them. 11 ** Finally delete each one from FEI. 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 27 int main (int argc, char *argv[]) 28 { 29 // Used for error messages 30 char msg[256]; 31 int status = FEI_OK; 32 33 int i; 34 35 if (argc < 2) 36 { 37 fprintf (stderr, "\n%s <file type> <file name expression>...\n\n", argv[0]); 38 return (FEI_INFO); 39 } 40 41 const char *fileType = argv[1]; 42 43 // Create an Fei object on the stack and connect to an FEI server 44 // using the supplied file type. If the status is not 0 (FEI_OK), 45 // return (exit). Use the FeiMsg reporting object to report messages. 46 Fei fei; 47 if ((status = fei.connect (fileType)) != FEI_OK) 48 return (status); 49 sprintf (msg, "\nConnected to Fei for file type \"%s\".", fileType); 50 feiMsg().record (msg, FEI_INFO); 51 52 // Each of the remaining command line arguments is a file to be added. 53 // We use the Fei object defined for our target file type. We first 54 // queue the transaction to the request queue and then wait on the result 55 // queue for the file to be added. Each file is represented by an FeiProile 56 // object that is returned to us. Once we're done with the object we 57 // must delete it. 58 FeiProfile *profile; 59 for (i = 2; i < argc; i++) 60 { 61 if (status = fei.add (argv[i])) 62 { 63 status = FEI_ERROR; 64 sprintf (msg, "Can't queue a request to add file \"%s\".", argv[i]); 65 feiMsg().record (msg, status); 66 } 67 else 68 { 69 while ((profile = fei.result ()) != (FeiProfile *)NULL) 70 { 71 if (profile->getStatus() == FEI_OK) 72 { 73 sprintf (msg, "Added %s.", profile->getFileName ()); 74 feiMsg().record (msg, FEI_INFO); 75 } 76 else 77 { 78 // error recovery 79 } 80 // When were done with a profile record we must delete it. 81 delete profile; 82 } 83 } 84 } 85 86 // Now get the files we just added and then delete them from FEI. 87 // We'll set an option so the existing files with the same name is 88 // renamed with a version number before we actually write the new 89 // file. 90 const char *localPath = "."; // write to current working directory 91 fei.setOptions (feiVersion); 92 for (i = 2; i < argc; i++) 93 { 94 if (status = fei.get (argv[i], localPath)) 95 { 96 status = FEI_ERROR; 97 sprintf (msg, "Can't queue a request to get file \"%s\".", argv[i]); 98 feiMsg().record (msg, status); 99 continue; 100 } 101 // Once we queue a request to get a file, queue a second request to 102 // delete the same file. 103 if (status = fei.remove (argv[i])) 104 { 105 status = FEI_ERROR; 106 sprintf (msg, "Can't queue a request to delete file \"%s\".", 107 argv[i]); 108 feiMsg().record (msg, status); 109 continue; 110 } 111 // We want both of the queued transactions to complete, so 112 // we check the transaction count and only leave the loop when 113 // it's 0. Initially it was 2. 114 char *command; 115 while (fei.transactions () > 0) 116 { 117 while ((profile = fei.result ()) != (FeiProfile *)NULL) 118 { 119 if (profile->getStatus() == FEI_OK) 120 { 121 // What type of command is associated with this profile? 122 if (profile->getCommand () == 'g') 123 command = "got"; 124 else if (profile->getCommand () == 'd') 125 command = "deleted"; 126 else 127 command = "unknown"; 128 sprintf (msg, "%s file %s.", command, profile->getFileName ()); 129 feiMsg().record (msg, FEI_INFO); 130 } 131 else 132 { 133 // error recovery 134 } 135 delete profile; 136 } 137 } 138 } 139 // This isn't necessary, because the connection is closed when the Fei 140 // object goes out of scope, but we include it so you see how it fits in 141 // with the other calls in an FEI session. If we were going to connect 142 // again later using the same object, we'd need to use this member 143 // function and then reconnect later. 144 feiMsg().record ("Disconnecting from Fei.", FEI_INFO); 145 fei.disconnect (); 146 147 return (status); 148 }