summaryrefslogtreecommitdiffstats
path: root/Source/cmNewLineStyle.cxx
blob: 28baeb69d05bbe8af3d2bec881ccc8f522d4af38 (plain)
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
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
#include "cmNewLineStyle.h"

#include <cstddef>

cmNewLineStyle::cmNewLineStyle() = default;

bool cmNewLineStyle::IsValid() const
{
  return this->NewLineStyle != Invalid;
}

bool cmNewLineStyle::ReadFromArguments(const std::vector<std::string>& args,
                                       std::string& errorString)
{
  this->NewLineStyle = Invalid;

  for (size_t i = 0; i < args.size(); i++) {
    if (args[i] == "NEWLINE_STYLE") {
      size_t const styleIndex = i + 1;
      if (args.size() > styleIndex) {
        std::string const& eol = args[styleIndex];
        if (eol == "LF" || eol == "UNIX") {
          this->NewLineStyle = LF;
          return true;
        }
        if (eol == "CRLF" || eol == "WIN32" || eol == "DOS") {
          this->NewLineStyle = CRLF;
          return true;
        }
        errorString = "NEWLINE_STYLE sets an unknown style, only LF, "
                      "CRLF, UNIX, DOS, and WIN32 are supported";
        return false;
      }
      errorString = "NEWLINE_STYLE must set a style: "
                    "LF, CRLF, UNIX, DOS, or WIN32";
      return false;
    }
  }
  return true;
}

std::string cmNewLineStyle::GetCharacters() const
{
  switch (this->NewLineStyle) {
    case Invalid:
      return "";
    case LF:
      return "\n";
    case CRLF:
      return "\r\n";
  }
  return "";
}

void cmNewLineStyle::SetStyle(Style style)
{
  this->NewLineStyle = style;
}

cmNewLineStyle::Style cmNewLineStyle::GetStyle() const
{
  return this->NewLineStyle;
}