OpenTTD Source  20241108-master-g80f628063a
textfile_gui.cpp File Reference

Implementation of textfile window. More...

#include "stdafx.h"
#include "core/backup_type.hpp"
#include "fileio_func.h"
#include "fontcache.h"
#include "gfx_type.h"
#include "gfx_func.h"
#include "string_func.h"
#include "textfile_gui.h"
#include "dropdown_type.h"
#include "dropdown_func.h"
#include "gfx_layout.h"
#include "debug.h"
#include "openttd.h"
#include "widgets/misc_widget.h"
#include "table/strings.h"
#include "table/control_codes.h"
#include <zlib.h>
#include <lzma.h>
#include <regex>
#include "safeguards.h"

Go to the source code of this file.

Enumerations

enum class  HyperlinkType { Internal , Web , File , Unknown }
 Types of link we support in markdown files. More...
 

Functions

static HyperlinkType ClassifyHyperlink (const std::string &destination, bool trusted)
 Classify the type of hyperlink the destination describes. More...
 
static std::string MakeAnchorSlug (const std::string &line)
 Create a valid slug for the anchor. More...
 
static std::vector< char > Gunzip (std::span< char > input)
 Do an in-memory gunzip operation. More...
 
static std::vector< char > Xunzip (std::span< char > input)
 Do an in-memory xunzip operation. More...
 
std::optional< std::string > GetTextfile (TextfileType type, Subdirectory dir, const std::string &filename)
 Search a textfile file next to the given content. More...
 

Variables

static constexpr NWidgetPart _nested_textfile_widgets []
 Widgets for the textfile window.
 
static WindowDesc _textfile_desc (WDP_CENTER, "textfile", 630, 460, WC_TEXTFILE, WC_NONE, 0, _nested_textfile_widgets)
 Window definition for the textfile window.
 
static const std::regex _markdown_link_regex {"\\[(.+?)\\]\\‍((.+?)\\‍)", std::regex_constants::ECMAScript | std::regex_constants::optimize}
 Regular expression that searches for Markdown links.
 

Detailed Description

Implementation of textfile window.

Definition in file textfile_gui.cpp.

Enumeration Type Documentation

◆ HyperlinkType

enum HyperlinkType
strong

Types of link we support in markdown files.

Enumerator
Internal 

Internal link, or "anchor" in HTML language.

Web 

Link to an external website.

File 

Link to a local file.

Unknown 

Unknown link.

Definition at line 170 of file textfile_gui.cpp.

Function Documentation

◆ ClassifyHyperlink()

static HyperlinkType ClassifyHyperlink ( const std::string &  destination,
bool  trusted 
)
static

Classify the type of hyperlink the destination describes.

Parameters
destinationThe hyperlink destination.
trustedWhether we trust the content of this file.
Returns
HyperlinkType The classification of the link.

Definition at line 184 of file textfile_gui.cpp.

References File, Internal, Unknown, and Web.

Referenced by TextfileWindow::FindHyperlinksInMarkdown(), and TextfileWindow::OnHyperlinkClick().

◆ GetTextfile()

std::optional<std::string> GetTextfile ( TextfileType  type,
Subdirectory  dir,
const std::string &  filename 
)

Search a textfile file next to the given content.

Parameters
typeThe type of the textfile to search for.
dirThe subdirectory to search in.
filenameThe filename of the content to look for.
Returns
The path to the textfile, nullptr otherwise.

Definition at line 833 of file textfile_gui.cpp.

References lengthof.

Referenced by GRFConfig::GetTextfile(), and ScriptConfig::GetTextfile().

◆ Gunzip()

static std::vector<char> Gunzip ( std::span< char >  input)
static

Do an in-memory gunzip operation.

This works on a raw deflate stream, or a file with gzip or zlib header.

Parameters
inputBuffer containing the input data.
Returns
Decompressed buffer.

When decompressing fails, an empty buffer is returned.

Definition at line 665 of file textfile_gui.cpp.

Referenced by TextfileWindow::LoadTextfile().

◆ MakeAnchorSlug()

static std::string MakeAnchorSlug ( const std::string &  line)
static

Create a valid slug for the anchor.

Parameters
lineThe line to create the slug for.
Returns
std::string The slug.

Definition at line 204 of file textfile_gui.cpp.

◆ Xunzip()

static std::vector<char> Xunzip ( std::span< char >  input)
static

Do an in-memory xunzip operation.

This works on a .xz or (legacy) .lzma file.

Parameters
inputBuffer containing the input data.
Returns
Decompressed buffer.

When decompressing fails, an empty buffer is returned.

Definition at line 705 of file textfile_gui.cpp.

Referenced by TextfileWindow::LoadTextfile().