I'm working on a widget, for reordering menus.
For this purpose I have added few buttons like MoveUp, MoveDown, Add, Delete which are supposed to change the order of menus in the XML file, as XML file is being read for populating menus.
Now my question is that I have applied file open, write and close operation for every click of these buttons, which instantly changes into xml file. It is relatively easy way for me. Given that, the widget has comboBox too, which read from xml and then populate current order of inner level submenus in the widget. ( same like : MS outlook->tools->customize->RearrangeCommands)
My XML file is a small one around 1.5K.
I want to know which option would be better?
i) The one I mentioned above, opening-reading-writing XML files for every mouse click.
ii) Creating a data structure which once reads the file, stores the data and all activity happens with this data and then finally OK buttons write the data into actual XML file.
I personally like option (i) which is very easy to implement overall.
And if (ii) one is better option then what data structure should I use?
XML file format is like :
<menu>
<action id="1">menu1</action>
<action id="9">menu2</action>
<submenus id="5" name="submenus 1">
<action id="17">menu14</action>
</submenus>
<action id = "10">menu1</action>
<submenus id="7" name="submenus 1">
<action id="3">menu14</action>
</submenus>
<action id="11">menu2</action>
</menu>
Trade-off between frequently reading/writing a small file and once reading, creating a data structure to store data and finally writing only once, which one is better option?
Note : I'm using QT DOM with c++ for all this purpose.
I know this is not c# but I am not too fond with c++, so if someone could fill in my blanks, that would be awesome
In C# you can just load your Xml into an XDocument object and do the manipulations in memory, aka instant.
When done you save them to file.
This has the disadvantage that when you crash, your changes are gone.
To migitate this you can save in a spefified intervall.
Advantage is that you can get rid of your frequent file accesses.
The question IF there is a performance penalty depends on the way you do the IO operations:
If you do them async you don't loose much time even if your OS is a bit occupied, sync strongly depends on your OS' performance, in particular the business of the file system.
Also too frequent file operations wear off the storage device. I know we're probably not talking about EEPROM here with ~10k IO-cycles, but still.
Can someone let me know wether there is a namespaces/library for c++ that provides similar functionality?
I think writing configuration directly into a file is bad practice. Imagine that you want to get the order of menus in some other code. For example, if "File" menu is hidden or moved, you want to display a message box in some moments.
Now you need to open the XML and parse it each time you want to get this information. You need to duplicate code needed for parsing XML and getting menus order from it.
If you had a structure containing your settings, you could just store it somewhere in a variable and use it any time you want. No duplication, more clean code, easier to write unit tests.
Generally, I know from my experience that creating a structure for settings is good. This approach looks harder at the start, but most likely will save you time in the future.
Related
I have a C++ application which has written for years. There are a lot of classes.
Every time we want to change some parameter values, we need to manually update them in the code and recompile it. It is not convenient as the growing demands of the users. We would like the values of the classes to be configured out side of the application. Probably, reading form an xml is the best? for each user, we can make an xml configuration setting and send it together with the application. Is it a good approach?
For every class e.g: Class classA, should we create another class called: ConfigClassA then classA will use the configuration setting from ConfigClassA? We dont want to make a lot of changes in the current implementation.
Suppose there is a structure of an xml file:
<classes>
<class name="ClassA">
<variable id="isUp" value="true" />
</class>
</classes>
In Xml, we can get the portion classA and parse it to ConfigClassA then classA has an instance of ConfigClass.
Or anybody has a better approach?
Thanks in advance.
In general I think that the whole configuration should be loaded when an application is launched, so you can immediately notify the user in case of an invalid configuration. In order to validate an XML file, you can use XML Schema.
However, if you don't want to make a lot of changes in the current implementation, your idea could be a valid solution.
Using JSON or YAML will be more lightweight than XML, since the parser of the config file will be simpler. Anyway, XML is also feasible.
It's actually quite common to have configuration files. The format in which they are stored is not important. They are usually loaded only once, at the beginning of the program, not queried each time a method requests something.
Also, you should make a tool available for editing these files (such as a "Preferences..." panel).
I'm working on a project that requires me to load some of the data from an XML file on to a GUI. The GUI allows the user to make some changes to the data. What I want to be able to do is to save these changes back onto the XML file.
I know it is possible to rewrite the whole file but the file is pretty huge, and not all the data in the file is being changed or even being used in my program.
This is my first project working with TinyXML and C++ Builder. I am just looking for some suggestions as to how I should approach this.
Unless you are certain that the new text will be exactly the same size as the old, rewriting only part of a text file is not a good idea in general. There are file formats where piecemeal replacement is possible. XML is not one of them. Not in the general case, at least.
Inserting data in the middle of a file, thus moving the rest down, is basically equivalent to loading the rest of the file, making the file bigger, and writing it back. So you may as well just load the entire file, make your modifications, and save it again. Your code will be simpler and likely not much slower.
And no, a SAX parser isn't going to help you here. It allows you to stream reading (though I would suggest a pull parser rather than a push one), but that's not going to allow you to insert data into the file. That's generally not supported by most XML parsers I know of. They can write data, but writing and non-destructively inserting are two different things.
TinyXml will let you do what you want without damaging the file contents (as long as its valid xml). I just checked this so I am quite certain. Obviously you have to know and precisely what attributes and tags you want to edit, but you can add/edit tags without affecting existing attributes/tags/comments even within the tags you edit. It will take a while until you get used to the structure, but it is definitely possible.
You have to know the structure of the xml!
TiXmlDocument doc("filepath"); //will open your document
if (!doc.LoadFile()) //you do have to open the whole file
{
cout<<"No XML structure found"<<endl;
return; // exit function don't load anything
}
TiXmlElement *root = doc.RootElement(); //pointer to root element
Now you can use this pointer and commands like:
root->FirstChild("tageone")->ToElement();
tageone->SetDoubleAttribute("attribute", value);
to change stuff.
Sorry for the rushed explanation, but you'll need to read through the documentation a bit to get the hang of it.
cheers
Update
As I said in the comment, I don't think that you are better off if you insert into the middle of a file. However, if you need/want additional security I suggest two additional steps:
perform a sanity check of the xml file at all the important steps. This can be anything where you make sure that the file you are reading is really what you need.
calculate a checksum over the content of the whole file before saving and check it afterwards. This does not necessarily need to be a CRC, I just named the function calculate_crc(). Anything that lets you verify the integrity of the data is good.
I would do this approximately as follows (pseudocode):
TiXmlDocument doc( "demo.xml" );
doc.LoadFile();
perform_sanitycheck(doc);
// do whatever you need to change
perform_sanitycheck(doc);
unsigned int crc = calculate_crc(doc);
doc.SaveFile("temp_name.xml"); // save the file under another name
TiXmlDocument doc2( "temp_name.xml" );
perform_sanitycheck(doc2);
if(verify_crc(doc, crc))
{
delete_file("demo.xml");
rename_file("temp_name.xml", "demo.xml");
}
The sanity check would take the appropriate action if necessary. You need to substitute the two function delete_file() and rename_file() with an API or library function for your environment.
The functions calculate_crc() and verify_crc() could be specifically crafted to check only the parts that you need to have unchanged.
I've had a thorough look at the tinyxml (C++) tutorial but still can't really understand how I apply the examples to what I'm trying to do. What I'm trying to do in short is use an xml to generate a series of room objects in a game. Is someone able to give me a short example with the following xml and Room object, please? Xml is:
<room>
<name>Prison room</name>
<connections>
<connection>Guard room</connection>
</connections>
<items>
<item>
<name>Short sword</name>
<attack>2</attack>
<armor>0</armor>
</item>
</items>
<monsters></monsters>
</room>
Room object has the following fields:
std::vector<Item> itemsInRoom;
std::vector<Room> connectingRooms;
std::vector<Monster> monstersInroom;
std::string roomName;
Thanks in advance!
Edit: Removed the edit as that particular problem was solved.
The first thing to do would be to learn more about XML and about representing/structuring/abstracting data. For example, it is usually unwise to encode e.g. the item "short sword" inside the room as you do. Rather you would want to provide a definition of that item (or a template of it) somewhere else and only have a reference to that, possibly with some extra parameters inside the room node. You will probably also want to learn to use attributes (all data is not the same, some data should be attributes).
Once you have groked that, the actual TinyXML stuff is easy. TinyXML is about as simple as it can get:
Agree on some semantics. Write them down, remember them, follow them when you create the XML files.
Create a TiXmlDocument, give it the name of your data file
Call LoadFile on your document object
Call FirstChildElement, giving you the root node (note that if you have more than one room in the XML you need to have a separate root node!)
Iterate over the cildren using FirstChildElement and NextSiblingElement.
Now you have to remember the structure of your XML file (or the semantics of its elements). TinyXML cannot magically figure that out for you.
Use FirstChildElement and NextSiblingElement in the same manner as for the room nodes for "anything inside" each room (whatever you decided that may be) to figure out what each room looks like and what's in them. You must know what this data means, TinyXML cannot know this kind of thing, it merely provides you with structured data.
Resolve references and set up the corresponding data structures (e.g. when you have something like <door to="guard_room" x="5" y="3" status="locked" /> create the necessary links so your game reacts appropriately.
(and don't forget to check for errors)
The tutorials at the TinyXML site are also very easy to understand (last I looked some 2-3 years ago, you could basically copy-paste them). If these really pose a considerable problem, I'd reconsider the idea of writing a RPG for the time being. I'm not saying forever, but at least until you have enough experience to follow these.
Are there a set of commands used to perform CRUD on an XML file?
I'm envisioning a component (myData.cfc) that has a Save function (Insert/Update), a Delete function and a Read function.
Are you asking about a way to use a XML file to store data? While you certainly can do that, I'm not sure I'd recommend it. You would have to be extremely careful to single thread the updates and you would not get good performance out of it. I mean it would probably work fine for a few hundred rows, but after that your going to suffer.
Can you elaborate on how you want to use the XML file? If you just mean settings, or a static set of data, I think it's fine. If you mean CRUD as in "N rows of data", you really want a database.
I'm creating a linux program in C++ for a portable device in order to render html files.
The problem is that the device is limited in RAM, thus making it impossible to open big files (with actual software).
One solution is to dynamically load/unload parts of the file, but I'm not sure how to implement that.
The ability of scrolling is a must, with a smooth experience if possible
I would like to hear from you what is the best approach for such situation ?
You can suggest an algorithm, an open-source project to take a look at, or a library that support what I'm trying to do (webkit?).
EDIT:
I'm writing an ebook reader, so I just need pure html rendering, no javascript, no CSS, ...
To be able to browse a tree document (like HTML) without fully loading, you'll have to make a few assumptions - like the document being an actual tree. So, don't bother checking close tags. Close tags are designed for human consumption anyway, computers would be happy with <> too.
The first step is to assume that the first part of your document is represented by the first part of your document. That sounds like a tautology, but with "modern" HTML and certainly JS this is technically no longer true. Still, if any line of HTML can affect any pixel, you simply cannot partially load a page.
So, if there's a simple relation between position the the HTML file and pages on screen, the next step is to define the parse state at the end of each page. This will then include a single file offset, probably (but not necessarily) at the end of a paragraph. Also part of this state is a stack of open tags.
To make paging easier, it's smart to keep this "page boundary" state for each page you've encountered so far. This makes paging back easy.
Now, when rendering a new page, the previous page boundary state will give you the initial rendering state. You simply read HTML and render it element by element until you overflow a single page. You then backtrack a bit and determine the new page boundary state.
Smooth scrolling is basically a matter of rendering two adjacent pages and showing x% of the first and 100-x% of the second. Once you've implemented this bit, it may become smart to finish a paragraph when rendering each page. This will give you slightly different page lengths, but you don't have to deal with broken paragraphs, and that in turn makes your page boundary state a bit smaller.
Dillo is the lightest weight Linux web browser that I'm aware of.
Edit: If it (or its rendering component) won't meet your needs, then you might find Wikipedia's list of and comparison of layout engines to be helpful.
Edit 2: I suspect that dynamically loading and unloading parts of an HTML file would be tricky; for example, how would you know that a randomly chosen chunk of the file isn't in the middle of a tag? You'd probably have to use something like SAX to parse the file into an intermediate representation, saving discrete chunks of the intermediate representation to persistent storage so that they won't take up too much RAM. Or you could parse the file with SAX to show whatever fits in RAM at once then re-parse it whenever the user scrolls too far. (Stylesheets and Javascript would ruin this approach; some plain HTML might too.) If it were me, I'd try to find a simple markup language or some kind of rich text viewer rather than going to all of that difficulty.