lnav/src/text_format.hh

110 lines
3.5 KiB
C++
Raw Normal View History

/**
* Copyright (c) 2017, Timothy Stack
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Timothy Stack nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2022-03-16 22:38:08 +00:00
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @file text_format.hh
*/
#ifndef text_format_hh
#define text_format_hh
#include <string>
2022-03-16 22:38:08 +00:00
#include <sys/types.h>
2022-05-23 03:44:18 +00:00
#include "base/intern_string.hh"
2021-03-06 00:17:28 +00:00
#include "fmt/format.h"
2022-05-23 03:44:18 +00:00
#include "ghc/filesystem.hpp"
2021-03-06 00:17:28 +00:00
enum class text_format_t {
TF_UNKNOWN,
2021-03-06 00:17:28 +00:00
TF_LOG,
TF_PYTHON,
2020-08-28 05:24:46 +00:00
TF_RUST,
2021-03-06 00:17:28 +00:00
TF_JAVA,
TF_C_LIKE,
TF_SQL,
2021-03-06 00:17:28 +00:00
TF_XML,
TF_JSON,
2022-05-23 03:44:18 +00:00
TF_MARKDOWN,
};
2021-03-06 00:17:28 +00:00
namespace fmt {
template<>
struct formatter<text_format_t> : formatter<string_view> {
template<typename FormatContext>
2022-03-16 22:38:08 +00:00
auto format(text_format_t tf, FormatContext& ctx)
2021-03-06 00:17:28 +00:00
{
string_view name = "unknown";
switch (tf) {
case text_format_t::TF_UNKNOWN:
name = "application/octet-stream";
break;
case text_format_t::TF_LOG:
name = "text/log";
break;
case text_format_t::TF_PYTHON:
name = "text/python";
break;
case text_format_t::TF_RUST:
name = "text/rust";
break;
case text_format_t::TF_JAVA:
name = "text/java";
break;
case text_format_t::TF_C_LIKE:
name = "text/c";
break;
case text_format_t::TF_SQL:
name = "application/sql";
break;
case text_format_t::TF_XML:
name = "text/xml";
break;
case text_format_t::TF_JSON:
name = "application/json";
break;
2022-05-23 03:44:18 +00:00
case text_format_t::TF_MARKDOWN:
name = "text/markdown";
break;
2021-03-06 00:17:28 +00:00
}
return formatter<string_view>::format(name, ctx);
}
};
2022-03-16 22:38:08 +00:00
} // namespace fmt
2021-03-06 00:17:28 +00:00
/**
* Try to detect the format of the given text file fragment.
*
* @return The detected format.
*/
2022-05-23 03:44:18 +00:00
text_format_t detect_text_format(string_fragment sf,
nonstd::optional<ghc::filesystem::path> path
= nonstd::nullopt);
#endif