1 /**
2  * Copyright (c) 2017, Timothy Stack
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * * Neither the name of Timothy Stack nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * @file text_format.hh
30  */
31 
32 #ifndef text_format_hh
33 #define text_format_hh
34 
35 #include <sys/types.h>
36 
37 #include <string>
38 
39 #include "fmt/format.h"
40 
41 enum class text_format_t {
42     TF_UNKNOWN,
43     TF_LOG,
44     TF_PYTHON,
45     TF_RUST,
46     TF_JAVA,
47     TF_C_LIKE,
48     TF_SQL,
49     TF_XML,
50     TF_JSON,
51 };
52 
53 
54 namespace fmt {
55 template<>
56 struct formatter<text_format_t> : formatter<string_view> {
57     template<typename FormatContext>
formatfmt::formatter58     auto format(text_format_t tf, FormatContext &ctx)
59     {
60         string_view name = "unknown";
61         switch (tf) {
62             case text_format_t::TF_UNKNOWN:
63                 name = "application/octet-stream";
64                 break;
65             case text_format_t::TF_LOG:
66                 name = "text/log";
67                 break;
68             case text_format_t::TF_PYTHON:
69                 name = "text/python";
70                 break;
71             case text_format_t::TF_RUST:
72                 name = "text/rust";
73                 break;
74             case text_format_t::TF_JAVA:
75                 name = "text/java";
76                 break;
77             case text_format_t::TF_C_LIKE:
78                 name = "text/c";
79                 break;
80             case text_format_t::TF_SQL:
81                 name = "application/sql";
82                 break;
83             case text_format_t::TF_XML:
84                 name = "text/xml";
85                 break;
86             case text_format_t::TF_JSON:
87                 name = "application/json";
88                 break;
89         }
90         return formatter<string_view>::format(name, ctx);
91     }
92 };
93 }
94 
95 /**
96  * Try to detect the format of the given text file fragment.
97  *
98  * @param str The text to scan.
99  * @param len The length of the 'str' buffer.
100  * @return The detected format.
101  */
102 text_format_t detect_text_format(const char *str, size_t len);
103 
detect_text_format(const std::string & str)104 inline text_format_t detect_text_format(const std::string &str) {
105     return detect_text_format(str.c_str(), str.length());
106 }
107 
108 #endif
109