Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
fix a bunch of compilation warnings
[simgrid.git] / src / replay / replay_trace_reader.c
1 /* Copyright (c) 2010. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6 #include "gras_config.h"
7 #include <errno.h>
8 #include "replay.h"
9 #include "xbt/str.h"
10
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(replay);
12
13 typedef struct s_replay_trace_reader {
14   FILE *fp;
15   char *line;
16   size_t line_len;
17   char *position; /* stable storage */
18   char *filename; int linenum;
19 } s_replay_trace_reader_t;
20
21 replay_trace_reader_t replay_trace_reader_new(const char*filename) {
22   replay_trace_reader_t res = xbt_new0(s_replay_trace_reader_t,1);
23   res->fp = fopen(filename, "r");
24   xbt_assert2(res->fp != NULL, "Cannot open %s: %s", filename,
25       strerror(errno));
26   res->filename = xbt_strdup(filename);
27   return res;
28 }
29
30 const char *replay_trace_reader_position(replay_trace_reader_t reader) {
31   if (reader->position)
32     free(reader->position);
33   reader->position = bprintf("%s:%d",reader->filename,reader->linenum);
34   return (const char*)reader->position;
35 }
36 const char * const *replay_trace_reader_get(replay_trace_reader_t reader) {
37   ssize_t read;
38   read = getline(&reader->line, &reader->line_len, reader->fp);
39   //INFO1("got from trace: %s",reader->line);
40   reader->linenum++;
41   if (read==-1)
42     return NULL; /* end of file */
43   char *comment = strchr(reader->line, '#');
44   if (comment != NULL)
45     *comment = '\0';
46   xbt_str_trim(reader->line, NULL);
47   if (reader->line[0] == '\0')
48     return replay_trace_reader_get(reader); /* Get next line */
49
50   return xbt_dynar_to_array(xbt_str_split_quoted_in_place(reader->line));
51 }
52
53 void replay_trace_reader_free(replay_trace_reader_t *reader) {
54   free((*reader)->filename);
55   if ((*reader)->position)
56     free((*reader)->position);
57   fclose((*reader)->fp);
58   free((*reader)->line);
59   free(*reader);
60   *reader=NULL;
61 }