files.cpp 18.8 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601
/*=============================================================================
    Copyright (c) 2002 2004 2006 Joel de Guzman
    Copyright (c) 2004 Eric Niebler
    http://spirit.sourceforge.net/

    Use, modification and distribution is subject to the Boost Software
    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
    http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#include "files.hpp"
#include <fstream>
#include <iterator>
#include <vector>
#include <list>
#include <boost/filesystem/fstream.hpp>
#include <boost/range/algorithm/transform.hpp>
#include <boost/range/algorithm/upper_bound.hpp>
#include <boost/unordered_map.hpp>
#include "for.hpp"

namespace quickbook
{
    namespace
    {
        boost::unordered_map<fs::path, file_ptr> files;
    }

    // Read the first few bytes in a file to see it starts with a byte order
    // mark. If it doesn't, then write the characters we've already read in.
    // Although, given how UTF-8 works, if we've read anything in, the files
    // probably broken.

    template <typename InputIterator, typename OutputIterator>
    bool check_bom(
        InputIterator& begin,
        InputIterator end,
        OutputIterator out,
        char const* chars,
        int length)
    {
        char const* ptr = chars;

        while (begin != end && *begin == *ptr) {
            ++begin;
            ++ptr;
            --length;
            if (length == 0) return true;
        }

        // Failed to match, so write the skipped characters to storage:
        while (chars != ptr)
            *out++ = *chars++;

        return false;
    }

    template <typename InputIterator, typename OutputIterator>
    std::string read_bom(
        InputIterator& begin, InputIterator end, OutputIterator out)
    {
        if (begin == end) return "";

        const char* utf8 = "\xef\xbb\xbf";
        const char* utf32be = "\0\0\xfe\xff";
        const char* utf32le = "\xff\xfe\0\0";

        unsigned char c = *begin;
        switch (c) {
        case 0xEF: { // UTF-8
            return check_bom(begin, end, out, utf8, 3) ? "UTF-8" : "";
        }
        case 0xFF: // UTF-16/UTF-32 little endian
            return !check_bom(begin, end, out, utf32le, 2)
                       ? ""
                       : check_bom(begin, end, out, utf32le + 2, 2) ? "UTF-32"
                                                                    : "UTF-16";
        case 0: // UTF-32 big endian
            return check_bom(begin, end, out, utf32be, 4) ? "UTF-32" : "";
        case 0xFE: // UTF-16 big endian
            return check_bom(begin, end, out, utf32be + 2, 2) ? "UTF-16" : "";
        default:
            return "";
        }
    }

    // Copy a string, converting mac and windows style newlines to unix
    // newlines.

    template <typename InputIterator, typename OutputIterator>
    void normalize(InputIterator begin, InputIterator end, OutputIterator out)
    {
        std::string encoding = read_bom(begin, end, out);

        if (encoding != "UTF-8" && encoding != "")
            throw load_error(encoding + " is not supported. Please use UTF-8.");

        while (begin != end) {
            if (*begin == '\r') {
                *out++ = '\n';
                ++begin;
                if (begin != end && *begin == '\n') ++begin;
            }
            else {
                *out++ = *begin++;
            }
        }
    }

    file_ptr load(fs::path const& filename, unsigned qbk_version)
    {
        boost::unordered_map<fs::path, file_ptr>::iterator pos =
            files.find(filename);

        if (pos == files.end()) {
            fs::ifstream in(filename, std::ios_base::in);

            if (!in) throw load_error("Could not open input file.");

            // Turn off white space skipping on the stream
            in.unsetf(std::ios::skipws);

            std::string source;
            normalize(
                std::istream_iterator<char>(in), std::istream_iterator<char>(),
                std::back_inserter(source));

            if (in.bad()) throw load_error("Error reading input file.");

            bool inserted;

            boost::tie(pos, inserted) = files.emplace(
                filename, new file(filename, source, qbk_version));

            assert(inserted);
        }

        return pos->second;
    }

    std::ostream& operator<<(std::ostream& out, file_position const& x)
    {
        return out << "line: " << x.line << ", column: " << x.column;
    }

    file_position relative_position(
        string_iterator begin, string_iterator iterator)
    {
        file_position pos;
        string_iterator line_begin = begin;

        while (begin != iterator) {
            if (*begin == '\r') {
                ++begin;
                ++pos.line;
                line_begin = begin;
            }
            else if (*begin == '\n') {
                ++begin;
                ++pos.line;
                line_begin = begin;
                if (begin == iterator) break;
                if (*begin == '\r') {
                    ++begin;
                    line_begin = begin;
                }
            }
            else {
                ++begin;
            }
        }

        pos.column = iterator - line_begin + 1;
        return pos;
    }

    file_position file::position_of(string_iterator iterator) const
    {
        return relative_position(source().begin(), iterator);
    }

    // Mapped files.

    struct mapped_file_section
    {
        enum section_types
        {
            normal,
            empty,
            indented
        };

        std::string::size_type original_pos;
        std::string::size_type our_pos;
        section_types section_type;

        explicit mapped_file_section(
            std::string::size_type original_pos_,
            std::string::size_type our_pos_,
            section_types section_type_ = normal)
            : original_pos(original_pos_)
            , our_pos(our_pos_)
            , section_type(section_type_)
        {
        }
    };

    struct mapped_section_original_cmp
    {
        bool operator()(
            mapped_file_section const& x, mapped_file_section const& y)
        {
            return x.original_pos < y.original_pos;
        }

        bool operator()(
            mapped_file_section const& x, std::string::size_type const& y)
        {
            return x.original_pos < y;
        }

        bool operator()(
            std::string::size_type const& x, mapped_file_section const& y)
        {
            return x < y.original_pos;
        }
    };

    struct mapped_section_pos_cmp
    {
        bool operator()(
            mapped_file_section const& x, mapped_file_section const& y)
        {
            return x.our_pos < y.our_pos;
        }

        bool operator()(
            mapped_file_section const& x, std::string::size_type const& y)
        {
            return x.our_pos < y;
        }

        bool operator()(
            std::string::size_type const& x, mapped_file_section const& y)
        {
            return x < y.our_pos;
        }
    };

    struct mapped_file : file
    {
        explicit mapped_file(file_ptr original_)
            : file(*original_, std::string())
            , original(original_)
            , mapped_sections()
        {
        }

        file_ptr original;
        std::vector<mapped_file_section> mapped_sections;

        void add_empty_mapped_file_section(string_iterator pos)
        {
            std::string::size_type original_pos =
                pos - original->source().begin();

            if (mapped_sections.empty() ||
                mapped_sections.back().section_type !=
                    mapped_file_section::empty ||
                mapped_sections.back().original_pos != original_pos) {
                mapped_sections.push_back(mapped_file_section(
                    original_pos, source().size(), mapped_file_section::empty));
            }
        }

        void add_mapped_file_section(string_iterator pos)
        {
            mapped_sections.push_back(mapped_file_section(
                pos - original->source().begin(), source().size()));
        }

        void add_indented_mapped_file_section(string_iterator pos)
        {
            mapped_sections.push_back(mapped_file_section(
                pos - original->source().begin(), source().size(),
                mapped_file_section::indented));
        }

        std::string::size_type to_original_pos(
            std::vector<mapped_file_section>::const_iterator section,
            std::string::size_type pos) const
        {
            switch (section->section_type) {
            case mapped_file_section::normal:
                return pos - section->our_pos + section->original_pos;

            case mapped_file_section::empty:
                return section->original_pos;

            case mapped_file_section::indented: {
                // Will contain the start of the current line.
                quickbook::string_view::size_type our_line = section->our_pos;

                // Will contain the number of lines in the block before
                // the current line.
                unsigned newline_count = 0;

                for (quickbook::string_view::size_type i = section->our_pos;
                     i != pos; ++i) {
                    if (source()[i] == '\n') {
                        our_line = i + 1;
                        ++newline_count;
                    }
                }

                // The start of the line in the original source.
                quickbook::string_view::size_type original_line =
                    section->original_pos;

                while (newline_count > 0) {
                    if (original->source()[original_line] == '\n')
                        --newline_count;
                    ++original_line;
                }

                // The start of line content (i.e. after indentation).
                our_line = skip_indentation(source(), our_line);

                // The position is in the middle of indentation, so
                // just return the start of the whitespace, which should
                // be good enough.
                if (our_line > pos) return original_line;

                original_line =
                    skip_indentation(original->source(), original_line);

                // Confirm that we are actually in the same position.
                assert(original->source()[original_line] == source()[our_line]);

                // Calculate the position
                return original_line + (pos - our_line);
            }
            default:
                assert(false);
                return section->original_pos;
            }
        }

        std::vector<mapped_file_section>::const_iterator find_section(
            string_iterator pos) const
        {
            std::vector<mapped_file_section>::const_iterator section =
                boost::upper_bound(
                    mapped_sections,
                    std::string::size_type(pos - source().begin()),
                    mapped_section_pos_cmp());
            assert(section != mapped_sections.begin());
            --section;

            return section;
        }

        virtual file_position position_of(string_iterator) const;

      private:
        static std::string::size_type skip_indentation(
            quickbook::string_view src, std::string::size_type i)
        {
            while (i != src.size() && (src[i] == ' ' || src[i] == '\t'))
                ++i;
            return i;
        }
    };

    namespace
    {
        std::list<mapped_file> mapped_files;
    }

    struct mapped_file_builder_data
    {
        mapped_file_builder_data() { reset(); }
        void reset() { new_file.reset(); }

        boost::intrusive_ptr<mapped_file> new_file;
    };

    mapped_file_builder::mapped_file_builder() : data(0) {}
    mapped_file_builder::~mapped_file_builder() { delete data; }

    void mapped_file_builder::start(file_ptr f)
    {
        if (!data) {
            data = new mapped_file_builder_data;
        }

        assert(!data->new_file);
        data->new_file = new mapped_file(f);
    }

    file_ptr mapped_file_builder::release()
    {
        file_ptr r = data->new_file;
        data->reset();
        return r;
    }

    void mapped_file_builder::clear() { data->reset(); }

    bool mapped_file_builder::empty() const
    {
        return data->new_file->source().empty();
    }

    mapped_file_builder::pos_type mapped_file_builder::get_pos() const
    {
        return data->new_file->source().size();
    }

    void mapped_file_builder::add_at_pos(quickbook::string_view x, iterator pos)
    {
        data->new_file->add_empty_mapped_file_section(pos);
        data->new_file->source_.append(x.begin(), x.end());
    }

    void mapped_file_builder::add(quickbook::string_view x)
    {
        data->new_file->add_mapped_file_section(x.begin());
        data->new_file->source_.append(x.begin(), x.end());
    }

    void mapped_file_builder::add(mapped_file_builder const& x)
    {
        add(x, 0, x.data->new_file->source_.size());
    }

    void mapped_file_builder::add(
        mapped_file_builder const& x, pos_type begin, pos_type end)
    {
        assert(data->new_file->original == x.data->new_file->original);
        assert(begin <= x.data->new_file->source_.size());
        assert(end <= x.data->new_file->source_.size());

        if (begin != end) {
            std::vector<mapped_file_section>::const_iterator i =
                x.data->new_file->find_section(
                    x.data->new_file->source().begin() + begin);

            std::string::size_type size = data->new_file->source_.size();

            data->new_file->mapped_sections.push_back(mapped_file_section(
                x.data->new_file->to_original_pos(i, begin), size,
                i->section_type));

            for (++i; i != x.data->new_file->mapped_sections.end() &&
                      i->our_pos < end;
                 ++i) {
                data->new_file->mapped_sections.push_back(mapped_file_section(
                    i->original_pos, i->our_pos - begin + size,
                    i->section_type));
            }

            data->new_file->source_.append(
                x.data->new_file->source_.begin() + begin,
                x.data->new_file->source_.begin() + end);
        }
    }

    quickbook::string_view::size_type indentation_count(
        quickbook::string_view x)
    {
        unsigned count = 0;

        QUICKBOOK_FOR (auto c, x) {
            switch (c) {
            case ' ':
                ++count;
                break;
            case '\t':
                // hardcoded tab to 4 for now
                count = count - (count % 4) + 4;
                break;
            default:
                assert(false);
            }
        }

        return count;
    }

    void mapped_file_builder::unindent_and_add(quickbook::string_view x)
    {
        // I wanted to do everything using a string_ref, but unfortunately
        // they don't have all the overloads used in here. So...
        std::string const program(x.begin(), x.end());

        // Erase leading blank lines and newlines:
        std::string::size_type text_start =
            program.find_first_not_of(" \t\r\n");
        if (text_start == std::string::npos) return;

        text_start = program.find_last_of("\r\n", text_start);
        text_start = text_start == std::string::npos ? 0 : text_start + 1;

        assert(text_start < program.size());

        // Get the first line indentation
        std::string::size_type indent =
            program.find_first_not_of(" \t", text_start) - text_start;
        quickbook::string_view::size_type full_indent = indentation_count(
            quickbook::string_view(&program[text_start], indent));

        std::string::size_type pos = text_start;

        // Calculate the minimum indent from the rest of the lines
        // Detecting a mix of spaces and tabs.
        while (std::string::npos !=
               (pos = program.find_first_of("\r\n", pos))) {
            pos = program.find_first_not_of("\r\n", pos);
            if (std::string::npos == pos) break;

            std::string::size_type n = program.find_first_not_of(" \t", pos);
            if (n == std::string::npos) break;

            char ch = program[n];
            if (ch == '\r' || ch == '\n') continue; // ignore empty lines

            indent = (std::min)(indent, n - pos);
            full_indent = (std::min)(
                full_indent, indentation_count(quickbook::string_view(
                                 &program[pos], n - pos)));
        }

        // Detect if indentation is mixed.
        bool mixed_indentation = false;
        quickbook::string_view first_indent(&program[text_start], indent);
        pos = text_start;

        while (std::string::npos !=
               (pos = program.find_first_of("\r\n", pos))) {
            pos = program.find_first_not_of("\r\n", pos);
            if (std::string::npos == pos) break;

            std::string::size_type n = program.find_first_not_of(" \t", pos);
            if (n == std::string::npos || n - pos < indent) continue;

            if (quickbook::string_view(&program[pos], indent) != first_indent) {
                mixed_indentation = true;
                break;
            }
        }

        // Trim white spaces from column 0..indent
        std::string unindented_program;
        std::string::size_type copy_start = text_start;
        pos = text_start;

        do {
            if (std::string::npos ==
                (pos = program.find_first_not_of("\r\n", pos)))
                break;

            unindented_program.append(
                program.begin() + copy_start, program.begin() + pos);
            copy_start = pos;

            // Find the end of the indentation.
            std::string::size_type next = program.find_first_not_of(" \t", pos);
            if (next == std::string::npos) next = program.size();

            if (mixed_indentation) {
                string_view::size_type length = indentation_count(
                    quickbook::string_view(&program[pos], next - pos));

                if (length > full_indent) {
                    std::string new_indentation(length - full_indent, ' ');
                    unindented_program.append(new_indentation);
                }

                copy_start = next;
            }
            else {
                copy_start = (std::min)(pos + indent, next);
            }

            pos = next;
        } while (std::string::npos !=
                 (pos = program.find_first_of("\r\n", pos)));

        unindented_program.append(program.begin() + copy_start, program.end());

        data->new_file->add_indented_mapped_file_section(x.begin());
        data->new_file->source_.append(unindented_program);
    }

    file_position mapped_file::position_of(string_iterator pos) const
    {
        return original->position_of(
            original->source().begin() +
            to_original_pos(find_section(pos), pos - source().begin()));
    }
}