![]() |
|
#1
|
|||
|
|||
Storing long string constants inside a programHi (first post
),I am working on a project where I have to store really big string constants (JavaScript snippets) inside my application. At the moment this looks very ugly as one could imagine. Is there a way I can create plain-text and put that into strings preprocessor wise (or the like)? In the end there should be a single executable (well dll actually (but that shouldn't matter)) so putting this strings inside some text-file or database that loads runtime is not possible. As I can only think that this problem should occur more often I am kind of hesitant to ask but my gOogle-Skilz have proven to be useless in this matter and searching the forum didn't yield anything either. So if someone has an idea how to do this or some search-words or that kind of information I would be happy to get advice. By the way; I have to use Visual Studio 2005 for this. Are resource files the way to go? Hmm... Thanks for your time. PS: The smiley in this post is for the sole purpose of enhancing the post's cuteness I am thus in breach with "guideline 10" of "Posting Requests for Help" I accept severe punishment. |
|||
|
#2
|
|||
|
|||
Re: storing long string constants inside a programQuote:
What's wrong with something like the following? It doesn't use any Windows-specific or Visual Studio-specific program constructs. It's standard C++ (works for standard C also). So, unless you need to make it easy to incorporate different strings for different languages, you don't need anything fancy. CPP / C++ / C Code:
Regards, Dave Footnote: Here is one of those web pages that you don't even have to open in your browser. The URL says it all: ugly-is-as-ugly-does on screaminblackmonkeyspit |
|
#3
|
|||
|
|||
Re: storing long string constants inside a programHello Dave,
Quote:
I usually compile with g++ and cross-platform is very important for me but this project will stay "very most likely" on Windows. (of course I'd prefer a portable solution) Here is what I do at the moment (does not much differ from yours): CPP / C++ / C Code:
The problem is, that I will get really long strings and many of them. Parts of these strings will surely change so I have to figure out some sort of sorting and categorizing scheme to keep my sanity. If I don't find an other way I will write myself something to produce this code from the original text (escaping quotation marks and the like one the way too). Doing this by hand would drive me mad So, any other ideas? |
|
#4
|
|||
|
|||
Re: Storing long string constants inside a programI think that the image is a well done illustration of the positive aspects of a despicable person of your choosing. *me having Mitläufer-mentality*
|
|
#5
|
|||
|
|||
Re: storing long string constants inside a programQuote:
I don't know exactly what you want to do, so I'll do something that is interesting to me. Maybe it will help you; maybe not... I would write a program that would read lines of text and create a program that would consist of a function that returns a string consisting of those lines of text. (Kind of like being a resource compiler.) Suppose I have the following text file: Code:
To make a string literal of these, I would need to use backslash characters to escape the quote marks, and I would probably put a '\n' character at the end of the lines inside the string. So the output file might look like this: CPP / C++ / C Code:
Here's a simple program that processes lines of text like this and creates the C++ function file as its output: CPP / C++ / C Code:
Of course, you might need something more elaborate than simply escaping the quote characters, but I hope you get the idea. Here's the actual output (I didn't bother trying to indent it properly, as is typical for machine-generated program files): CPP / C++ / C Code:
Now, here's a test program for using the function defined in lines.cpp: CPP / C++ / C Code:
And, here's "lines.h" CPP / C++ / C Code:
And here's the output of the test program when I compiled it together with lines.cpp: Code:
Here's a Makefile for GNU make and gcc on my cygwin Windows XP platform, so that everything is generated automatically. (Also used on my Linux system by following the comments about $(EXEEXT): Code:
When I did "make clean" and then "make", here's what I saw: Code:
This created lines.cpp and test.exe Regards, Dave |
|
#6
|
|||
|
|||
Re: Storing long string constants inside a programThank you for your great work Dave,
Quote:
You did exactly what I had in mind (except that I'll maybe write it in ruby). I am still curious if there is a well accepted way of getting a long text into a string constant without putting the text altogether to the rest of the source-code... I really appreciate extensive reply. Regards, Rob |
|
#7
|
|||
|
|||
Re: Storing long string constants inside a programQuote:
There are about a million ways (maybe more). I don't know whether any of which of them is/are well-accepted. I personally accept things that I like. I like most things that work and are easily maintainable and that don't use proprietary or other closed-source tools. Sometimes I am forced to accept things whether I like them or not. So I accept them: maybe gracefully, maybe not. Let's think this through. Here are a few comments showing my thought processes so far: 1. A string constant must be known to the compiler at compile time. This is consistent with your desire not to have the executable target program read in the stuff at execution time. 2. At compile time, the compiler only knows what you tell it. 3. The way that you tell the compiler something is to put it into source code somewhere. If the stuff you need is in a separate file (as in my example, where the C++ source for the string stuff was in a file named "lines.cpp"), it may be compiled separately and the result linked together with the main(), but it still must be some kind of source code recognizable by that compiler. 4. At link time, when the executable target program is created, the other stuff (object files) could have come from any kind of source file for which your compiler or other tool can compile into a form that can be combined with the object code for main(). For example, the GNU Octave program links a bunch of C++ and C code together with matrix manipulation and other math functions that are written in Fortran. So the other stuff doesn't have to originate as C++. 4. Windows compiler suites (like Visual Studio) allow resource code, such as stuff to define string constants, to be in some other format (not C++ or C# other high-level language code), but a separate tool (the Resource Compiler) converts it to something that can be linked together with the other stuff (or put into a static library file or dll). Implementation details of the Resource Compiler are unknown to me. That is: I don't know (and I don't particularly care) how it does it since it is closed source. There are open-source tools that do the equivalent, but I haven't looked at them since I haven't needed anything more elaborate than my simple example. 5. My little example uses the C++ compiler to create a program that converts the string constant "resource file," lines.txt, into a standard C++ source file for a function. That file, lines.cpp can be compiled and its function will be linked into the target executable with the same C++ compiler without using any proprietary or other non-C++ tools. 6. My example is a real simple task, and it is easily automated using only the GNU make utility. Slightly more "interesting" stuff might be handled as shell scripts, possibly in conjunction with sed, awk, perl, etc., bit I stick with standard C/C++ and the make utility until I get really, really tired of it and decide that I need something more elaborate. After all I already have a Makefile for any but the most trivial projects. Like C++ itself, I think I use something like 1% of the power of the make utility (maybe less), and if I need to do something that I haven't considered before, I try to learn more about the tools that I already use. If I were a perl wonk, maybe I would do it in perl. I hope that you get the idea. 7. Go back over my comments 1-6 with a view towards answering your question about ways of doing the deed (or possibly restating it in a way that we can go from here). Regards, Dave |
|
#8
|
|||
|
|||
Re: Storing long string constants inside a programYou just opened my eyes
especially point 5 and 6 made it clear for me, that I should automate the procedure of creating the C++ strings. Normally I have an aversion against machine-generated source-code but I guess this should be ok... I'll check back with my results when I am done. Regards, Rob |
|
#9
|
|||
|
|||
Re: Storing long string constants inside a programQuote:
The trick is that after the development and debugging, you don't really want to look at the machine-generated C++. Always deal with the input "lines.txt" file (or whatever), whose format you have designed for your convenience. The fact that you will be looking at "lines.cpp" from time to time (when questions and/or bugs raise their ugly heads) means that you should at least put newlines at the end of each line rather than have one big long line, as I have seen in certain machine-generated code. Stuff like that. Heck, you could even put in a comment or two. here and there. The problem is that if you hand the project off to someone else, they are likely to edit "lines.cpp" directly instead of "lines.txt," thereby royally screwing the pooch for repeatability and project maintainability. That's why you make the entire process automatic instead of telling the end user to manually recompile and/or and re-apply the conversion program whenever the strings are to be altered. Bottom line: In my experience, regardless of the excellence and quantity of external documentation, the only thing that you can count on surviving is the source code. Make it easy on yourself and future maintainers by putting everything possible (and making it as clear as possible) in the source code (including documentation in and of the Makefile.) Regards, Dave |
|
#10
|
|||
|
|||
Re: Storing long string constants inside a programOk,
Here is what I have done till now: Each string I want to use is in a separate file. I create a header file with a map<file_name,string_in_file> This is the Ruby script I use to do that: txt2string.rb: Code:
fey_read_file.rb: Code:
If I call this script like this: $ ./txt2string.rb -i file_name1 file_name2 -o header_name.h I get this output file: header_name.h CPP / C++ / C Code:
Which I can use in main.cpp like this: CPP / C++ / C Code:
Now I only have to add this to my Visual Studio environment... (what great fun... Any suggestions for improvements are very welcome. |
Recent GIDBlog
Problems with the Navy (Chiefs) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Memory leak when nothing is happening... How can I even debug this ? | Algar | MS Visual C++ / MFC Forum | 10 | 19-Nov-2007 08:17 |
| Two-Tier data dissemination code installation problem | nidhibansal1984 | Computer Software Forum - Linux | 6 | 16-Sep-2007 11:13 |
| Compiled program does not execute | Serpentine | MS Visual C++ / MFC Forum | 1 | 23-Jan-2007 10:41 |
| Help with String Program | limergal | C++ Forum | 6 | 03-Dec-2006 15:47 |
| help with survey program | shaffer | C++ Forum | 5 | 01-Dec-2006 09:51 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The