Merge pull request #1506 from akoeplinger/fix-paste-test
[mono.git] / mcs / class / Microsoft.Build.Tasks / Microsoft.Build.Tasks / CreateCSharpManifestResourceName.cs
1 //
2 // CreateCSharpManifestResourceName.cs
3 //
4 // Author:
5 //   Marek Sieradzki (marek.sieradzki@gmail.com)
6 //
7 // (C) 2005 Marek Sieradzki
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28
29 using System;
30 using System.IO;
31 using System.Text;
32 using Microsoft.Build.Framework;
33
34 namespace Microsoft.Build.Tasks {
35         public class CreateCSharpManifestResourceName : CreateManifestResourceName {
36                 
37                 public CreateCSharpManifestResourceName ()
38                 {
39                 }
40
41                 protected override bool IsSourceFile (string fileName)
42                 {
43                         return string.Equals (Path.GetExtension (fileName), ".cs", StringComparison.OrdinalIgnoreCase);
44                 }
45                 
46                 protected override string CreateManifestName (string fileName,
47                                                               string linkFileName,
48                                                               string rootNamespace,
49                                                               string dependentUponFileName,
50                                                               Stream binaryStream)
51                 {
52                         string filename_to_use = String.IsNullOrEmpty (linkFileName) ? fileName : linkFileName;
53                         if (String.IsNullOrEmpty (dependentUponFileName) || binaryStream == null)
54                                 return GetResourceIdFromFileName (filename_to_use, rootNamespace);
55
56                         string ns = null;
57                         string classname = null;
58
59                         using (StreamReader rdr = new StreamReader (binaryStream)) {
60                                 int numopen = 0;
61                                 while (true) {
62                                         string tok = GetNextToken (rdr);
63                                         if (tok == null)
64                                                 break;
65
66                                         if (tok == "@") {
67                                                 //Handle @namespace, @class
68                                                 GetNextToken (rdr);
69                                                 continue;
70                                         }
71
72                                         if (String.Compare (tok, "namespace", false) == 0)
73                                                 ns = GetNextToken (rdr);
74
75                                         if (tok == "{")
76                                                 numopen ++;
77
78                                         if (tok == "}") {
79                                                 numopen --;
80                                                 if (numopen == 0)
81                                                         ns = String.Empty;
82                                         }
83
84                                         if (tok == "class") {
85                                                 classname = GetNextToken (rdr);
86                                                 break;
87                                         }
88                                 }
89
90                                 if (classname == null)
91                                         return GetResourceIdFromFileName (filename_to_use, rootNamespace);
92
93                                 string culture, extn, only_filename;
94                                 if (AssignCulture.TrySplitResourceName (filename_to_use, out only_filename, out culture, out extn))
95                                         extn = "." + culture;
96                                 else
97                                         extn = String.Empty;
98
99                                 if (ns == null)
100                                         return classname + extn;
101                                 else
102                                         return ns + '.' + classname + extn;
103                         }
104                 }
105
106                 /* Special parser for C# files
107                  * Assumes that the file is compilable
108                  * skips comments,
109                  * skips strings "foo",
110                  * skips anything after a # , eg. #region, #if
111                  * Won't handle #if false etc kinda blocks*/
112                 static string GetNextToken (StreamReader sr)
113                 {
114                         StringBuilder sb = new StringBuilder ();
115
116                         while (true) {
117                                 int c = sr.Peek ();
118                                 if (c == -1)
119                                         return null;
120
121                                 if (c == '\r' || c == '\n') {
122                                         sr.ReadLine ();
123                                         if (sb.Length > 0)
124                                                 break;
125
126                                         continue;
127                                 }
128
129                                 if (c == '/') {
130                                         sr.Read ();
131
132                                         if (sr.Peek () == '*') {
133                                                 /* multi-line comment */
134                                                 sr.Read ();
135
136                                                 while (true) {
137                                                         int n = sr.Read ();
138                                                         if (n == -1)
139                                                                 break;
140                                                         if (n != '*')
141                                                                 continue;
142
143                                                         if (sr.Peek () == '/') {
144                                                                 /* End of multi-line comment */
145                                                                 if (sb.Length > 0) {
146                                                                         sr.Read ();
147                                                                         return sb.ToString ();
148                                                                 }
149                                                                 break;
150                                                         }
151                                                 }
152                                         } else if (sr.Peek () == '/') {
153                                                 //Single line comment, skip the rest of the line
154                                                 sr.ReadLine ();
155                                                 continue;
156                                         }
157                                 } else if (c == '"') {
158                                         /* String "foo" */
159                                         sr.Read ();
160                                         while (true) {
161                                                 int n = sr.Peek ();
162                                                 if (n == '\r' || n == '\n' || n == -1)
163                                                         throw new Exception ("String literal not closed");
164
165                                                 if (n == '"') {
166                                                         /* end of string */
167                                                         if (sb.Length > 0) {
168                                                                 sr.Read ();
169                                                                 return sb.ToString ();
170                                                         }
171
172                                                         break;
173                                                 }
174                                                 sr.Read ();
175                                         }
176                                 } else if (c == '#') {
177                                         //skip rest of the line
178                                         sr.ReadLine ();
179                                 } else {
180                                         if (Char.IsLetterOrDigit ((char) c) || c == '_' || c == '.') {
181                                                 sb.Append ((char) c);
182                                         } else {
183                                                 if (sb.Length > 0)
184                                                         break;
185
186                                                 if (c != ' ' && c != '\t') {
187                                                         sr.Read ();
188                                                         return ((char) c).ToString ();
189                                                 }
190                                         }
191                                 }
192
193                                 sr.Read ();
194                         }
195
196                         return sb.ToString ();
197                 }
198
199         }
200 }
201