File: CbcGenMessages.cpp

package info (click to toggle)
coinor-cbc 2.9.9+repack1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,848 kB
  • ctags: 5,787
  • sloc: cpp: 104,337; sh: 8,921; xml: 2,950; makefile: 520; ansic: 491; awk: 197
file content (198 lines) | stat: -rw-r--r-- 5,537 bytes parent folder | download | duplicates (3)
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*! \legal
  Copyright (C) 2007
  Lou Hafer, International Business Machines Corporation and others. All
  Rights Reserved.

  This code is licensed under the terms of the Eclipse Public License (EPL).

  $Id: CbcGenMessages.cpp 1899 2013-04-09 18:12:08Z stefan $
*/
/*
  This file is part of cbc-generic.
*/

#include "CbcGenMessages.hpp"

#include "CbcGenCtlBlk.hpp"

namespace {

char svnid[] = "$Id: CbcGenMessages.cpp 1899 2013-04-09 18:12:08Z stefan $" ;

}

/*
  Begin file local namespace
*/
namespace {

/*
  Message definitions.

  The precise form isn't important here, so long as the method that loads them
  into a CoinMessages object can come up with values for external ID,
  detail level, and format string. The use of an enum to provide an internal ID
  for each message is mainly useful with the internationalisation feature. It
  makes it easy to slap the same ID on alternate versions of a message.
*/


typedef struct {
    CbcGenMsgCode inID ;
    int exID ;
    int lvl ;
    const char *fmt ;
} MsgDefn ;

static MsgDefn us_en_defns[] = {
    // informational (0 -- 2999)
    { CBCGEN_TEST_MSG, 1, 2, "This is the us_en test message, eh." },
    { CBCGEN_NEW_SOLVER, 2, 2, "Solver is now \"%s\"." },
    // warning (3000 -- 5999)
    // Non-fatal errors (6000 -- 8999)
    // Fatal errors (9000 and up)
    { CBCGEN_CONFUSION, 9001, 1, "Internal confusion, line %d." },
    { CBCGEN_DUMMY_END, 999999, 0, "" }
} ;

/*
  We seem to need a dummy CoinMessages object to prevent the compiler from
  complaining that CoinMessages::Language is unintialised.

  const CoinMessages dummy(0) ;
*/
/*
  The author is Canadian, eh. But we'll go with us_en anyways.
*/
const CoinMessages::Language default_language = CoinMessages::us_en ;

} /* End file local namespace */

/*!
  This function constructs a CoinMessages object filled with a default set of
  messages, overlaid with whatever is available for the specified language.
  It is used to establish the initial set of messages, and is also called
  whenever the language is changed. The latter, because there's no way of
  guaranteeing that the message sets for alternate languages will all
  replace the same messages. This approach guarantees that the set of
  messages is always composed of the default language overlaid with any
  messages for an alternate language.

  The default for lang is us_en, specified up in CbcGenCtlBlk.hpp. If you want
  to change the default language, change the declaration there. That said,
  you'll also have to provide the necessary message definitions and augment the
  case statements below.
*/

void CbcGenCtlBlk::setMessages (CoinMessages::Language lang)

{
    /*
      If messages exist, in the correct language, we have nothing more to do.
    */
    if (msgs_ && cur_lang_ == lang) {
        return ;
    }
    /*
      Otherwise, we need to do a wholesale rebuild. Create a new object of the
      appropriate size.
    */
    CoinMessages *msgs = new CoinMessages(sizeof(us_en_defns) / sizeof(MsgDefn)) ;

    msgs->setLanguage(lang) ;
    strcpy(msgs->source_, "CbcG");
    /*
      Yes, this is gloriously redundant, but it's set up in anticipation of
      future extensions.
    */
    MsgDefn *msgdefn ;
    switch (lang) {
    case CoinMessages::us_en: {
        msgdefn = us_en_defns ;
        break ;
    }
    default: {
        msgdefn = us_en_defns ;
        break ;
    }
    }
    /*
      Open a loop to create and load the messages.
    */
    while (msgdefn->inID != CBCGEN_DUMMY_END) {
        CoinOneMessage msg(msgdefn->exID, msgdefn->lvl, msgdefn->fmt) ;
        msgs->addMessage(msgdefn->inID, msg) ;
        msgdefn++ ;
    }
    /*
      Now, if the local language differs from the default language, load any
      overrides. Again, useless now, but maybe in the future ...
    */
    if (lang != cur_lang_) {
        switch (lang) {
        case CoinMessages::us_en: {
            msgdefn = us_en_defns ;
            break;
        }
        default: {
            msgdefn = us_en_defns ;
            break;
        }
        }

        while (msgdefn->inID != CBCGEN_DUMMY_END) {
            msgs->replaceMessage(msgdefn->inID, msgdefn->fmt) ;
            msgdefn++ ;
        }
    }
    /*
      Each CoinOneMessage has a fixed-length array to hold the message; by default
      this is 400 chars. Convert to `compressed' CoinOneMessage objects where the
      array is only as large as necessary. Any attempt to replace a message, or the
      message text, will automatically trigger a decompress operation before doing
      the replacement, but the messages will *not* be automatically recompressed.
    */
    msgs->toCompact() ;
    msgs_ = msgs ;

    return ;
}


/*
  Replaces the current message handler with the handler supplied as a
  parameter. If ourMsgHandler_ is true, the existing handler is destroyed.
*/

void CbcGenCtlBlk::passInMessageHandler (CoinMessageHandler *newMsgHandler)

{
    if (msgHandler_ && ourMsgHandler_) {
        delete msgHandler_ ;
    }

    msgHandler_ = newMsgHandler ;
    ourMsgHandler_ = false ;

    return ;
}

/*
  Start a message. This routine buries the whole business of locating the
  message handler and messages, getting the log level right, etc. If, by some
  chance, messages are not yet loaded, do so.
*/

CoinMessageHandler &CbcGenCtlBlk::message (CbcGenMsgCode inID)

{
    if (!msgs_) {
        setMessages() ;
    }
    msgHandler_->setLogLevel(logLvl_) ;

    msgHandler_->message(inID, *msgs_) ;

    return (*msgHandler_) ;
}