Small library for defining string translations. Basically it stores key-values-pairs, and allows their retrieval. Needs the csv_parser library.
Localization.cpp@0:56714c5edd05, 2011-02-22 (annotated)
- Committer:
- hlipka
- Date:
- Tue Feb 22 23:07:49 2011 +0000
- Revision:
- 0:56714c5edd05
initial public version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hlipka | 0:56714c5edd05 | 1 | /* |
hlipka | 0:56714c5edd05 | 2 | * Localization library |
hlipka | 0:56714c5edd05 | 3 | * Copyright (c) 2010 Hendrik Lipka |
hlipka | 0:56714c5edd05 | 4 | * |
hlipka | 0:56714c5edd05 | 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
hlipka | 0:56714c5edd05 | 6 | * of this software and associated documentation files (the "Software"), to deal |
hlipka | 0:56714c5edd05 | 7 | * in the Software without restriction, including without limitation the rights |
hlipka | 0:56714c5edd05 | 8 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
hlipka | 0:56714c5edd05 | 9 | * copies of the Software, and to permit persons to whom the Software is |
hlipka | 0:56714c5edd05 | 10 | * furnished to do so, subject to the following conditions: |
hlipka | 0:56714c5edd05 | 11 | * |
hlipka | 0:56714c5edd05 | 12 | * The above copyright notice and this permission notice shall be included in |
hlipka | 0:56714c5edd05 | 13 | * all copies or substantial portions of the Software. |
hlipka | 0:56714c5edd05 | 14 | * |
hlipka | 0:56714c5edd05 | 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
hlipka | 0:56714c5edd05 | 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
hlipka | 0:56714c5edd05 | 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
hlipka | 0:56714c5edd05 | 18 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
hlipka | 0:56714c5edd05 | 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
hlipka | 0:56714c5edd05 | 20 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
hlipka | 0:56714c5edd05 | 21 | * THE SOFTWARE. |
hlipka | 0:56714c5edd05 | 22 | */ |
hlipka | 0:56714c5edd05 | 23 | |
hlipka | 0:56714c5edd05 | 24 | #include "Localization.h" |
hlipka | 0:56714c5edd05 | 25 | #include "csv_parser.h" |
hlipka | 0:56714c5edd05 | 26 | |
hlipka | 0:56714c5edd05 | 27 | using namespace std; |
hlipka | 0:56714c5edd05 | 28 | |
hlipka | 0:56714c5edd05 | 29 | Localization::Localization() { |
hlipka | 0:56714c5edd05 | 30 | const char * filename = "/local/loc.csv"; |
hlipka | 0:56714c5edd05 | 31 | |
hlipka | 0:56714c5edd05 | 32 | const char field_terminator = '='; |
hlipka | 0:56714c5edd05 | 33 | const char line_terminator = '\n'; |
hlipka | 0:56714c5edd05 | 34 | const char enclosure_char = '"'; |
hlipka | 0:56714c5edd05 | 35 | |
hlipka | 0:56714c5edd05 | 36 | _entries=new list<MsgEntry*>(); |
hlipka | 0:56714c5edd05 | 37 | |
hlipka | 0:56714c5edd05 | 38 | csv_parser file_parser;//=new csv_parser();; |
hlipka | 0:56714c5edd05 | 39 | |
hlipka | 0:56714c5edd05 | 40 | /* Define how many records we're gonna skip. This could be used to skip the column definitions. */ |
hlipka | 0:56714c5edd05 | 41 | file_parser.set_skip_lines(0); |
hlipka | 0:56714c5edd05 | 42 | |
hlipka | 0:56714c5edd05 | 43 | /* Specify the file to parse */ |
hlipka | 0:56714c5edd05 | 44 | file_parser.init(filename); |
hlipka | 0:56714c5edd05 | 45 | |
hlipka | 0:56714c5edd05 | 46 | /* Here we tell the parser how to parse the file */ |
hlipka | 0:56714c5edd05 | 47 | file_parser.set_enclosed_char(enclosure_char, ENCLOSURE_OPTIONAL); |
hlipka | 0:56714c5edd05 | 48 | |
hlipka | 0:56714c5edd05 | 49 | file_parser.set_field_term_char(field_terminator); |
hlipka | 0:56714c5edd05 | 50 | |
hlipka | 0:56714c5edd05 | 51 | file_parser.set_line_term_char(line_terminator); |
hlipka | 0:56714c5edd05 | 52 | |
hlipka | 0:56714c5edd05 | 53 | /* Check to see if there are more records, then grab each row one at a time */ |
hlipka | 0:56714c5edd05 | 54 | while (file_parser.has_more_rows()) { |
hlipka | 0:56714c5edd05 | 55 | csv_row row = file_parser.get_row(); |
hlipka | 0:56714c5edd05 | 56 | if (row.size()<2) { |
hlipka | 0:56714c5edd05 | 57 | // printf("row too short\n"); |
hlipka | 0:56714c5edd05 | 58 | continue; |
hlipka | 0:56714c5edd05 | 59 | } |
hlipka | 0:56714c5edd05 | 60 | MsgEntry* me=new MsgEntry(); |
hlipka | 0:56714c5edd05 | 61 | me->key=new char[row[0].size()+1]; |
hlipka | 0:56714c5edd05 | 62 | strcpy(me->key,row[0].c_str()); |
hlipka | 0:56714c5edd05 | 63 | |
hlipka | 0:56714c5edd05 | 64 | me->text=new char[row[1].size()+1]; |
hlipka | 0:56714c5edd05 | 65 | strcpy(me->text,row[1].c_str()); |
hlipka | 0:56714c5edd05 | 66 | |
hlipka | 0:56714c5edd05 | 67 | _entries->push_back(me); |
hlipka | 0:56714c5edd05 | 68 | } |
hlipka | 0:56714c5edd05 | 69 | } |
hlipka | 0:56714c5edd05 | 70 | |
hlipka | 0:56714c5edd05 | 71 | const char* Localization::getLocalizedValue(const char* id) { |
hlipka | 0:56714c5edd05 | 72 | if (NULL==id) |
hlipka | 0:56714c5edd05 | 73 | return NULL; |
hlipka | 0:56714c5edd05 | 74 | for (list<MsgEntry*>::iterator it = _entries->begin(); it != _entries->end(); it++) { |
hlipka | 0:56714c5edd05 | 75 | MsgEntry* me=*it; |
hlipka | 0:56714c5edd05 | 76 | if (0==strcmp(me->key,id)) |
hlipka | 0:56714c5edd05 | 77 | return me->text; |
hlipka | 0:56714c5edd05 | 78 | } |
hlipka | 0:56714c5edd05 | 79 | |
hlipka | 0:56714c5edd05 | 80 | return NULL; |
hlipka | 0:56714c5edd05 | 81 | } |