简体   繁体   English

从C ++样式打印转换为my_printf()

[英]Convert from C++ style printing to my_printf()

C++ purists may want to look away now. C ++纯粹主义者现在可能想要把目光移开。 You will hate this. 你会讨厌这个。

I have been given an open source windows console app that I am merging with a pre-existing, very old, very large windows app of my own. 我得到了一个开源的Windows控制台应用程序,我正在与我自己的预先存在的,非常古老的,非常大的Windows应用程序合并。 My old program started life as pure C though recently has been tweaked so that it can compile as C++. 我的旧程序以纯C开始,虽然最近已被调整,因此它可以编译为C ++。 My program makes extensive use of a my_printf() function which prints text to a window. 我的程序广泛使用my_printf()函数,该函数将文本打印到窗口。

The old console app does its printing C++ style via streams (I have never used this type of printing mechanism before). 旧的控制台应用程序通过流来打印C ++风格(之前我从未使用过这种类型的打印机制)。

When converting the console app to work under my system I could manually edit all the lines that do printing so that they use my_printf() instead. 将控制台应用程序转换为在我的系统下工作时,我可以手动编辑所有打印的行,以便他们使用my_printf()代替。 But before I embarked on that I thought I'd just check with StackOverflow to see if I was missing a trick. 但在我开始之前,我认为我只是检查StackOverflow,看看我是否错过了一个技巧。 For example I could imagine somehow letting the C++ prints be done via the stream and then somehow scooping the final text somewhere and then calling my_printf() with the result. 例如,我可以想象以某种方式让C ++打印通过流完成,然后以某种方式将最终文本舀到某处,然后用结果调用my_printf()。 Might that be possible? 可能有可能吗?

EDIT: please note my knowledge of C++ is extremely limited and I may need to look some things up in order to understand your answers so please use language that facilitates this. 编辑:请注意我对C ++的了解非常有限,我可能需要查看一些内容才能理解您的答案,所以请使用有助于此的语言。

There's indeed a trivial trick. 这确实是一个微不足道的伎俩。 But C++ impurists will hate the fact that C++ has a pure solution ;) 但是C ++的混合体会讨厌C ++有一个纯粹的解决方案;)

std::ostream is responsible for formatting, but not printing itself. std::ostream负责格式化,但不打印自身。 That's handled by std::streambuf . 这是由std::streambuf处理的。 std::cout combines a std::ostream formatter with a std::streambuf -derived object that writes to stdout. std::coutstd::ostream格式化程序与写入stdout的std::streambuf -derived对象组合在一起。

However, you can change the streambuf backing an ostream with ostream::rdbuf(newbuf) . 但是,您可以使用ostream::rdbuf(newbuf)更改支持ostreamstreambuf As std::cout is just another ostream , you can replace its streambuf too. 由于std::cout只是另一个ostream ,你也可以替换它的streambuf In this case, you only need to come up with a streambuf -derived class that writes already-formatted output to my_printf() . 在这种情况下,您只需要提供一个streambuf -derived类,它将已经格式化的输出写入my_printf() That should be quite trivial. 这应该是微不足道的。

You might find string streams useful. 您可能会发现字符串流很有用。 For example: 例如:

std::ostringstream os;
os << "Print " << whatever << data;
my_printf( "%s", os.str().c_str() );

In case you were feeling adventurous, you could write your own streambuf instead that used my_printf underneath, and inject it into the stream object that is currently used in output statements (eg std::cout). 如果您喜欢冒险,可以编写自己的streambuf,而不是在下面使用my_printf,并将其注入当前在输出语句中使用的流对象(例如std :: cout)。 Be warned that this latter approach might not be trivial, however it would result in almost no changes to existing codebase. 请注意,后一种方法可能并不简单,但是它会导致现有代码库几乎没有变化。

Subclass std::ostream and make operator << call my_print_f() . 子类std::ostream和make operator << call my_print_f() You can use internal stringstream in your stream to retrieve string representation of operator << arguments. 您可以在流中使用内部stringstream来检索operator << arguments的字符串表示形式。

Then you'd just have to do find&replace, replacing cout (or whatever stream you directed your output to in the console app) with an instance of your stream class. 然后你只需要查找和替换,用你的流类的实例替换cout (或你在控制台应用程序中引导输出的任何流)。

Something along these lines might be helpful: 这些内容可能会有所帮助:

http://www.codeproject.com/KB/debug/debugout.aspx http://www.codeproject.com/KB/debug/debugout.aspx

Should be obvious where the meat of it is, so you can make it print via your own systems, or what have you. 应该很明显它的肉,所以你可以通过自己的系统,或你有什么打印。 In theory, you'd need only search for references to std::cout and replace them with references to your own object. 理论上,您只需要搜索对std::cout引用,并用对自己对象的引用替换它们。

Overloading the global operator<< is probably what will solve your problem: 重载全局operator<<可能会解决您的问题:

#include <iostream>
#include <cstdio>

static int
my_printf (const char* s)
{
  printf ("my_printf: %s\n", s);
}

namespace std {
    std::ostream& operator<< (std::ostream& out, const char* s)
    {
      my_printf (s);
      return out;
    }
}

int
main ()
{
  std::cout << "hello, world"; // => my_printf: hello, world
  return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM