blob: 0e330331944072e25cba5047ff4a3cfe1ca487a6 [file] [edit]
// Copyright 2026 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <string>
#include <vector>
#include "string_piece.h"
struct DiskInterface;
/// Remove both leading and trailing whitespace from a StringPiece.
StringPiece StripStringPiece(StringPiece input);
/// Read input text as a StringPiece, and iterate over its lines.
/// Usage is:
/// 1. Create instance.
/// 2. Call GetNextLine() in a loop, until it returns false.
struct TextLineSplitter {
/// Constructor takes input text as argument.
TextLineSplitter(StringPiece content);
/// Get next line of input. On success, set |*line| and return true.
/// On end of text, return false.
bool GetNextLine(StringPiece* line);
private:
const char* p_ = nullptr;
const char* limit_ = nullptr;
};
/// Convenience class to read a text file line-by-line.
/// Empty lines are ignored. Usage is:
///
/// 1. Create instance.
/// 2. Call Reset() to read an input file.
/// 3. Call lines() to see the lines read from the input.
/// these do not include line terminators.
///
struct TextFileParser {
/// Constructor takes a DiskInterface instance.
TextFileParser(DiskInterface&);
/// Read the input file and parse it into lines.
/// On success, returns true and populates lines_ with the lines from the
/// input file. On failure, returns false and sets error to a description of
/// the error.
bool Reset(const char* input_file, std::string* error);
/// Returns the lines read from the input file.
/// Always empty if Reset() returned false.
const std::vector<StringPiece>& lines() const { return lines_; }
/// Return the content of the input file.
/// Always empty if Reset() returned false.
StringPiece content() const { return content_; }
private:
DiskInterface& disk_interface_;
std::string content_;
std::vector<StringPiece> lines_;
};