source: git/MP/examples/send-tree.c @ f78374

fieker-DuValspielwiese
Last change on this file since f78374 was 678cfd, checked in by Olaf Bachmann <obachman@…>, 27 years ago
This commit was generated by cvs2svn to compensate for changes in r337, which included commits to RCS files with non-trunk default branches. git-svn-id: file:///usr/local/Singular/svn/trunk@338 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 10.4 KB
Line 
1/*
2  send-tree.c - simple client process
3*/
4
5#include <sys/types.h>
6#include <string.h>
7#include <fcntl.h>
8#include "MP.h"
9#include "gmp.h"
10/*
11   imp_file2tree() - Routine to read sample data from a file.  The routine
12                     does some simple checking (e.g., tells you if it
13                     expects an annotation and doesn't get one or gets
14                     a type it doesn't understand).
15*/
16   
17/* global to return an error message when we read something we
18   think should be an annotation, but we don't recognize it */
19
20char *badstring;
21
22/**********************************************************************
23 * FUNCTION: imp_get_annotation
24 * ARGUMENT: link - pointer to a stream structure
25 *           fd    - file descriptor for the input file
26 * RETURN:   MP_AnnotFlags_t - flags associated with the annotation.
27 * PURPOSE:  Read a single annotation and associated flags from the
28 *           input file associated with fd and "put" the annotation
29 *           to the stream associated with link.  We don't handle
30 *           valuated annotations here.  That is done by the caller
31 *           (which is why we return the annotations flags.
32 **********************************************************************/
33MP_AnnotFlags_t
34#ifdef _ANSI_
35imp_get_annotation(MP_Link_pt link, FILE *fd)
36#else
37imp_get_annotation(link, fd)
38MP_Link_pt link;
39FILE *fd;
40#endif
41{
42  char type[32];
43  MP_AnnotType_t atype = 0;
44  long   dtag, flags;
45
46  fscanf(fd, "%s", type); 
47  if(strcmp(type, "Label_AP") == 0) atype = MP_AnnotMpLabel;
48  else if(strcmp(type, "Reference_AP") == 0) atype = MP_AnnotMpRef;
49  else if(strcmp(type, "Prototype_AP") == 0)  atype = MP_AnnotProtoPrototype;
50  else if(strcmp(type, "Store_AP") == 0) atype = MP_AnnotMpStore;
51  else if(strcmp(type, "Retrieve_AP") == 0) atype = MP_AnnotMpRetrieve;
52  else if(strcmp(type, "Stored_AP") == 0)  atype = MP_AnnotMpStored;
53  else if(strcmp(type, "Source_AP") == 0)  atype = MP_AnnotMpSource;
54  else if(strcmp(type, "Comment_AP") == 0)  atype = MP_AnnotMpComment;
55  else if(strcmp(type, "Timing_AP") == 0)  atype = MP_AnnotMpTiming;
56  else if(strcmp(type, "Type_AP") == 0)  atype = MP_AnnotMpType;
57  else if(strcmp(type, "Units_AP") == 0)  atype = MP_AnnotMpUnits;
58  else {
59    badstring = type;
60    return 99;
61  }
62  if (atype != 0) {
63    fscanf(fd, "%d  %X", &dtag, &flags);
64    MP_PutAnnotationPacket(link, dtag, atype, flags);
65  }
66  return flags;
67}
68
69
70/**********************************************************************
71 * FUNCTION: imp_get_term
72 * ARGUMENT: link - pointer to a stream structure
73 *           fd    - file descriptor for the input file
74 * RETURN:   int - 0 means there is no more data to read
75 *                 1 means there are more trees to be read.
76 * PURPOSE:  Read a term of a tree from the input file associated
77 *           with fd and "put" the term (along with associated
78 *           annotations and children) to the stream associated with
79 *           link. 
80 **********************************************************************/
81int
82#ifdef _ANSI_
83imp_get_term(MP_Link_pt link, FILE *fd)
84#else
85imp_get_term(link, fd)
86MP_Link_pt link;
87FILE *fd;
88#endif
89{
90  char type[32], str[64], raw[512], c;
91  MP_ApInt_t apint;
92  MP_ApReal_t  apreal;
93  MP_AnnotFlags_t flags = 0;
94  MP_NumAnnot_t   annots = 0;
95  MP_NumChild_t   num_children = 0;
96  unsigned long   dtag, cval;
97  MP_NodeHeader_t hdr;
98  unsigned int    my_uint;
99  int i, sint;
100  float  r32;
101  double r64;
102
103  fscanf(fd, "%s", type); 
104  if (strcmp(type, "MP_Sint32Type") == 0) {
105    fscanf(fd, "%d %u", &sint, &annots);
106    IMP_PutNodeHeader(link, MP_Sint32Type, 0, 0, annots, 0);
107    IMP_PutSint32(link, sint);
108  }
109  else if (strcmp(type, "MP_Uint32Type") == 0) {
110    fscanf(fd, "%u %u", &my_uint, &annots);
111    IMP_PutNodeHeader(link, MP_Uint32Type, 0, 0, annots, 0);
112    IMP_PutUint32(link, my_uint);
113  }
114  else if (strcmp(type, "MP_Sint8Type") == 0) {
115    fscanf(fd, "%d %u", &sint, &annots);
116    MP_PutSint8Packet(link, sint, annots);
117  }
118  else if (strcmp(type, "MP_Uint8Type") == 0) {
119    fscanf(fd, "%d %u", &my_uint, &annots);
120    MP_PutUint8Packet(link, my_uint, annots);
121  }
122  else if (strcmp(type, "MP_BooleanType") == 0) {
123    fscanf(fd, "%1s %d", &c, &annots);
124    my_uint = (c == 'T') ? 1 : 0;
125    MP_PutBooleanPacket(link, my_uint, annots);
126  }
127  else if (strcmp(type, "MP_ApIntType") == 0) {
128    mpz_init((mpz_ptr)&apint);
129    /* now get the apint from the file */
130    mpz_inp_str((mpz_ptr)&apint, fd, 10);
131    fscanf(fd, "%u ", &annots);
132    IMP_PutNodeHeader(link, MP_ApIntType, 0, 0, annots, 0);
133    IMP_PutApInt(link, &apint);
134    mpz_clear((mpz_ptr)&apint);
135  }
136  else if (strcmp(type, "MP_ApRealType") == 0) {
137    mpf_init((mpf_ptr)&apreal);
138    /* now get the apreal from the file */
139    mpf_inp_str((mpf_ptr)&apreal, fd, 10);
140    fscanf(fd, "%u ", &annots);
141    IMP_PutNodeHeader(link, MP_ApRealType, 0, 0, annots, 0);
142    IMP_PutApReal(link, &apreal);
143    mpf_clear((mpf_ptr)&apreal); 
144  }
145  else if (strcmp(type, "MP_Real32Type") == 0) {
146    fscanf(fd, "%f %u", &r32, &annots);
147    IMP_PutNodeHeader(link, MP_Real32Type, 0, 0, annots, 0);
148    IMP_PutReal32(link, r32);
149  }
150  else if (strcmp(type, "MP_Real64Type") == 0) {
151    fscanf(fd, "%lG %u", &r64, &annots);
152    IMP_PutNodeHeader(link, MP_Real64Type, 0, 0, annots, 0);
153    IMP_PutReal64(link, r64);
154  }
155  else if (strcmp(type, "MP_StringType") == 0) {
156    fscanf(fd, "%s %u", str, &annots);
157    IMP_PutNodeHeader(link, MP_StringType, 0, 0, annots, 0);
158    IMP_PutString(link, str);
159  }
160  else if (strcmp(type, "MP_IdentifierType") == 0) {
161    fscanf(fd, "%d %s %u", &dtag, str, &annots);
162    IMP_PutNodeHeader(link, MP_IdentifierType, dtag, 0, annots, 00);
163    IMP_PutIdentifier(link, str);
164  }
165  else if (strcmp(type, "MP_CommonLatinIdentifierType") == 0) {
166    fscanf(fd, "%d %c %u", &dtag, &c, &annots);
167    IMP_PutNodeHeader(link, MP_CommonLatinIdentifierType, dtag,c,annots,0);
168  }
169  else if (strcmp(type, "MP_CommonGreekIdentifierType") == 0) {
170    fscanf(fd, "%d %c %u", &dtag, &c, &annots);
171    IMP_PutNodeHeader(link, MP_CommonGreekIdentifierType, dtag,c,annots,0);
172  }
173  else if (strcmp(type, "MP_CommonConstantType") == 0) {
174    fscanf(fd, "%d %d %u", &dtag, &cval, &annots);
175    IMP_PutNodeHeader(link, MP_CommonConstantType, dtag, cval, annots, 0);
176    }
177  else if (strcmp(type, "MP_ConstantType") == 0) {
178    fscanf(fd, "%d %s %u", &dtag, str, &annots); 
179    MP_PutConstantPacket(link, dtag, str, annots);
180  }
181  else if (strcmp(type, "MP_MetaType") == 0) {
182    fscanf(fd, "%d %s %u", &dtag, str, &annots);
183    IMP_PutNodeHeader(link, MP_MetaType, 0, 0, annots, 0);
184    IMP_PutMetaType(link, str);
185  }
186  else if (strcmp(type, "MP_RawType") == 0) {
187    fscanf(fd, "%s %u", raw, &annots);
188    IMP_PutNodeHeader(link, MP_RawType, 0, 0, annots, 0);
189    IMP_PutRaw(link, raw, strlen(raw));
190  }
191  else if (strcmp(type, "MP_OperatorType") == 0) {
192    fscanf(fd, "%d %s %u %u",&dtag, str, &annots, &num_children);
193    IMP_PutNodeHeader(link, MP_OperatorType, dtag,0, annots, num_children);
194    IMP_PutOperator(link, str);
195  }
196  else if (strcmp(type, "MP_CommonOperatorType") == 0) {
197    fscanf(fd, "%d %d %u %u", &dtag, &cval, &annots, &num_children);
198    IMP_PutNodeHeader(link, MP_CommonOperatorType, dtag, cval, annots, 
199                      num_children);
200      if (dtag == MP_MpDict  && cval == MP_CopMpEndSession) {
201        return 0;
202        }
203    }
204  /* first we get the annotations, if there are any */
205  while (annots-- > 0) {
206    if ((flags = imp_get_annotation(link, fd)) == 99) {
207      fprintf(stderr,"Error in input file, annotation expected,");
208      fprintf(stderr," received %s\n", badstring);
209      exit(1);
210    }
211    if (flags & MP_AnnotValuated) 
212      imp_get_term(link, fd);
213  }
214 
215  /* now get the children */
216  while (num_children-- > 0) imp_get_term(link, fd);
217  return 1;
218}
219
220
221/**********************************************************************
222 * FUNCTION: imp_file2tree
223 * ARGUMENT: link - pointer to a stream structure
224 *           fname - If not empty, this is a string giving the name
225 *                   of the input file (as given from the command line). 
226 *                   If the string is empty, the routine asks for a
227 *                   filename.  If the filename cannot be opened for
228 *                   reading we print an error message and croak.
229 * RETURN:   None.
230 * PURPOSE:  Read successive trees from an input file and send them to
231 *           the data stream associated with link. 
232 *
233 * COMMENT:  Note that m_endofrecord() is called after writing each
234 *           tree to the buffer.  This flushes the buffer to the
235 *           network and sets the last fragment bit in the buffer
236 *           header.  The receiver must do a corrsponding m_skiprecord()
237 *           PRIOR to retrieving EACH tree from the data stream.
238 **********************************************************************/
239void 
240#ifdef _ANSI_
241imp_file2tree(MP_Link_pt link, char *fname)
242#else
243imp_file2tree(link, fname)
244MP_Link_pt link;
245char *fname;
246#endif
247{
248  FILE *fd;
249  int more = 0;
250
251  if (strlen(fname) == 0) {
252    fname = malloc(32);
253    printf("Enter name of input file: ");
254    scanf("%s", fname);
255    }
256
257  if ((fd = fopen(fname, "r")) == NULL) {
258    fprintf(stderr, "Can't open %s for reading\n", fname);
259    return;
260    }
261
262  do {
263    more = imp_get_term(link, fd);
264    MP_EndMsgReset(link);
265  } while (more);
266
267  fclose(fd);
268}
269
270int get_outfilename(fname)
271char *fname;
272{
273  int fd;
274
275  if ((fd = open(fname, O_WRONLY | O_CREAT, 0600)) < 0)
276    fprintf(stderr, "can't open %s for writing\n", fname);
277
278  return fd;
279}
280
281main(argc, argv)
282  int argc;
283  char *argv[];
284{
285  char *ifname = NULL;
286  MP_Link_pt link = NULL;
287  MP_Env_pt  env = NULL;
288
289  env = MP_InitializeEnv(NULL);
290  if (env == NULL) {
291    fprintf(stderr, "%s: MP_InitializeEnv() failed!\n", argv[0]);
292    exit(1);
293    }
294  fprintf(stderr,"%s: MP_InitializeEnv() succeeded\n", argv[0]);
295
296  if ((link = MP_OpenLink(env, argc, argv)) == NULL) {
297    fprintf(stderr, "%s: MP_OpenLink() failed!\n", argv[0]);
298    exit(1);
299    }
300  MP_SetLinkOption(link, MP_LINK_LOG_MASK_OPT, MP_LOG_INIT_EVENTS);
301  MP_SetLinkOption(link, MP_LINK_LOG_MASK_OPT, MP_LOG_WRITE_EVENTS);
302  MP_SetLinkOption(link, MP_LINK_SEND_MODE_OPT, MP_SEND_WHOLE_MSG);
303
304  mpf_set_default_prec(256);  /* for the GMP arb prec reals */
305
306  ifname = IMP_GetCmdlineArg(argc, argv, "-infile");
307
308  if (ifname == NULL)  {
309    MP_LogEvent(link, MP_ERROR_EVENT, "missing -infile argument");
310    exit(-1);
311    }
312  fprintf(stderr, "ifname = %s\n", ifname);
313
314  imp_file2tree(link, ifname); 
315
316  MP_CloseLink(link);
317  MP_ReleaseEnv(env);
318
319  fprintf(stderr, "%s: All done!\n", argv[0]);
320
321}
Note: See TracBrowser for help on using the repository browser.