Imported Upstream version 2.8.9
[platform/upstream/cmake.git] / Source / cmNewLineStyle.cxx
1 /*============================================================================
2   CMake - Cross Platform Makefile Generator
3   Copyright 2011 Kitware, Inc., Insight Software Consortium
4
5   Distributed under the OSI-approved BSD License (the "License");
6   see accompanying file Copyright.txt for details.
7
8   This software is distributed WITHOUT ANY WARRANTY; without even the
9   implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10   See the License for more information.
11 ============================================================================*/
12 #include "cmNewLineStyle.h"
13
14
15
16 cmNewLineStyle::cmNewLineStyle() : NewLineStyle(Invalid)
17 {
18 }
19
20
21 bool cmNewLineStyle::IsValid() const
22 {
23   return NewLineStyle != Invalid;
24 }
25
26
27 bool cmNewLineStyle::ReadFromArguments(const std::vector<std::string>& args,
28                                        std::string& errorString)
29 {
30   NewLineStyle = Invalid;
31
32   for (size_t i = 0; i< args.size(); i++)
33     {
34     if (args[i] == "NEWLINE_STYLE")
35       {
36       size_t const styleIndex = i + 1;
37       if (args.size() > styleIndex)
38         {
39         const std::string eol = args[styleIndex];
40         if (eol == "LF" || eol == "UNIX")
41           {
42           NewLineStyle = LF;
43           return true;
44           }
45         else if (eol == "CRLF" || eol == "WIN32" || eol == "DOS")
46           {
47           NewLineStyle = CRLF;
48           return true;
49           }
50         else
51           {
52           errorString = "NEWLINE_STYLE sets an unknown style, only LF, "
53                         "CRLF, UNIX, DOS, and WIN32 are supported";
54           return false;
55           }
56         }
57       else
58         {
59         errorString = "NEWLINE_STYLE must set a style: "
60                       "LF, CRLF, UNIX, DOS, or WIN32";
61         return false;
62         }
63       }
64     }
65   return true;
66 }
67
68
69 const std::string cmNewLineStyle::GetCharacters() const
70 {
71   switch (NewLineStyle)
72     {
73     case Invalid:
74       return "";
75     case LF:
76       return "\n";
77     case CRLF:
78       return "\r\n";
79     default:
80       ;
81     };
82   return "";
83 }
84
85
86 void cmNewLineStyle::SetStyle(Style style)
87 {
88   NewLineStyle = style;
89 }
90
91
92 cmNewLineStyle::Style cmNewLineStyle::GetStyle() const
93 {
94   return NewLineStyle;
95 }