CONOPT
Loading...
Searching...
No Matches
std.cpp
Go to the documentation of this file.
1/* std.cpp
2 *
3 * Some standard methods for running the tutorials
4 */
5
6#include <iostream>
7#include <fstream>
8#include <sstream>
9#include <string>
10
11#include "conopticpp.h"
12
14{
15public:
16 Tut_MessageHandler(std::string name)
18 fs(name + ".sta"),
19 fd(name + ".lst")
20 {
21 }
22
24 {
25 // closing the file streams
26 fd.close();
27 fs.close();
28 }
29
31 virtual int message(
32 int smsg,
33 int dmsg,
34 int nmsg,
35 const std::vector<std::string>& msgv
36 )
37 {
38 // printing to stdout
39 for (int i = 0; i < smsg; i++)
40 {
41 // printing the message to stdout
42 std::cout << msgv[i] << std::endl;
43 }
44
45 // printing to the listing file
46 for (int i = 0; i < dmsg; i++)
47 {
48 // printing the message to stdout
49 fd << msgv[i] << std::endl;
50 }
51
52 // printing to the status file
53 for (int i = 0; i < smsg; i++)
54 {
55 // printing the message to stdout
56 fs << msgv[i] << std::endl;
57 }
58
59 return 0;
60 }
61
64 virtual int errorMessage(
65 int rowno,
66 int colno,
67 int posno,
68 const std::string& msg
69 )
70 {
71 // preparing the string for printing to the output
72 std::stringstream ss;
73 if ( rowno == -1 )
74 ss << "Variable " << colno << " : ";
75 else if ( colno == -1 )
76 ss << "Equation " << rowno << " : ";
77 else
78 ss << "Variable " << colno << " appearing in Equation " << rowno << " : ";
79 ss << msg;
80
81 // printing the error message to both the status and listing files
82 fd << ss.str() << std::endl;
83 fs << ss.str() << std::endl;
84
85 return 0;
86 }
87
88private:
89 std::ofstream fs;
90 std::ofstream fd;
91};
92
93
95std::string getProgramName(char* execname)
96{
97 std::string execstr(execname);
98 std::size_t lastslash = execstr.find_last_of("/\\");
99 std::string pname(execstr.substr(lastslash + 1));
100
101 // extracting the file name from the executable
102 std::size_t extpos = pname.find(".exe");
103 if (extpos != std::string::npos)
104 pname = pname.substr(0, extpos);
105
106 return pname;
107}
108
109
111void cpp_log( ConoptCpp& conopt, std::string msg, int code )
112{
113 std::ofstream fc;
114 fc.open(conopt.getName() + ".rc");
115 fc << conopt.getName() << ": " << msg << std::endl;
116 fc.close();
117
118 if ( code == 0 )
119 exit(0);
120 else
121 exit(code);
122}
The Conopt class.
Definition conopticpp.h:27
Tut_MessageHandler(std::string name)
Definition std.cpp:16
virtual int message(int smsg, int dmsg, int nmsg, const std::vector< std::string > &msgv)
Definition std.cpp:31
virtual int errorMessage(int rowno, int colno, int posno, const std::string &msg)
Definition std.cpp:64
char pname[MAXLINE]
Definition comdecl.h:10
FILE * fd
Definition comdecl.h:3
FILE * fs
Definition comdecl.h:2
CONOPT C++ interface header file. This is the main object for the CONOPT C++ interface.
ConoptMessageHandler()
Constructor.
COI_API std::string getName()
returns the model name
void cpp_log(ConoptCpp &conopt, std::string msg, int code)
Definition std.cpp:111
std::string getProgramName(char *execname)
Definition std.cpp:95