HPCToolkit
StringSet.cpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //***************************************************************************
48 //
49 // File:
50 // StringSet.cpp
51 //
52 // Purpose:
53 // implement I/O for StringSet abstraction used by hpcprof/hpcprof-mpi
54 //
55 // Description:
56 // fmt_fread: read a string set from a file stream
57 // fmt_fwrite: write a string set to a file stream
58 //
59 //***************************************************************************
60 
61 //***************************************************************************
62 // system include files
63 //***************************************************************************
64 
65 #include <iostream>
66 
67 
68 
69 //***************************************************************************
70 // local include files
71 //***************************************************************************
72 
73 #include "StringSet.hpp"
74 #include <lib/prof-lean/hpcfmt.h>
75 
76 
77 
78 //***************************************************************************
79 // interface functions
80 //***************************************************************************
81 
82 int
84 (
85  StringSet* &stringSet, // result set read from stream
86  FILE* infs // input file stream
87 )
88 {
89  int result;
90 
91  stringSet = new StringSet;
92 
93  // ------------------------------------------------------------------
94  // read string set size
95  // ------------------------------------------------------------------
96  uint64_t size = 0;
97 
98  result = hpcfmt_int8_fread(&size, infs);
99  if (result != HPCFMT_OK) return result;
100 
101  // ------------------------------------------------------------------
102  // read strings in string set
103  // ------------------------------------------------------------------
104  for (size_t i = 0; i < size; i++) {
105  char *cstr = NULL;
106 
107  int result = hpcfmt_str_fread(&cstr, infs, malloc);
108  if (result != HPCFMT_OK) return result;
109 
110  std::string str = cstr;
111  free(cstr);
112 
113  stringSet->insert(str);
114  }
115 
116  return HPCFMT_OK;
117 }
118 
119 
120 int
122 (
123  const StringSet& stringSet, // input set to write to stream
124  FILE* outfs // output file stream
125 )
126 {
127  int result;
128 
129  // ------------------------------------------------------------------
130  // write string set size
131  // ------------------------------------------------------------------
132  uint64_t size = stringSet.size();
133 
134  result = hpcfmt_int8_fwrite(size, outfs);
135  if (result != HPCFMT_OK) return HPCFMT_ERR;
136 
137  // ------------------------------------------------------------------
138  // write strings in string set
139  // ------------------------------------------------------------------
140  for (auto s = stringSet.begin(); s != stringSet.end(); s++) {
141  result = hpcfmt_str_fwrite((*s).c_str(), outfs);
142  if (result != HPCFMT_OK) return HPCFMT_ERR;
143  }
144 
145  return HPCFMT_OK;
146 }
147 
148 
149 void
151 (
152  void
153 )
154 {
155  // ------------------------------------------------------------------
156  // dump strings in string set
157  // ------------------------------------------------------------------
158  std::cerr << "StringSet (" << this << ")" << std::endl;
159  for (auto s = begin(); s != end(); s++) {
160  std::cerr << "\t" << *s << std::endl;
161  }
162 
163 }
void MONITOR_EXT_WRAP_NAME() free(void *ptr)
static int fmt_fread(StringSet *&stringSet, FILE *infs)
Definition: StringSet.cpp:84
static int fmt_fwrite(const StringSet &stringSet, FILE *outfs)
Definition: StringSet.cpp:122
void dump(void)
Definition: StringSet.cpp:151
int hpcfmt_str_fwrite(const char *str, FILE *outfs)
Definition: hpcfmt.c:115
static int hpcfmt_int8_fread(uint64_t *val, FILE *infs)
Definition: hpcfmt.h:172
static int hpcfmt_int8_fwrite(uint64_t val, FILE *outfs)
Definition: hpcfmt.h:227
void *MONITOR_EXT_WRAP_NAME() malloc(size_t bytes)
#define NULL
Definition: ElfHelper.cpp:85
int hpcfmt_str_fread(char **str, FILE *infs, hpcfmt_alloc_fn alloc)
Definition: hpcfmt.c:87