[xbuild] Fix warnings.
[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 #if NET_2_0
29
30 using System;
31 using System.IO;
32 using System.Text;
33 using Microsoft.Build.Framework;
34
35 namespace Microsoft.Build.Tasks {
36         public class CreateCSharpManifestResourceName : CreateManifestResourceName {
37                 
38                 public CreateCSharpManifestResourceName ()
39                 {
40                 }
41
42                 protected override bool IsSourceFile (string fileName)
43                 {
44                         return Path.GetExtension (fileName).ToLower () == ".cs";
45                 }
46                 
47                 protected override string CreateManifestName (string fileName,
48                                                               string linkFileName,
49                                                               string rootNamespace,
50                                                               string dependentUponFileName,
51                                                               Stream binaryStream)
52                 {
53                         string filename_to_use = String.IsNullOrEmpty (linkFileName) ? fileName : linkFileName;
54                         if (String.IsNullOrEmpty (dependentUponFileName) || binaryStream == null)
55                                 return GetResourceIdFromFileName (filename_to_use, rootNamespace);
56
57                         string ns = null;
58                         string classname = null;
59
60                         using (StreamReader rdr = new StreamReader (binaryStream)) {
61                                 int numopen = 0;
62                                 while (true) {
63                                         string tok = GetNextToken (rdr);
64                                         if (tok == null)
65                                                 break;
66
67                                         if (tok == "@") {
68                                                 //Handle @namespace, @class
69                                                 GetNextToken (rdr);
70                                                 continue;
71                                         }
72
73                                         if (String.Compare (tok, "namespace", false) == 0)
74                                                 ns = GetNextToken (rdr);
75
76                                         if (tok == "{")
77                                                 numopen ++;
78
79                                         if (tok == "}") {
80                                                 numopen --;
81                                                 if (numopen == 0)
82                                                         ns = String.Empty;
83                                         }
84
85                                         if (tok == "class") {
86                                                 classname = GetNextToken (rdr);
87                                                 break;
88                                         }
89                                 }
90
91                                 if (classname == null)
92                                         return GetResourceIdFromFileName (filename_to_use, rootNamespace);
93
94                                 string culture, extn, only_filename;
95                                 if (AssignCulture.TrySplitResourceName (filename_to_use, out only_filename, out culture, out extn))
96                                         extn = "." + culture;
97                                 else
98                                         extn = String.Empty;
99
100                                 if (ns == null)
101                                         return classname + extn;
102                                 else
103                                         return ns + '.' + classname + extn;
104                         }
105                 }
106
107                 /* Special parser for C# files
108                  * Assumes that the file is compilable
109                  * skips comments,
110                  * skips strings "foo",
111                  * skips anything after a # , eg. #region, #if
112                  * Won't handle #if false etc kinda blocks*/
113                 static string GetNextToken (StreamReader sr)
114                 {
115                         StringBuilder sb = new StringBuilder ();
116
117                         while (true) {
118                                 int c = sr.Peek ();
119                                 if (c == -1)
120                                         return null;
121
122                                 if (c == '\r' || c == '\n') {
123                                         sr.ReadLine ();
124                                         if (sb.Length > 0)
125                                                 break;
126
127                                         continue;
128                                 }
129
130                                 if (c == '/') {
131                                         sr.Read ();
132
133                                         if (sr.Peek () == '*') {
134                                                 /* multi-line comment */
135                                                 sr.Read ();
136
137                                                 while (true) {
138                                                         int n = sr.Read ();
139                                                         if (n == -1)
140                                                                 break;
141                                                         if (n != '*')
142                                                                 continue;
143
144                                                         if (sr.Peek () == '/') {
145                                                                 /* End of multi-line comment */
146                                                                 if (sb.Length > 0) {
147                                                                         sr.Read ();
148                                                                         return sb.ToString ();
149                                                                 }
150                                                                 break;
151                                                         }
152                                                 }
153                                         } else if (sr.Peek () == '/') {
154                                                 //Single line comment, skip the rest of the line
155                                                 sr.ReadLine ();
156                                                 continue;
157                                         }
158                                 } else if (c == '"') {
159                                         /* String "foo" */
160                                         sr.Read ();
161                                         while (true) {
162                                                 int n = sr.Peek ();
163                                                 if (n == '\r' || n == '\n' || n == -1)
164                                                         throw new Exception ("String literal not closed");
165
166                                                 if (n == '"') {
167                                                         /* end of string */
168                                                         if (sb.Length > 0) {
169                                                                 sr.Read ();
170                                                                 return sb.ToString ();
171                                                         }
172
173                                                         break;
174                                                 }
175                                                 sr.Read ();
176                                         }
177                                 } else if (c == '#') {
178                                         //skip rest of the line
179                                         sr.ReadLine ();
180                                 } else {
181                                         if (Char.IsLetterOrDigit ((char) c) || c == '_' || c == '.') {
182                                                 sb.Append ((char) c);
183                                         } else {
184                                                 if (sb.Length > 0)
185                                                         break;
186
187                                                 if (c != ' ' && c != '\t') {
188                                                         sr.Read ();
189                                                         return ((char) c).ToString ();
190                                                 }
191                                         }
192                                 }
193
194                                 sr.Read ();
195                         }
196
197                         return sb.ToString ();
198                 }
199
200         }
201 }
202
203 #endif