Howto manage XML recipes with c++ and Qt? [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I want to write a software for managing recipes. The recipes are valid standalone xml files. They contain string and integer values and image data (in a base64 representation). The user should be able to create/open, edit and save recipes. Now I have concerns about file management and efficiency (speed and memory).
How should I manage all the recipes? I think I could either store them all as plain xml files in a folder, or have them all in one library file of some sort. The first approach might be faster on startup, as the recipes are loaded when the user clicks them, e.g. in a QTreeView. The second approach might be more efficient and fast because the whole library would be kept in memory, although it could be more complex to change its contents.
I want to use DOM for xml file handling - or would QXmlStreamReader/writer be a better choice here?
Either way, I would have to manage the recipe data using unique IDs, e.g. for several different "Cookies" recipes. When using the first approach from question 1., I would use file names based on such an ID. Are there any convenient ways to create unique IDs? Because when using incrementing integers I'd have to track which numbers are already in use.

I'm using XML files for transferring recipes in my recipe program. I have options to import and export using XML.
I am using a database to store the recipes. I do not use XML data files to store recipes. I'll leave data management to the database (I'm not wasting my time writing and debugging a database).
For data storage, there more efficient file formats than XML. Also, you may want to create "index tables" which allow for sorted and more efficient access to files than having to always parse an XML file.

Related

Create an invisible text file in C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Put simply, I have a browser widget I'm using in FLTK Gui toolkit which can be loaded using a file like so.
browser::load("textfile.txt");
The problem is I don't want to create a physical text file, just an invisible one so I can use it as an argument for browser::load above. I plan to use this invisible text file by loading it with the values I'm going to place in my browser....then use it like this.
browser::load("invisible_textfile.txt");
Is it possible to do this in C++?
I have already tried using ifstream::rdbuf() which probably has nothing to do with this. I'm not even sure what to call this so I'm just calling it an invisible textfile for now.
I'm using windows 7 64 bit. MinGW compiler.
Let's say what you want to add is equivalent of text file like this:
One
Two
Three
But you don't want to have the text file. So one piece of code which would do the same thing is this:
const char *lines[] = { "One", "Two", "Three", 0 };
for(int i = 0 ; lines[i] != 0 ; ++i)
browser.add(lines[i]);
Documentation link for that overload of add
Please refine your question, or perhaps ask a new question, if you want more help on how to get lines with your data.
It depends a lot on what browser::load() actually does internally, but let's assume that it will look for your filename and load it.
You probably already know how to read / write a standard file (e.g. http://www.cplusplus.com/doc/tutorial/files/). Now, if you just want to hide the file from the user, you can set OS specific hidden flags (e.g. windows). I'm assuming that's not what you actually want to accomplish here. (You could obviously also create a temporary file that you delete again, but it's not an elegant solution either.)
What you might want to use is a named pipe. Under Linux you can create these with mkfifo and then stream content through those file objects.
The general point is, though, unless the browser API allows you to pass it a complete string holding the text file or a stringstream, you will need a file object.
If your only target system is NTFS, there is a good answer on creating virtual files over here:
How to create a virtual file?
But in the end, you probably want to create an actual file (in your case probably a temporary one, though). I would recommend placing that file into the systems temporary path.

C++/MFC - How to export window contents to Excel? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Excel files and C++
I am using an API called UltimateGrid which allows me to easily create Excel-like grids using C++/MFC. I need a way to export all of the contents of these grids to an actual Excel document. I could not find anything about this in the documentation for this API. Then I got to thinking ... exporting contents to an Excel document is a common requirement in applications. I was thinking about creating a right-click menu option for exporting a grid to Excel. I feel like I am reinventing the wheel as I start to code this. I googled around and saw a lot of common ways of dealing with this when database tables are involved. But that is not the case here. My underlying "datasource" is not a database table, but rather just some business objects internal to the application. Am I going to have to just write my own custom handler that iterates through the business objects corresponding to the rows and output them to a CSV file to be used by Excel?
I'm not sure how automated you want this to be. I did this a while back (as in years ago), so forgive me if this info is out of date. Here's what I remember:
CSV is fine, as long as you don't want to export formats, graphs, etc...
You should be able to access the Excel object from your C++ code, and Microsoft should have documentation on how to do this.
It may be easier to have your C++ generate a VBScript to access the component, since last I checked, there was much less red tape to doing it that way, but you could run into permissions/security issues with this method.
Also, have you checked to see if UltimateGrid supports other export formats that are compatible with Excel?
If there are no other options, I think you might want to look into DDE (Dynamic Data Exchange).
There could be a limitation on that OS should be Microsoft Windows, however you might be able to find some open source/runtime that supports other OSs as well.
One that I used long ago was NDde.

How to extract plain text from MS word document file in pure C++? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
Improve this question
Is there any pure C++ library to extract plain text from a .doc file?
I'm developing a C++ program to read .doc and .pdf files. I have to extract plain text from the file and write it into a .txt file.
You could have a look at the open source C library used by Abiword, wv.
You can also call out to a batch convert tool
Open source batch converter, based on OpenOffice: http://dag.wieers.com/home-made/unoconv/
The open source for unix: http://www.wagner.pp.ru/~vitus/software/catdoc/
Proprietary for windows: http://doc2txt.com/. Note I havn't tried this one.
If you want to manipulate/read .doc files, you can just take the time and learn the format and manipulate the .doc file manually. You can get it at the MSDN page linking to the format-specification (PDF file).
I admit, it's quite a bit of reading to do, but if you're looking to create software to manipulate/read files, you should have the relevant underlying knowledge to back it all up.
Same goes for the pdf format (which is an open format, and as such specifications should be easy to find).
For doc - Use the Word object model to get to the the document and extract the text. This example uses OLE Automation and C . Another link for DOCX that might help you.
For PDF - Use Haru .
You could always use OIVT (OutsideIn Viewer Technology, I think) now owned by oracle.
I'll be honest, it's not a cheap solution, and while this product is to allow you view, print, etc... I think if i remember correctly, they do offer an option to extract the content to text or they another product that does that. it can do this from pretty much any document type including doc, docx, pdf (just to name a few) without having to use the "original" application installed as they have their own set of filters.
Here's a link to get you started
Outside In Viewer Technolog
Good luck

Turn .txt file into .pdf file on the fly? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 months ago.
Improve this question
I'm we're trying to figure out if there would be a way to convert a .txt file to a .pdf file. Here's the catch. This needs to be done behind the scenes, and on the fly. Meaning, with a radio control selected, OnOK would create a .txt file. Behind the scenes, at run time, we would like for the .txt file to be converted to a .pdf file. Ideally we would like this to be done by running an executable in the background. The executable would take input "File.txt" and output "File.pdf". We're using C++ and Visual Studio 6.
Does anyone have any experience on this? Is this possible?
libHaru may do what you want. Demo.
This a2pdf tool will probably do the trick with minimal effort. Just be sure to turn off perl syntax highlighting.
http://perl.jonallen.info/projects/a2pdf
I recommend using this open source library.
Once you have the base for generating PDF documents programmatically, you would still need a method for converting the text to the PDF elements, while keeping the text flow and word wrapping. This article may help. Please pay attention to the DoText(StreamReader sr) function. It takes text and purge it into separate lines within the PDF document, keeping the rendered within the margins.
On of the simpler methods that has worked for 3 decades e.g. more than one quarter of a century is place a postscript header before the text then use ghostscript ps2pdf it is the same method as used by some commercial apps such as acrobat
at its most basic
Copy heading.ps file.txt printfile.ps
GS -sDEVICE=pdfwrite printfile.ps printfile.pdf
Master Example can be seen here
How to modify this plaintext-to-PDF-converting PostScript from 1992 to actually specify a page size?

Should the text in a C++ text based game be in the code or in external files? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I am creating a text based game using C++ for a school project, the game works by allowing the user to pick a choice from a list of options in each scene; similar to how the games hosted by Choice of Games work. As a result of this I have a large amount of text that must be displayed in my game, however I am unsure as to the proper conventions when working with large amounts text in a program. Should I simply make use of std::cout and write the text directly into the code, or should I write into text files an used std::ifstream in order to read the text.
My only major concern regarding the use of files to hold the text is that each choice the user makes results in a different paragraph being displayed and as a result I believe that I would need to create a text file for each paragraph, which seems like it will lead to more issues (such as using the wrong file name or mistyping my code leading to the game reading from the wrong file) than writing the text straight into the code could. If there is a way to read particular sections of a text file then this would be useful to know, however I am currently unaware of any such method. However I am new to C++ and I am certain that there is plenty that I have yet to learn so I would not be surprised if such a method did exist.
Any help is greatly appreciated, be it anything from simply telling me if I should enter text into my code or into files, to telling me if there is a way to read text from specific sections of a text file. And once again, I am very grateful for any help you can provide.
Please don't put displayed text into code. That's an antipattern. You have to recompile your game for every minor text change like fixing typos, and for major changes like translating into other languages.
Convention for most programming languages is to put all the displayed text into (a few) resource files or properties files as key-value pairs, where the code only references the key of the paragraph to be displayed and the value will be loaded from that external file. (Usually once during startup.) No need to use one file per paragraph, but the kv pairs have to be parsed. There'll be utilities for you to reuse.
I recommend using external files. It makes changing the content much easier and doesn't require recompiling the entire program for a simple typo.
You can use one file and just separate each paragraph with a blank line. Grabbing "all text between blank lines" at that point is trivial.
If the choices cause the paragraph choices to jump around the file you can give them IDs and load them on-the-fly by searching linearly through the file for a given ID.
--EDIT--
As per the request here is an algorithm or two:
Algorithm 1:
Give each paragraph an ID, usually a simple number on the line immediately above the paragraph.
Separate each number-paragraph pair by blank lines.
Parse the file line-by-line looking for a "line" that contains only a number.
From that point you found the paragraph you are looking for, all lines until the next blank is the content of that paragraph.
Display to user.
Algorithm 2 (recommended):
Use XML to store your paragraphs and their IDs.
Use TinyXML2 to parse the file: http://www.grinninglizard.com/tinyxml2/index.html
If you do not plan to translate you game to other languages, you are on your own, both approaches have their pros and cons:
text in source: easy to write, text is near the place where it is used.
text in resource files: easier to remove duplicate strings, forces a better structure of text data.
If you simply imagine that your application could be translated, then you should put all text in ressource files. You can even find framework that will assist your for translations as Gnu gettext, but you can find others, for example qt has its own translation tools.
Storing text in the program files is not a good coding practice. This would result in unnecessary code bloat (it's not even code) and the need to recompile if you need to change the text.
A simple solution would be to create a text file with careful formatting like line numbers or whitespace that would allow you to pull out the desired text.
A more elegant solution would be to put the necessary text in xml or json files, and read them into your program when necessary. This would be a great choice.