]> git.lyx.org Git - lyx.git/blobdiff - src/tex2lyx/texparser.C
Get rid of extraneous whitespace.
[lyx.git] / src / tex2lyx / texparser.C
index ea69d460d49c311960521319a3610ea385fbb50a..8a17a47d33b897e9cd34a09e17790148c87cf153 100644 (file)
@@ -39,14 +39,14 @@ void catInit()
        theCatcode[int('}')]  = catEnd;
        theCatcode[int('$')]  = catMath;
        theCatcode[int('&')]  = catAlign;
-       theCatcode[10]   = catNewline;
+       theCatcode[int('\n')] = catNewline;
        theCatcode[int('#')]  = catParameter;
        theCatcode[int('^')]  = catSuper;
        theCatcode[int('_')]  = catSub;
-       theCatcode[0x7f] = catIgnore;
+       theCatcode[0x7f]      = catIgnore;
        theCatcode[int(' ')]  = catSpace;
        theCatcode[int('\t')] = catSpace;
-       theCatcode[13]   = catIgnore;
+       theCatcode[int('\r')] = catNewline;
        theCatcode[int('~')]  = catActive;
        theCatcode[int('%')]  = catComment;
 
@@ -54,6 +54,30 @@ void catInit()
        theCatcode[int('@')]  = catLetter;
 }
 
+
+/*!
+ * Translate a line ending to '\n'.
+ * \p c must have catcode catNewline, and it must be the last character read
+ * from \p is.
+ */
+char getNewline(istream & is, char c)
+{
+       // we have to handle 3 different line endings:
+       // - UNIX (\n)
+       // - MAC  (\r)
+       // - DOS  (\r\n)
+       if (c == '\r') {
+               // MAC or DOS
+               if (is.get(c) && c != '\n') {
+                       // MAC
+                       is.putback(c);
+               }
+               return '\n';
+       }
+       // UNIX
+       return c;
+}
+
 }
 
 
@@ -247,21 +271,22 @@ char Parser::getChar()
 }
 
 
-string Parser::getArg(char left, char right)
+Parser::Arg Parser::getFullArg(char left, char right)
 {
        skip_spaces(true);
 
        // This is needed if a partial file ends with a command without arguments,
        // e. g. \medskip
        if (! good())
-               return string();
+               return std::make_pair(false, string());
 
        string result;
        char c = getChar();
 
-       if (c != left)
+       if (c != left) {
                putback();
-       else
+               return std::make_pair(false, string());
+       } else
                while ((c = getChar()) != right && good()) {
                        // Ignore comments
                        if (curr_token().cat() == catComment) {
@@ -272,14 +297,29 @@ string Parser::getArg(char left, char right)
                                result += curr_token().asInput();
                }
 
-       return result;
+       return std::make_pair(true, result);
+}
+
+
+string Parser::getArg(char left, char right)
+{
+       return getFullArg(left, right).second;
+}
+
+
+string Parser::getFullOpt()
+{
+       Arg arg = getFullArg('[', ']');
+       if (arg.first)
+               return '[' + arg.second + ']';
+       return arg.second;
 }
 
 
 string Parser::getOpt()
 {
        string const res = getArg('[', ']');
-       return res.size() ? '[' + res + ']' : string();
+       return res.empty() ? string() : '[' + res + ']';
 }
 
 
@@ -309,10 +349,10 @@ void Parser::tokenize(istream & is)
 
                        case catNewline: {
                                ++lineno_;
-                               string s(1, c);
+                               string s(1, getNewline(is, c));
                                while (is.get(c) && catcode(c) == catNewline) {
                                        ++lineno_;
-                                       s += c;
+                                       s += getNewline(is, c);
                                }
                                if (catcode(c) != catNewline)
                                        is.putback(c);
@@ -326,6 +366,9 @@ void Parser::tokenize(istream & is)
                                string s;
                                while (is.get(c) && catcode(c) != catNewline)
                                        s += c;
+                               // handle possible DOS line ending
+                               if (catcode(c) == catNewline)
+                                       c = getNewline(is, c);
                                // Note: The '%' at the beginning and the '\n' at the end
                                // of the comment are not stored.
                                ++lineno_;
@@ -352,8 +395,7 @@ void Parser::tokenize(istream & is)
                        }
 
                        case catIgnore: {
-                               if (c != 13)
-                                       cerr << "ignoring a char: " << int(c) << "\n";
+                               cerr << "ignoring a char: " << int(c) << "\n";
                                break;
                        }