summaryrefslogtreecommitdiff
path: root/TAO/local/bin/Event_Service/GPlot_File.cpp
blob: 2f985e5d47c384f88c58d8aab242afc0469253fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// $Id$
//
// ============================================================================
//
// = FILENAME
//    GPlot_File.cpp
//
// = AUTHOR
//    Tim Harrison
// 
// ============================================================================

#include "GPlot_File.h"

#if !defined (__ACE_INLINE__)
#include "GPlot_File.i"
#endif /* __ACE_INLINE__ */

int
ACE_GPlot_File::open (const char *filename)
{
  ACE_OS::strcpy (filename_, filename);

  FILE *read_file = ACE_OS::fopen (filename_, "r");

  long entry;
  char *value;
  ACE_NEW_RETURN (value, char [32], -1);

  if (read_file > 0)
    {
      int result;
      do
	{
	  result = fscanf (read_file,
			   "%ld\t%s\n",
			   &entry,
			   value);

	  //if ((result == -1) && (ACE_OS::last_error () != 0))
	  //ACE_ERROR_RETURN ((LM_ERROR, "%p.\n", "ACE_GPlot_File::open"), -1);

	  if (result > 0)
	    // Success.
	    map_.bind (entry, value);
	  else if (result != EOF)
	    // Error.
	    {
	      ACE_OS::fclose (read_file);
	      ACE_ERROR_RETURN 
		((LM_ERROR, "Error reading GPlot file %s.\n", filename_), -1);
	    }
	} while (result != EOF);

      fclose (read_file);
    }

  write_file_ = ACE_OS::fopen (filename_, "w");
  if (write_file_ == 0)
    ACE_ERROR_RETURN ((LM_ERROR, "%p: can't open\n", filename_), -1);

  closed_ = 0;

  return 0;
}


void
ACE_GPlot_File::close (void)
{
  if (closed_ == 0)
    {
      closed_ = 1;
      GPLOT_ITERATOR iterator ((GPLOT_MAP &) map_);
      for (GPLOT_ENTRY *entry; iterator.next (entry); iterator.advance ())
        {
	  ACE_OS::fprintf (write_file_, "%ld\t%s\n",
			   entry->ext_id_, entry->int_id_);
          delete [] entry->int_id_;
        }

      ACE_OS::fclose (write_file_);
    }
}


void
ACE_GPlot_File::dump (void)
{
  GPLOT_ITERATOR iterator ((GPLOT_MAP &) map_);
  for (GPLOT_ENTRY *entry; iterator.next (entry); iterator.advance ())
    {
      if (entry->int_id_ != 0)
	ACE_DEBUG ((LM_DEBUG, "%d\t%s\n", entry->ext_id_, entry->int_id_));
      else
	{
	  ACE_ERROR ((LM_ERROR, "Value for entry %d is null.\n",
		      entry->ext_id_));
	  return;
	}
    }
}


int
ACE_GPlot_File::get (long entry, long &value)
{
  char *val;

  if (map_.find (entry, val) == -1)
    return -1;
  else
    {
      ::sscanf (val, "%ld", &value);
      return 0;
    }
}


int
ACE_GPlot_File::get (long entry, float &value)
{
  char *val;

  if (map_.find (entry, val) == -1)
    return -1;
  else
    {
      ::sscanf (val, "%f", &value);
      return 0;
    }
}


void
ACE_GPlot_File::set (long entry, long value)
{
  long old_entry;
  char *val;
  char *old_value;

  ACE_NEW (val, char [32]);

  ::sprintf (val, "%ld", value);
  map_.rebind (entry, val, old_entry, old_value);

  delete [] old_value;
}


void
ACE_GPlot_File::set (long entry, float value)
{
  long old_entry;
  char *val;
  char *old_value;

  ACE_NEW (val, char [32]);

  ::sprintf (val, "%f", value);
  map_.rebind (entry, val, old_entry, old_value);

  delete [] old_value;
}


#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Map_Entry<long, char *>;
template class ACE_Map_Iterator<long, char *, ACE_Null_Mutex>;
template class ACE_Map_Manager<long, char *, ACE_Null_Mutex>;
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */