Results 1 to 5 of 5
- 02-08-2012, 09:58 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
gettext binary MO file creating with Java
I tried creating a utility to parse gettext po file and generate binary mo file. The parser is simple (my co. not use fuzzy, plural, etc. things, just msgid/msgstr), but the generator is not work.
Here is the description of the mo file, here is the original generator source (it's C), and found a php script also.
My code:
The line os.writeInt(key_start); seems like ok, the differences from the original tool generated mo file starting after write the hash table.Java Code:public void writeFile(String filename, Map<String, String> polines) throws FileNotFoundException, IOException { DataOutputStream os = new DataOutputStream(new FileOutputStream(filename)); HashMap<String, String> bvc = new HashMap<String, String>(); TreeMap<String, String> hash = new TreeMap(bvc); hash.putAll(polines); StringBuilder ids = new StringBuilder(); StringBuilder strings = new StringBuilder(); ArrayList<ArrayList> offsets = new ArrayList<ArrayList>(); ArrayList<Integer> key_offsets = new ArrayList<Integer>(); ArrayList<Integer> value_offsets = new ArrayList<Integer>(); ArrayList<Integer> temp_offsets = new ArrayList<Integer>(); for (Map.Entry<String, String> entry : hash.entrySet()) { String id = entry.getKey(); String str = entry.getValue(); ArrayList<Integer> offsetsItems = new ArrayList<Integer>(); offsetsItems.add(ids.length()); offsetsItems.add(id.length()); offsetsItems.add(strings.length()); offsetsItems.add(str.length()); offsets.add((ArrayList) offsetsItems.clone()); ids.append(id).append('\0'); strings.append(str).append('\0'); } Integer key_start = 7 * 4 + hash.size() * 4 * 4; Integer value_start = key_start + ids.length(); Iterator e = offsets.iterator(); while (e.hasNext()) { ArrayList<Integer> offEl = (ArrayList<Integer>) e.next(); key_offsets.add(offEl.get(1)); key_offsets.add(offEl.get(0) + key_start); value_offsets.add(offEl.get(3)); value_offsets.add(offEl.get(2) + value_start); } temp_offsets.addAll(key_offsets); temp_offsets.addAll(value_offsets); os.writeInt(0xde120495); os.writeByte(0x00); os.writeInt(hash.size()); os.writeInt((7 * 4)); os.writeInt((7 * 4 + hash.size() * 8)); os.writeInt(0x00000000); os.writeInt(key_start); Iterator offi = temp_offsets.iterator(); while (offi.hasNext()) { Integer off = (Integer) offi.next(); os.writeInt(off); } os.write(ids.toString().getBytes("utf-8")); os.write(strings.toString().getBytes("utf-8")); os.close(); }
There is a good generated file binary dump (with the original msgfmt utility): the good And this is the wrong, generated by the code above: the bad.
So, what's wrong? (aside from my scary english..)Last edited by Mortein79; 02-08-2012 at 10:24 AM.
- 02-08-2012, 01:06 PM #2
Re: gettext binary MO file creating with Java
How are you trying to debug the code?
Take a very small input file and follow all the calculations done by the program, byte by byte, as it creates the output file.
If you don't have an interactive debugger then use printlns statements to show what the code is doing.
If you understand what the code is supposed to do, seeing what it is doing should help you change the code so it does what you want.
If you are having trouble with statements that don't do what you want, post the statements, the input to them and the desired output from them and your questions.
- 02-08-2012, 01:18 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
Re: gettext binary MO file creating with Java
Thanks to the reply. My problem is: don't understand the specification of the mo file exatly, specially the hash table part. My implementation is buggy, and when step-by-step debugging (I use Netbeans 7.1), I don't know the numbers in the variables is true or wrong. My problem is not theoretical, it's concrete technical. :)
- 02-08-2012, 01:32 PM #4
Re: gettext binary MO file creating with Java
You need to understand the algorithm for what the code is supposed to do.
When you can do the steps manually you should be able to write code to do the steps.
If you are having trouble writing the steps, then post your specific questions about how to do a specific step.
- 02-08-2012, 01:34 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Creating a picture in java out of a txt file containing 0's and 1's
By The Dark Dragon in forum New To JavaReplies: 1Last Post: 11-27-2011, 04:25 AM -
Creating a file is Holding up java.
By rizowski in forum New To JavaReplies: 0Last Post: 04-20-2011, 05:47 PM -
Reading Binary File using java
By pnbalaji in forum New To JavaReplies: 16Last Post: 06-11-2010, 02:50 PM -
Creating Folder in JAVA and Copying File
By fnoman in forum New To JavaReplies: 8Last Post: 10-18-2008, 07:33 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks