⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 texutils.cpp

📁 Wxpython Implemented on Windows CE, Source code
💻 CPP
📖 第 1 页 / 共 4 页
字号:
        );
        TexReferences.DeleteContents(false);
    }
}


/*
 * Bibliography-handling code
 *
 */

void BibEatWhiteSpace(wxString& line)
{
    while(!line.empty() && (line[0] == _T(' ') || line[0] == _T('\t') || line[0] == (wxChar)EOF))
    {
        if (line[0] == 10)
            BibLine ++;
        line = line.substr(1);
    }

    // Ignore end-of-line comments
    if (line[0] == _T('%') || line[0] == _T(';') || line[0] == _T('#'))
    {
        line = wxEmptyString;
    }
}

void BibEatWhiteSpace(wxSTD istream& str)
{
  char ch = (char)str.peek();

  while (!str.eof() && (ch == ' ' || ch == '\t' || ch == 13 || ch == 10 || ch == (char)EOF))
  {
    if (ch == 10)
      BibLine ++;
    str.get(ch);
    if ((ch == (char)EOF) || str.eof()) return;
    ch = (char)str.peek();
  }

  // Ignore end-of-line comments
  if (ch == '%' || ch == ';' || ch == '#')
  {
    str.get(ch);
    ch = (char)str.peek();
    while (ch != 10 && ch != 13 && !str.eof())
    {
      str.get(ch);
      ch = (char)str.peek();
    }
    BibEatWhiteSpace(str);
  }
}

// Read word up to { or , or space
wxString BibReadWord(wxString& line)
{
    wxString val;

    while (!line.empty() &&
           line[0] != _T('\t') &&
           line[0] != _T(' ') &&
           line[0] != _T('{') &&
           line[0] != _T('(') &&
           line[0] != _T(',') &&
           line[0] != _T('='))
    {
        val << line[0];
        line = line.substr(1);
    }
    return val;
}

void BibReadWord(wxSTD istream& istr, wxChar *buffer)
{
  int i = 0;
  buffer[i] = 0;
  char ch = (char)istr.peek();
  while (!istr.eof() && ch != ' ' && ch != '{' && ch != '(' && ch != 13 && ch != 10 && ch != '\t' &&
         ch != ',' && ch != '=')
  {
    istr.get(ch);
    buffer[i] = ch;
    i ++;
    ch = (char)istr.peek();
  }
  buffer[i] = 0;
}

// Read string (double-quoted or not) to end quote or EOL
wxString BibReadToEOL(wxString& line)
{
    if(line.empty())
        return wxEmptyString;

    wxString val;
    bool inQuotes = false;
    if (line[0] == _T('"'))
    {
        line = line.substr(1);
        inQuotes = true;
    }
    // If in quotes, read white space too. If not,
    // stop at white space or comment.
    while (!line.empty() && line[0] != _T('"') &&
           (inQuotes || ((line[0] != _T(' ')) && (line[0] != 9) &&
                          (line[0] != _T(';')) && (line[0] != _T('%')) && (line[0] != _T('#')))))
    {
        val << line[0];
        line = line.substr(1);
    }
    if (line[0] == '"')
        line = line.substr(1);

    return val;
}

void BibReadToEOL(wxSTD istream& istr, wxChar *buffer)
{
    int i = 0;
    buffer[i] = 0;
    char ch = (char)istr.peek();
    bool inQuotes = false;
    if (ch == '"')
    {
        istr.get(ch);
        ch = (char)istr.peek();
        inQuotes = true;
    }
    // If in quotes, read white space too. If not,
    // stop at white space or comment.
    while (!istr.eof() && ch != 13 && ch != 10 && ch != _T('"') &&
           (inQuotes || ((ch != _T(' ')) && (ch != 9) &&
                          (ch != _T(';')) && (ch != _T('%')) && (ch != _T('#')))))
    {
        istr.get(ch);
        buffer[i] = ch;
        i ++;
        ch = (char)istr.peek();
    }
    if (ch == '"')
        istr.get(ch);
    buffer[i] = 0;
}

// Read }-terminated value, taking nested braces into account.
wxString BibReadValue(wxString& line,
                      bool ignoreBraces = true,
                      bool quotesMayTerminate = true)
{
    wxString val;
    int braceCount = 1;
    bool stopping = false;

    if (line.length() >= 4000)
    {
        wxChar buf[100];
        wxSnprintf(buf, sizeof(buf), _T("Sorry, value > 4000 chars in bib file at line %ld."), BibLine);
        wxLogError(buf, "Tex2RTF Fatal Error");
        return wxEmptyString;
    }

    while (!line.empty() && !stopping)
    {
        wxChar ch = line[0];
        line = line.substr(1);

        if (ch == _T('{'))
            braceCount ++;

        if (ch == _T('}'))
        {
            braceCount --;
            if (braceCount == 0)
            {
                stopping = true;
                break;
            }
        }
        else if (quotesMayTerminate && ch == _T('"'))
        {
            stopping = true;
            break;
        }

        if (!stopping)
        {
            if (!ignoreBraces || (ch != _T('{') && ch != _T('}')))
            {
                val << ch;
            }
        }
    }

    return val;
}

void BibReadValue(wxSTD istream& istr, wxChar *buffer, bool ignoreBraces = true,
                  bool quotesMayTerminate = true)
{
    int braceCount = 1;
    int i = 0;
    buffer[i] = 0;
    char ch = (char)istr.peek();
    bool stopping = false;
    while (!istr.eof() && !stopping)
    {
//      i ++;
        if (i >= 4000)
        {
            wxChar buf[100];
            wxSnprintf(buf, sizeof(buf), _T("Sorry, value > 4000 chars in bib file at line %ld."), BibLine);
            wxLogError(buf, "Tex2RTF Fatal Error");
            return;
        }
        istr.get(ch);

        if (ch == '{')
            braceCount ++;

        if (ch == '}')
        {
            braceCount --;
            if (braceCount == 0)
            {
                stopping = true;
                break;
            }
        }
        else if (quotesMayTerminate && ch == '"')
        {
            stopping = true;
            break;
        }
        if (!stopping)
        {
            if (!ignoreBraces || (ch != '{' && ch != '}'))
            {
                buffer[i] = ch;
                i ++;
            }
        }
        if (ch == 10)
            BibLine ++;
    }
    buffer[i] = 0;
    wxUnusedVar(stopping);
}

bool ReadBib(wxChar *filename)
{
  if (!wxFileExists(filename))
      return false;

  wxString name = filename;
  wxChar buf[300];
  wxSTD ifstream istr((char const *)name.fn_str(), wxSTD ios::in);
  if (istr.bad()) return false;

  BibLine = 1;

  OnInform(_T("Reading .bib file..."));

  char ch;
  wxChar fieldValue[4000];
  wxChar recordType[100];
  wxChar recordKey[100];
  wxChar recordField[100];
  while (!istr.eof())
  {
    Tex2RTFYield();

    BibEatWhiteSpace(istr);
    istr.get(ch);
    if (ch != '@')
    {
      wxSnprintf(buf, sizeof(buf), _T("Expected @: malformed bib file at line %ld (%s)"), BibLine, filename);
      OnError(buf);
      return false;
    }
    BibReadWord(istr, recordType);
    BibEatWhiteSpace(istr);
    istr.get(ch);
    if (ch != '{' && ch != '(')
    {
      wxSnprintf(buf, sizeof(buf), _T("Expected { or ( after record type: malformed .bib file at line %ld (%s)"), BibLine, filename);
      OnError(buf);
      return false;
    }
    BibEatWhiteSpace(istr);
    if (StringMatch(recordType, _T("string"), false, true))
    {
      BibReadWord(istr, recordType);
      BibEatWhiteSpace(istr);
      istr.get(ch);
      if (ch != '=')
      {
        wxSnprintf(buf, sizeof(buf), _T("Expected = after string key: malformed .bib file at line %ld (%s)"), BibLine, filename);
        OnError(buf);
        return false;
      }
      BibEatWhiteSpace(istr);
      istr.get(ch);
      if (ch != '"' && ch != '{')
      {
        wxSnprintf(buf, sizeof(buf), _T("Expected = after string key: malformed .bib file at line %ld (%s)"), BibLine, filename);
        OnError(buf);
        return false;
      }
      BibReadValue(istr, fieldValue);

      // Now put in hash table if necesary
      if (!BibStringTable.Get(recordType))
        BibStringTable.Put(recordType, (wxObject *)copystring(fieldValue));

      // Read closing ) or }
      BibEatWhiteSpace(istr);
      istr.get(ch);
      BibEatWhiteSpace(istr);
    }
    else
    {
      BibReadWord(istr, recordKey);

      BibEntry *bibEntry = new BibEntry;
      bibEntry->key = copystring(recordKey);
      bibEntry->type = copystring(recordType);

      bool moreRecords = true;
      while (moreRecords && !istr.eof())
      {
        BibEatWhiteSpace(istr);
        istr.get(ch);
        if (ch == '}' || ch == ')')
        {
          moreRecords = false;
        }
        else if (ch == ',')
        {
          BibEatWhiteSpace(istr);
          BibReadWord(istr, recordField);
          BibEatWhiteSpace(istr);
          istr.get(ch);
          if (ch != '=')
          {
            wxSnprintf(buf, sizeof(buf), _T("Expected = after field type: malformed .bib file at line %ld (%s)"), BibLine, filename);
            OnError(buf);
            return false;
          }
          BibEatWhiteSpace(istr);
          istr.get(ch);
          if (ch != '{' && ch != '"')
          {
            fieldValue[0] = ch;
            BibReadWord(istr, fieldValue+1);

            // If in the table of strings, replace with string from table.
            wxChar *s = (wxChar *)BibStringTable.Get(fieldValue);
            if (s)
            {
              wxStrcpy(fieldValue, s);
            }
          }
          else
            BibReadValue(istr, fieldValue, true, (ch == _T('"') ? true : false));

          // Now we can add a field
          if (StringMatch(recordField, _T("author"), false, true))
            bibEntry->author = copystring(fieldValue);
          else if (StringMatch(recordField, _T("key"), false, true))
            {}
          else if (StringMatch(recordField, _T("annotate"), false, true))
            {}
          else if (StringMatch(recordField, _T("abstract"), false, true))
            {}
          else if (StringMatch(recordField, _T("edition"), false, true))
            {}
          else if (StringMatch(recordField, _T("howpublished"), false, true))
            {}
          else if (StringMatch(recordField, _T("note"), false, true) || StringMatch(recordField, _T("notes"), false, true))
            {}
          else if (StringMatch(recordField, _T("series"), false, true))
            {}
          else if (StringMatch(recordField, _T("type"), false, true))
            {}
          else if (StringMatch(recordField, _T("keywords"), false, true))
            {}
          else if (StringMatch(recordField, _T("editor"), false, true) || StringMatch(recordField, _T("editors"), false, true))
            bibEntry->editor= copystring(fieldValue);
          else if (StringMatch(recordField, _T("title"), false, true))
            bibEntry->title= copystring(fieldValue);
          else if (StringMatch(recordField, _T("booktitle"), false, true))
            bibEntry->booktitle= copystring(fieldValue);
          else if (StringMatch(recordField, _T("journal"), false, true))
            bibEntry->journal= copystring(fieldValue);
          else if (StringMatch(recordField, _T("volume"), false, true))
            bibEntry->volume= copystring(fieldValue);
          else if (StringMatch(recordField, _T("number"), false, true))
            bibEntry->number= copystring(fieldValue);
          else if (StringMatch(recordField, _T("year"), false, true))
            bibEntry->year= copystring(fieldValue);
          else if (StringMatch(recordField, _T("month"), false, true))
            bibEntry->month= copystring(fieldValue);
          else if (StringMatch(recordField, _T("pages"), false, true))
            bibEntry->pages= copystring(fieldValue);
          else if (StringMatch(recordField, _T("publisher"), false, true))
            bibEntry->publisher= copystring(fieldValue);
          else if (StringMatch(recordField, _T("address"), false, true))
            bibEntry->address= copystring(fieldValue);
          else if (StringMatch(recordField, _T("institution"), false, true) || StringMatch(recordField, _T("school"), false, true))
            bibEntry->institution= copystring(fieldValue);
          else if (StringMatch(recordField, _T("organization"), false, true) || StringMatch(recordField, _T("organisation"), false, true))
            bibEntry->organization= copystring(fieldValue);
          else if (StringMatch(recordField, _T("comment"), false, true) || StringMatch(recordField, _T("comments"), false, true))
            bibEntry->comment= copystring(fieldValue);
          else if (StringMatch(recordField, _T("annote"), false, true))
            bibEntry->comment= copystring(fieldValue);
          else if (StringMatch(recordField, _T("chapter"), false, true))
            bibEntry->chapter= copystring(fieldValue);
          else
          {
            wxSnprintf(buf, sizeof(buf), _T("Unrecognised bib field type %s at line %ld (%s)"), recordField, BibLine, filename);
            OnError(buf);
          }
        }
      }
      BibList.Append(recordKey, bibEntry);
      BibEatWhiteSpace(istr);
    }
  }
  return true;
}

void OutputBibItem(TexRef *ref, BibEntry *bib)
{
  Tex2RTFYield();

  OnMacro(ltNUMBEREDBIBITEM, 2, true);
  OnArgument(ltNUMBEREDBIBITEM, 1, true);
  TexOutput(ref->sectionNumber);
  OnArgument(ltNUMBEREDBIBITEM, 1, false);
  OnArgument(ltNUMBEREDBIBITEM, 2, true);

  TexOutput(_T(" "));
  OnMacro(ltBF, 1, true);
  OnArgument(ltBF, 1, true);
  if (bib->author)
    TexOutput(bib->author);
  OnArgument(ltBF, 1, false);
  OnMacro(ltBF, 1, false);
  if (bib->author && (wxStrlen(bib->author) > 0) && (bib->author[wxStrlen(bib->author) - 1] != '.'))
    TexOutput(_T(". "));
  else
    TexOutput(_T(" "));

  if (bib->year)
  {
    TexOutput(bib->year);
  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -