How does GDB pretty printer know the output format the user is requesting? - gdb

I have a few complex classes generated by a third party domain-specific tool. The classes are complex because the tool tries to be generic as much as possible, so that I was asked to design a GDB pretty printer python script to improve the debugging experience.
I designed it by "to_string" APIs which can print the information with organized rich text (e.g. colors), so far everything works fine.
However, I don't know how to get the output format user requests, no matter that user uses "p" or "p/x" will all output the same string because my script does not know user wants hex rather than decimal.
I tried googling but didn't figure out an elegant approach. I indeed have a few workarounds but they all change the usages (for example, implement two printers to be switched), please give me some suggestions, thank you.

Just realized there had been a feature request to GDB already.
Bug 17291 - IWBN if the print format was available to pretty-printers.

Related

Smalltalk (Pharo) How to interface with the user simply

I stumbled upon Smalltalk and further downloaded Pharo, because it was recommended. My first impression is very positive. I wanted to port some simple programs I have written in C++ (my main language), so I can get a feel for Smalltalk.
However, I realized I can't find a simple way to just get some input from the user, e.g. I want to create a console calculator with a REPL loop, but I can't find a way to do this in Pharo. I don't want a gui with buttons, because that is just slow and inconvenient.
Could someone please point out to me how to do simple stuff in Smalltalk, like have the user enter 10 numbers or a command etc.? Do I have to use a different implementation? I'm aiming at std::cin/cout or python's print/input
I know Transcript show:, but that only covers the output portion.
Thanks in advance!
Getting input from the user is easy, but emulating a line-based console not so much. At least it would be more cumbersome than relying on the GUI, which is not really so slow and inconvenient after all.
The closest input equivalent to Transcript show: would be:
UIManager default request: 'Title of the request'
If you mark this snippet and "print it" or "inspect it", you will see a prompt dialog and can type something into it. When you accept the dialog with OK or return, you will get the entered String back as the return value of request:.
Getting ten numbers could be done like this:
(1 to: 10) collect: [:each | (UIManager default request: 'Enter number ', each asString) asNumber ]
The result will be an Array of the entered numbers.
Or, without using collect: and building up the collection yourself:
numbers := OrderedCollection new.
10 timesRepeat:
[ numbers add: (UIManager default request: 'Enter next number') asNumber ].
numbers do: [:each | Transcript show: each ].
You might also like UIManager default chooseFrom:. Browse the UIManager class and try out the various methods.
Note that Pharo's playground (called workspace in most other Smalltalks) already does what a REPL does, albeit it is not limited to line-based input. You can type Smalltalk expressions, evaluate them and have the results printed there. I am aware that this information does not give you the experience of porting your calculator app from C++. But I guess the way how the I/O works, or rather the interaction with the user, is not the essence of the programs you are trying to port, right? So you might as well write a class Calculator, in which you implement your calculator app, taking a String as input and returning the resulting number, and then invoke it from the playground by evaluating an expression like the following:
Calculator new calculate: '3 + 4'
If you really, really want to stick to console stuff and miss the benefits of the usual Smalltalk IDE and the experience of learning and using it, you might be better off with GNU Smalltalk. I think there are ways to create console applications with Pharo as well, but I would not recommend that to Smalltalk newcomers and I will leave that answer to someone who has already done it once.
You can use Pharo and get immediate feedback from the terminal like this:
➜ ./pharo Pharo.image eval "6+5"
11
Whatever you have between these quotes is being used as source by the compiler and the answered object will be printed to the stdout. In that example, it will be using SmallInteger>>asString behind the scenes.
For making a REPL, you don't need a lot. If you want to take a look, I've made REPLEndpoint using the Zinc HTTP server. In REPLEndpoint >> post you'll find the part where it uses the snippet you send to be evaluated:
snippet := '6+5'.
answer := OpalCompiler new evaluate: snippet.
answer
And answer will be the 11 SmallInteger instance you'd expect.
For additional hints on how your command line based REPL, take a look at the EvaluateCommandLineHandler class as it implements the basic step of what a REPL would do.

How to Show C++ Results in Excel

I am trying to create C++ code that allows User Input in selecting a variety of fields, then it will calculate many different angles and show the users the results, as well as a graph.
However, it has been suggested to us by our lecturer that it may be a good idea to write the code to these calculations etc in C++, then input the results into Excel.
Does anyone have any idea how to do this? Literally looking for a way for the user to fill in the required values on C++ and then to be AUTOMATICALLY taken to the excel file to show the results in the table and graph format.
If this is not possible, is there a way to display the results in the table and graph format through C++?
Thanks very much in advance
Excel provides COM interface which you can use from your C++ application.
This can be done in the way described in this article:
http://support.microsoft.com/kb/216686
This link might also be useful:
http://www.codeproject.com/Articles/10886/How-to-use-Managed-C-to-Automate-Excel
I think the second link would be better for you as its more of a step by step guide which should help you to workout the answer.
Use COM Automation to automate excel.
The best way to do this is to use the vole library by Matthew Wilson at
http://vole.sourceforge.net/
Take a look at the examples. I do not think there is an example for excel, but there is one for microsoft word at http://www.codeproject.com/KB/COM/VOLE_word.aspx
I have used vole in the past, and it makes it a whole lot easier

the best approaches for logging localization using c++

I am working on a multinational project where target audience for logs might be from two nationalities. Therefore it is becoming important to log in more than one language , I am thinking about writing to 2 different log folders based on language every time I am logging something, but I am also wondering if there's some out of the box functionality that is coming along with logging frameworks like log4cpp?
As other commenters have mentioned, it sounds like you are going down the wrong track by looking to do multilingual logging.
My recommendation would be to use English (which is the standard for technical information, and which I guess is the language you know best) and to make sure that the language you use is clear, grammatically correct and unambiguous. Then if one of the technicians cannot understand it, they can very easily and efficiently run it through a machine translation engine such as Google Translate. Or indeed they could process the logs and run everything through Google Translate to append translated text, particularly if you annotate the logs to mark the language content.
Assuming that the input language is well-written, machine transation usually gives a good result which the end user can understand. If the message isn't clear, has typos or abbreviations, then that's where machine translation fails spectacularly.
Writing log naturally brings down the speed of execution due to file open, seek and write operations involved as part of it.
This is one primary reason why many developer and architects suggest to write log at different levels.Increasing the depth of log entries as level increases to trace down the problems better. At higher level, you will notice that your process speed drops due to more log entries getting generated.
Rather suggest you to use services that can translate from one language to other.
I'm sure there are libraries free or paid which does this translation. You can create a small utility program that runs in the background and does this conversion during process idle time.
Well one suggestion is you can use a different process/thread which listens for your log messages, which you can log it from there ..
This reduces I/O logging time in your main process/thread and you can make all changes related to Logging language over there..
For multi - Lingual support I think you can try writing with widechar string .. though I am not sure..
the best approaches for logging localization using c++
Install Qt 4 and use QObject::tr/ tr() macro for strings. Write strings in whatever language you want. Hire/Get a translator to localize strings using QT Linguist.
Please note that perfect translation is impossible, so there will be many "amusing" misunderstandings, even if your translator is a genius. So it might be a better idea to select main language for programming team.
--EDIT--
Didn't notice this part before:
in more than one language
One way to approach it is to implement log reader. Instead of writing plaintext messages, you could dump message ids (generated by some kind of macros) and string arguments if strings are formatted. "Log reader" will allow user to select desired language while viewing log file, and translate messages based on their ids/arguments using mechanism similar to QTranslator. The good thing about this approach is that you'll be able to add more languages later - so it'll be possible to retranslate old logs. The bad thing is that this format will be harder to read for "normal human", although you can add plaintext messages in addition to message ids and arguments and you'll need to write log viewer.
Qt 4 has most of this framework implemented (there are routines for dumping variants into text/data streams, and so on) along with translation tool. See QTranslator documentation and Linguist manual for more info.

alternative for jasperreport in c++

from a c++ program i need to print a simple label. the label contains a text, an image and a barcode.(in my project the label is more complex, this is just for example)
my customer need a way to customize che label layout.
in the past in java I solve this problem using a report created with jasperreport. my customer customize the report with ireport and then i fill the data with an hashtable datasource (i never connect to an sql database)
anybody know a way to obtain something like this in java
really sorry for my scholastic english
Offhand, it's a bit hard to say -- most report generators assume some sort of database (SQL or at least accessible via ODBC) as the data source. I'd probably look into some that are free and include source code so you can change the data source (though I've no idea how difficult a modification that will be).
The other problem is that printing anything but plain text is somewhat non-portable; you'll need different code for Linux, Mac/OS or Windows. For Windows, one possibility would be Report Generator from CodeProject.com. If you want something more portable, you could use something like Xport to create XHTML output to be viewed in/printed from a browser (or any number of other programs that understand [X]HTML (there's also a commercial version). Of course, you could generate output in any number of other formats that support graphics, such as Postscript/PDF, LaTex, etc. This lets you use portable code to generate the report, but usually requires some non-portable code to invoke a viewer.

I want to show off my C++ projects through a website

The problem is that, well, it's C++. The way I've created them makes it such that they've always been run via a terminal/console window and wait for user input or else simply take a sample input and run with that. The output has also always been to the terminal screen or sometimes to a file. I'm not quite sure how I could take all of that and integrate it with a website while leaving the source code as it is, if that's at all possible. I guess what I'm trying to aim for is to have whatever website I use behave like a terminal window that will accept user input and then send it off to run the C++ program in question and return with the output (whatever it may be), all with minimal modification to the source code. Either that or else set up a more automated kind of page where a user can just click 'Go' and the program will run using a sample input.
When it comes to web I consider myself intermediate with HTML, CSS, PHP & MySQL, and a beginner with Javascript, so if this can be accomplished using those languages, that would be fantastic. If not, don't be afraid to show me something new though.
The easiest interaction model to bring to the web is an application that takes its input up front and produces its output on stdout. In this situation, as the unknown poster mentioned, you could use CGI. But due to the nature of CGI, this will only work (in the simplest sense) if all the information is collected from the user in one page, sent to the application and the results returned in one page. This is because each invocation of a page using CGI spawns a new indepdent process to serve the request. (There are other more efficient solutions now, such as FastCGI which keeps a pool of processes around.) If your application is interactive, in that it collects some information, presents some results, prints some options, collects some more user input, then produces more results, it will need to be adapted.
Here is about the simplest possible CGI program in C++:
#include <iostream>
int main(int argc, char* argv[])
{
std::cout << "Content-type: text/plain\n" << std::endl;
std::cout << "Hello, CGI World!" << std::endl;
}
All it does is return the content type followed by a blank line, then the actual content with the usual boring greeting.
To accept user input, you would write a form in HTML, and the POST target would be your application. It will be passed a string containing the parameters of the request, in the usual HTTP style:
foo.cgi?QTY=123&N=41&DESC=Simple+Junk
You would then need to parse the query string (which is passed to the program via the QUERY_STRING environment variable) to gather the input fields from the form to pass to your application. Beware, as parsing parameter strings is the source of a great number of security exploits. It would definitely be worthwhile finding a CGI library for C++ (a Google search reveals many) that does the parsing for you. The query data can be obtained with:
const char* data = getenv("QUERY_STRING");
So at a minimum, you would need to change your application to accept its input from a query string of name=value pairs. You don't even need to generate HTML if you don't want to; simply return the content type as text/plain to begin with. Then you can improve it later with HTML (and change the content type accordingly).
There are other more sophisticated solutions, including entire web frameworks such as Wt. But that would involve considerable changes to your apps, which you said you wished to avoid.
Almost off-topic, but you might want to take a look at Wt.
have you considered using cgi ... its 19th century technology which lets webserver execute programs written in C/C++ to run and generate output
I do not know much about it ... but I used it for some school projects
Show it all off with Screencasts. I use Camtasia Studio, but there are a ton of them out there: http://en.wikipedia.org/wiki/Screencast
Camtasia will even generate all of the HTML and Flash you need to upload to your web server. Buy a nice USB microphone, and write a script of what you're going to say and show.
What is the purpose of showing off your projects? Do you wish to impress your friends or employers?
It doesn't seem feasible to emulate or port your C++ console apps through a web interface.
I suppose you could write a bridge between a server side script and your C++ binary which passes the user input through to your app, then returns the result through the web interface. Bear in mind this would be a huge task for you to undertake.
Ruby have a compiler on their website which demonstrates this can be done.
However no one on the web would expect to run your C++ apps in a web browser. Also I think that anyone who is interested in running a C++ app would be totally comfortable with downloading a C++ binary that you made and running it (apart from the security risk) but when you think about it we download apps and run them all the time, whilst trusting the source.
I have a portfolio website which I created for the purpose of letting employers see my work. Take a look, it will give you an idea of another way you can do things.
Basically I provide the binaries for download, videos, screenshots and links. Things that the user can use to see my work quickly if they don't have time (or an appropriate computer) to run my projects on.
Good luck
I have no experience with this (other than hearing a guy on BART talk about implementing his server-side code all in C), but you might consider taking a look at SWIG (http://www.swig.org/). It allows you to wrap C++ so that you can access C++ code when using languages such as PHP.