]> git.lyx.org Git - lyx.git/blobdiff - src/lengthcommon.cpp
Correctly interpret return value of LyXRC::read(FileName const & filename, bool check...
[lyx.git] / src / lengthcommon.cpp
index 59ba5070020e90d469de872377fa77cd1e69ad9d..f71d009dbd443cfa67f51b9b1db5d11e641381ce 100644 (file)
@@ -19,9 +19,6 @@
 #include "support/lassert.h"
 #include "support/lstrings.h"
 
-#include <cstring>
-#include <string>
-
 using namespace std;
 using namespace lyx::support;
 
@@ -35,7 +32,7 @@ char const * const unit_name[] = {
        "bp", "cc", "cm", "dd", "em", "ex", "in", "mm", "mu",
        "pc", "pt", "sp",
        "text%",  "col%", "page%", "line%",
-       "theight%", "pheight%", "" };
+       "theight%", "pheight%", "baselineskip%", "" };
 
 int const num_units = int(sizeof(unit_name) / sizeof(unit_name[0]) - 1);
 
@@ -45,7 +42,8 @@ char const * const unit_name_gui[] = {
        N_("ex"), N_("in[[unit of measure]]"), N_("mm"), N_("mu[[unit of measure]]"), N_("pc"),
        N_("pt"), N_("sp"), N_("Text Width %"),
        N_("Column Width %"), N_("Page Width %"), N_("Line Width %"),
-       N_("Text Height %"), N_("Page Height %"), "" };
+       N_("Text Height %"), N_("Page Height %"), N_("Line Distance %"), "" };
+
 
 Length::UNIT unitFromString(string const & data)
 {
@@ -58,22 +56,6 @@ Length::UNIT unitFromString(string const & data)
 
 namespace {
 
-/// used to return numeric values in parsing vspace
-double number[4] = { 0, 0, 0, 0 };
-
-/// used to return unit types in parsing vspace
-Length::UNIT unit[4] = {
-       Length::UNIT_NONE,
-       Length::UNIT_NONE,
-       Length::UNIT_NONE,
-       Length::UNIT_NONE
-};
-
-/// the current position in the number array
-int number_index;
-/// the current position in the unit array
-int unit_index;
-
 /// skip n characters of input
 inline void lyx_advance(string & data, size_t n)
 {
@@ -91,6 +73,8 @@ inline bool isEndOfData(string const & data)
 /**
  * nextToken -  return the next token in the input
  * @param data input string
+ * @param number_index the current position in the number array
+ * @param unit_index the current position in the unit array
  * @return a char representing the type of token returned
  *
  * The possible return values are :
@@ -100,7 +84,8 @@ inline bool isEndOfData(string const & data)
  *     u       a unit type (stored in unit array)
  *     E       parse error
  */
-char nextToken(string & data)
+char nextToken(string & data, double * number, int & number_index,
+               Length::UNIT * unit, int & unit_index)
 {
        data = ltrim(data);
 
@@ -219,7 +204,8 @@ LaTeXLength table[] = {
 };
 
 
-} // namespace anon
+} // namespace
+
 
 const char * stringFromUnit(int unit)
 {
@@ -247,8 +233,11 @@ bool isValidGlueLength(string const & data, GlueLength * result)
        // forward approach leads to very long, tedious code that would be
        // much harder to understand and maintain. (AS)
 
-       if (data.empty())
+       if (data.empty()) {
+               if (result)
+                       *result = GlueLength();
                return true;
+       }
        string buffer = ltrim(data);
 
        // To make isValidGlueLength recognize negative values as
@@ -267,7 +256,13 @@ bool isValidGlueLength(string const & data, GlueLength * result)
        }
        // end of hack
 
-       number_index = unit_index = 1;  // entries at index 0 are sentinels
+       // used to return numeric values in parsing vspace
+       double number[4] = { 0, 0, 0, 0 };
+       // used to return unit types in parsing vspace
+       Length::UNIT unit[4] = {Length::UNIT_NONE, Length::UNIT_NONE,
+                               Length::UNIT_NONE, Length::UNIT_NONE};
+       int number_index = 1; // entries at index 0 are sentinels
+       int unit_index = 1;   // entries at index 0 are sentinels
 
        // construct "pattern" from "data"
        size_t const pattern_max_size = 20;
@@ -275,7 +270,8 @@ bool isValidGlueLength(string const & data, GlueLength * result)
        while (!isEndOfData(buffer)) {
                if (pattern.size() > pattern_max_size)
                        return false;
-               char const c = nextToken(buffer);
+               char const c = nextToken(buffer, number, number_index, unit,
+                               unit_index);
                if (c == 'E')
                        return false;
                pattern.push_back(c);
@@ -310,12 +306,13 @@ bool isValidLength(string const & data, Length * result)
        // The parser may seem overkill for lengths without
        // glue, but since we already have it, using it is
        // easier than writing something from scratch.
-       if (data.empty())
+       if (data.empty()) {
+               if (result)
+                       *result = Length();
                return true;
+       }
 
-       string   buffer = data;
-       int      pattern_index = 0;
-       char     pattern[3];
+       string buffer = data;
 
        // To make isValidLength recognize negative values
        // this little hack is needed:
@@ -334,21 +331,28 @@ bool isValidLength(string const & data, Length * result)
        }
        // end of hack
 
-       number_index = unit_index = 1;  // entries at index 0 are sentinels
+       // used to return numeric values in parsing vspace
+       double number[4] = { 0, 0, 0, 0 };
+       // used to return unit types in parsing vspace
+       Length::UNIT unit[4] = {Length::UNIT_NONE, Length::UNIT_NONE,
+                               Length::UNIT_NONE, Length::UNIT_NONE};
+       int number_index = 1; // entries at index 0 are sentinels
+       int unit_index = 1;   // entries at index 0 are sentinels
 
        // construct "pattern" from "data"
+       string pattern;
        while (!isEndOfData(buffer)) {
-               if (pattern_index > 2)
+               if (pattern.size() > 2)
                        return false;
-               pattern[pattern_index] = nextToken(buffer);
-               if (pattern[pattern_index] == 'E')
+               char const token = nextToken(buffer, number,
+                               number_index, unit, unit_index);
+               if (token == 'E')
                        return false;
-               ++pattern_index;
+               pattern += token;
        }
-       pattern[pattern_index] = '\0';
 
        // only the most basic pattern is accepted here
-       if (strcmp(pattern, "nu") != 0)
+       if (pattern != "nu")
                return false;
 
        // It _was_ a correct length string.