Changeset 27563

Show
Ignore:
Timestamp:
11/16/08 18:02:00 (2 months ago)
Author:
lasgouttes
Message:

First serious step for utf8 file format format. tex2lyx is now able to
read utf8 tex documents and translate them to lyxformat 249.

There is still no code to discover the encoding and use it, but it is the
easiest part (I hope).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • lyx-devel/trunk/src/tex2lyx/Parser.cpp

    r27489 r27563  
    1414 
    1515#include <iostream> 
    16 #include <sstream> 
    1716 
    1817using namespace std; 
     
    5554} 
    5655 
    57  
    5856/*! 
    5957 * Translate a line ending to '\n'. 
     
    6159 * from \p is. 
    6260 */ 
    63 char getNewline(istream & is, char c) 
     61char getNewline(idocstream & is, char c) 
    6462{ 
    6563        // we have to handle 3 different line endings: 
     
    6967        if (c == '\r') { 
    7068                // MAC or DOS 
    71                 if (is.get(c) && c != '\n') { 
     69                char_type wc; 
     70                if (is.get(wc) && wc != '\n') { 
    7271                        // MAC 
    73                         is.putback(c); 
     72                        is.putback(wc); 
    7473                } 
    7574                return '\n'; 
     
    7978} 
    8079 
    81 
    82  
    83  
    84 // 
    85 // catcodes 
    86 // 
    87  
    88 CatCode catcode(unsigned char c) 
    89 
    90         return theCatcode[c]; 
    91 
    92  
     80CatCode catcode(char_type c) 
     81
     82        if (c < 256) 
     83                return theCatcode[(unsigned char)c]; 
     84        return catOther; 
     85
     86 
     87
    9388 
    9489 
     
    106101                os << '\\' << t.cs() << ' '; 
    107102        else if (t.cat() == catLetter) 
    108                 os << t.character(); 
     103                os << t.cs(); 
    109104        else if (t.cat() == catNewline) 
    110105                os << "[" << t.cs().size() << "\\n," << t.cat() << "]\n"; 
    111106        else 
    112                 os << '[' << t.character() << ',' << t.cat() << ']'; 
     107                os << '[' << t.cs() << ',' << t.cat() << ']'; 
    113108        return os; 
    114109} 
     
    117112string Token::asString() const 
    118113{ 
    119         return cs_.size() ? cs_ : string(1, char_)
     114        return cs_
    120115} 
    121116 
     
    125120        if (cat_ == catComment) 
    126121                return '%' + cs_ + '\n'; 
    127         if (cat_ == catSpace || cat_ == catNewline) 
    128                 return cs_; 
    129         return char_ ? string(1, char_) : '\\' + cs_; 
     122        if (cat_ == catEscape) 
     123                return '\\' + cs_; 
     124        return cs_; 
    130125} 
    131126 
     
    136131 
    137132 
    138 Parser::Parser(istream & is) 
     133Parser::Parser(idocstream & is) 
    139134        : lineno_(0), pos_(0), iss_(0), is_(is) 
    140135{ 
     
    143138 
    144139Parser::Parser(string const & s) 
    145         : lineno_(0), pos_(0), iss_(new istringstream(s)), is_(*iss_) 
     140        : lineno_(0), pos_(0),  
     141          iss_(new idocstringstream(from_utf8(s))), is_(*iss_) 
    146142{ 
    147143} 
     
    268264        if (!good()) 
    269265                error("The input stream is not well..."); 
    270         return tokens_[pos_++].character(); 
     266        return get_token().character(); 
    271267} 
    272268 
     
    366362{ 
    367363        catInit(); 
    368         char c; 
     364        char_type c; 
    369365        if (!is_.get(c))  
    370366                return; 
    371         //cerr << "reading c: " << c << "\n"; 
    372367 
    373368        switch (catcode(c)) { 
    374369        case catSpace: { 
    375                 string s(1, c); 
     370                docstring s(1, c); 
    376371                while (is_.get(c) && catcode(c) == catSpace) 
    377372                        s += c; 
     
    384379        case catNewline: { 
    385380                ++lineno_; 
    386                 string s(1, getNewline(is_, c)); 
     381                docstring s(1, getNewline(is_, c)); 
    387382                while (is_.get(c) && catcode(c) == catNewline) { 
    388383                        ++lineno_; 
     
    398393                // We don't treat "%\n" combinations here specially because 
    399394                // we want to preserve them in the preamble 
    400                 string s; 
     395                docstring s; 
    401396                while (is_.get(c) && catcode(c) != catNewline) 
    402397                        s += c; 
     
    416411                        error("unexpected end of input"); 
    417412                } else { 
    418                         string s(1, c); 
     413                        docstring s(1, c); 
    419414                        if (catcode(c) == catLetter) { 
    420415                                // collect letters 
     
    430425                 
    431426        case catIgnore: { 
    432                 cerr << "ignoring a char: " << int(c) << "\n"; 
     427                cerr << "ignoring a char: " << c << "\n"; 
    433428                break; 
    434429        } 
    435430                 
    436431        default: 
    437                 push_back(Token(c, catcode(c))); 
    438         } 
     432                push_back(Token(docstring(1, c), catcode(c))); 
     433        } 
     434        //cerr << tokens_.back(); 
    439435} 
    440436 
     
    465461        if (next_token().character() == '[') { 
    466462                Token t = get_token(); 
    467                 for (Token t = get_token(); t.character() != ']' && good(); t = get_token()) { 
     463                for (t = get_token(); t.character() != ']' && good(); t = get_token()) { 
    468464                        if (t.cat() == catBegin) { 
    469465                                putback(); 
  • lyx-devel/trunk/src/tex2lyx/Parser.h

    r27489 r27563  
    1313#define PARSER_H 
    1414 
    15 #include <vector> 
    1615#include <string> 
    1716#include <utility> 
    18  
     17#include <vector> 
     18 
     19#include "support/docstream.h" 
    1920 
    2021namespace lyx { 
     
    4748 
    4849 
    49 CatCode catcode(unsigned char c); 
    50  
    51  
    5250enum { 
    5351        FLAG_BRACE_LAST = 1 << 1,  //  last closing brace ends the parsing 
     
    7674public: 
    7775        /// 
    78         Token() : cs_(), char_(0), cat_(catIgnore) {} 
    79         /// 
    80         Token(char c, CatCode cat) : cs_(), char_(c), cat_(cat) {} 
    81         /// 
    82         Token(std::string const & cs, CatCode cat) : cs_(cs), char_(0), cat_(cat) {} 
     76        Token() : cs_(), cat_(catIgnore) {} 
     77        /// 
     78        Token(docstring const & cs, CatCode cat) : cs_(to_utf8(cs)), cat_(cat) {} 
    8379 
    8480        /// 
     
    8783        CatCode cat() const { return cat_; } 
    8884        /// 
    89         char character() const { return char_; } 
     85        char character() const { return cs_.empty() ? 0 : cs_[0]; } 
    9086        /// Returns the token as string 
    9187        std::string asString() const; 
     
    9692        /// 
    9793        std::string cs_; 
    98         /// 
    99         char char_; 
    10094        /// 
    10195        CatCode cat_; 
     
    120114public: 
    121115        /// 
    122         Parser(std::istream & is); 
     116        Parser(idocstream & is); 
    123117        /// 
    124118        Parser(std::string const & s); 
     
    218212        unsigned pos_; 
    219213        /// 
    220         std::istringstream * iss_; 
    221         /// 
    222         std::istream & is_; 
     214        idocstringstream * iss_; 
     215        /// 
     216        idocstream & is_; 
    223217}; 
    224218 
  • lyx-devel/trunk/src/tex2lyx/math.cpp

    r27425 r27563  
    9595                               t.cat() == catActive || 
    9696                               t.cat() == catParameter) 
    97                         os << t.character(); 
     97                        os << t.cs(); 
    9898 
    9999                else if (t.cat() == catBegin) { 
  • lyx-devel/trunk/src/tex2lyx/preamble.cpp

    r27425 r27563  
    414414{ 
    415415        os << "#LyX file created by tex2lyx " << PACKAGE_VERSION << "\n" 
    416            << "\\lyxformat 247\n" 
     416           << "\\lyxformat 249\n" 
    417417           << "\\begin_document\n" 
    418418           << "\\begin_header\n" 
  • lyx-devel/trunk/src/tex2lyx/table.cpp

    r27425 r27563  
    662662                } 
    663663 
    664                 else if (t.cat() == catSpace || t.cat() == catNewline) 
    665                                 os << t.cs(); 
    666  
    667                 else if (t.cat() == catLetter || 
    668                                t.cat() == catSuper || 
    669                                t.cat() == catSub || 
    670                                t.cat() == catOther || 
    671                                t.cat() == catActive || 
    672                                t.cat() == catParameter) 
    673                         os << t.character(); 
     664                else if (t.cat() == catSpace  
     665                         || t.cat() == catNewline 
     666                         || t.cat() == catLetter  
     667                         || t.cat() == catSuper  
     668                         || t.cat() == catSub  
     669                         || t.cat() == catOther  
     670                         || t.cat() == catActive  
     671                         || t.cat() == catParameter) 
     672                        os << t.cs(); 
    674673 
    675674                else if (t.cat() == catBegin) { 
  • lyx-devel/trunk/src/tex2lyx/tex2lyx.cpp

    r27428 r27563  
    1919#include "Layout.h" 
    2020 
    21 #include "support/lassert.h" 
    2221#include "support/convert.h" 
    2322#include "support/debug.h" 
    2423#include "support/ExceptionMessage.h" 
    2524#include "support/filetools.h" 
     25#include "support/lassert.h" 
    2626#include "support/lstrings.h" 
    2727#include "support/os.h" 
     
    2929 
    3030#include <cstdlib> 
    31 #include <fstream> 
    3231#include <iostream> 
    3332#include <string> 
     
    203202void read_syntaxfile(FileName const & file_name) 
    204203{ 
    205         ifstream is(file_name.toFilesystemEncoding().c_str()); 
     204        ifdocstream is(file_name.toFilesystemEncoding().c_str()); 
    206205        if (!is.good()) { 
    207206                cerr << "Could not open syntax file \"" << file_name 
     
    390389 *  this function! 
    391390 */ 
    392 void tex2lyx(istream & is, ostream & os) 
     391void tex2lyx(idocstream & is, ostream & os) 
    393392{ 
    394393        Parser p(is); 
     
    412411#ifdef TEST_PARSER 
    413412        p.reset(); 
    414         ofstream parsertest("parsertest.tex"); 
     413        ofdocstream parsertest("parsertest.tex"); 
    415414        while (p.good()) 
    416415                parsertest << p.get_token().asInput(); 
     
    423422bool tex2lyx(FileName const & infilename, ostream & os) 
    424423{ 
    425         ifstream is(infilename.toFilesystemEncoding().c_str()); 
     424        ifdocstream is(infilename.toFilesystemEncoding().c_str()); 
    426425        if (!is.good()) { 
    427426                cerr << "Could not open input file \"" << infilename 
  • lyx-devel/trunk/src/tex2lyx/text.cpp

    r27489 r27563  
    12631263                        // This translates "&" to "\\&" which may be wrong... 
    12641264                        context.check_layout(os); 
    1265                         os << t.character(); 
     1265                        os << t.cs(); 
    12661266                } 
    12671267 
     
    12821282                                        os << "\\InsetSpace ~\n"; 
    12831283                        } else 
    1284                                 os << t.character(); 
     1284                                os << t.cs(); 
    12851285                } 
    12861286 
     
    13101310                                p.get_token(); 
    13111311                                if (p.next_token().cat() == catEnd) { 
    1312                                         os << next.character(); 
     1312                                        os << next.cs(); 
    13131313                                        p.get_token(); 
    13141314                                } else {