summaryrefslogtreecommitdiff
path: root/TAO/examples/Simulator/DOVEMIB/PrintVisitor.cpp
blob: 652e5e239a3efe26df57a3eb4605704f2fba4aae (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
// $Id$
// ============================================================================
//
// = LIBRARY
//
// = FILENAME
//    any_test_i.cpp
//
// = AUTHOR
//    Michael Kircher 
//
// = DESCRIPTION
//   Visitor for the Nodes of the any analyser.
//
// ============================================================================

#include "PrintVisitor.h"
#include <stdio.h>


PrintVisitor::PrintVisitor (const char *file_name) {
  
  TAO_TRY {
    if (file_name != 0) {
      output_ =  ACE_OS::fopen(file_name, "w");
      TAO_CHECK_ENV;

       // print it on the screen if file could be opened
      if (output_ == 0) {

        output_ = stdout;

        ACE_DEBUG ((LM_DEBUG,"PrintVisitor: Use stdout for output.\n"));
      }

      // everything is ok
      ACE_DEBUG ((LM_DEBUG,"PrintVisitor: Opened file <%s> successfully.\n",
                  file_name));
    }
    else {
       output_ = stdout;
       ACE_DEBUG ((LM_DEBUG,"PrintVisitor: Use stdout for output.\n"));

    }

  }
  TAO_CATCHANY
    {
      ACE_ERROR ((LM_ERROR, "Failing when trying to open the output file.\n"));
    }
  TAO_ENDTRY;  
}


PrintVisitor::~PrintVisitor () {
  this->close ();
}

void 
PrintVisitor::close () {
  if (output_ != stdout && output_ != 0) {
    ACE_OS::fclose (output_);
  }
}


// Visit a struct node
void
PrintVisitor::visitStructNode (StructNode *structNode) {
  
  // print the padding in front of the line
  printPadding (structNode->getRecursionLevel());
  ACE_OS::fprintf (output_, "struct %s {\n", structNode->getName ());
  
  for (unsigned int i = 0; i < structNode->getChildNumber (); i++) {
    printPadding (structNode->getChild (i)->getRecursionLevel());
    structNode->getChild (i)->Accept((NodeVisitor *)this);
    ACE_OS::fprintf (output_, "\n");  
  }
  
  printPadding (structNode->getRecursionLevel());
  ACE_OS::fprintf (output_, "}\n");  
}

void 
PrintVisitor::visitDoubleNode (DoubleNode *doubleNode) {
  printPadding (doubleNode->getRecursionLevel());  
  ACE_OS::fprintf (output_, "CORBA::double %s = %f;", doubleNode->getName(), doubleNode->getValue());
}

void
PrintVisitor::visitLongNode (LongNode *longNode) {
  printPadding (longNode->getRecursionLevel());  
  ACE_OS::fprintf (output_, "CORBA::Long %s = %d;", longNode->getName(), longNode->getValue());
}

void
PrintVisitor::visitULongNode (ULongNode *uLongNode) { 
  printPadding (uLongNode->getRecursionLevel());  
  ACE_OS::fprintf (output_, "CORBA::ULong %s = %d;", uLongNode->getName(), uLongNode->getValue());
}

void
PrintVisitor::visitStringNode (StringNode *stringNode) { 
  printPadding (stringNode->getRecursionLevel());  
  ACE_OS::fprintf (output_, "CORBA::String %s = \"%s\";", stringNode->getName(), (char *)stringNode->getValue());
}

void
PrintVisitor::printPadding (unsigned int recursion_level) {
  switch (recursion_level) {
  case 0: break;
  case 1: ACE_OS::fprintf (output_, "   ");
    break;
  case 2: ACE_OS::fprintf (output_, "      ");
    break;
  case 3: ACE_OS::fprintf (output_, "         ");
    break;
  case 4: ACE_OS::fprintf (output_, "            ");
    break;
  default: for (unsigned int i = 0; i < recursion_level; i++) 
    {
      ACE_OS::fprintf (output_, "   ");
    }
  break;
  }
}