~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric-updates

« back to all changes in this revision

Viewing changes to src/addins/CBinding/Project/Package.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-18 08:40:51 UTC
  • mfrom: (1.2.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090218084051-gh8m6ukvokbwj7cf
Tags: 1.9.2+dfsg-1ubuntu1
* Merge from Debian Experimental (LP: #330519), remaining Ubuntu changes:
  + debian/control:
    - Update for Gnome# 2.24
    - Add libmono-cairo1.0-cil to build-deps to fool pkg-config check

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//
 
2
// ProjectPackage.cs: A pkg-config package
 
3
//
 
4
// Authors:
 
5
//   Marcos David Marin Amador <MarcosMarin@gmail.com>
 
6
//
 
7
// Copyright (C) 2007 Marcos David Marin Amador
 
8
//
 
9
//
 
10
// This source code is licenced under The MIT License:
 
11
//
 
12
// Permission is hereby granted, free of charge, to any person obtaining
 
13
// a copy of this software and associated documentation files (the
 
14
// "Software"), to deal in the Software without restriction, including
 
15
// without limitation the rights to use, copy, modify, merge, publish,
 
16
// distribute, sublicense, and/or sell copies of the Software, and to
 
17
// permit persons to whom the Software is furnished to do so, subject to
 
18
// the following conditions:
 
19
// 
 
20
// The above copyright notice and this permission notice shall be
 
21
// included in all copies or substantial portions of the Software.
 
22
// 
 
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
24
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
25
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
26
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
27
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
28
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
29
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
30
//
 
31
 
 
32
using System;
 
33
using System.IO;
 
34
using System.Text;
 
35
using System.Collections.Generic;
 
36
 
 
37
using Mono.Addins;
 
38
 
 
39
using MonoDevelop.Core;
 
40
using MonoDevelop.Core.Serialization;
 
41
 
 
42
namespace CBinding
 
43
{
 
44
        public class Package
 
45
        {
 
46
                [ItemProperty ("file")]
 
47
                private string file;
 
48
                
 
49
                [ItemProperty ("name")]
 
50
                private string name;
 
51
                
 
52
                [ItemProperty ("IsProject")]
 
53
                private bool is_project;
 
54
 
 
55
                private string description;
 
56
                private string version;
 
57
                private List<string> requires = new List<string>();
 
58
                private List<string> libPaths = new List<string>();
 
59
                private List<string> libs = new List<string>();
 
60
                private List<string> cflags = new List<string>();
 
61
                private Dictionary<string, string> vars = new Dictionary<string,string> ();
 
62
                
 
63
                public Package (string file)
 
64
                {
 
65
                        this.file = file;
 
66
                        this.is_project = false;
 
67
                        
 
68
                        ParsePackageEssentials ();
 
69
                }
 
70
                
 
71
                public Package (CProject project)
 
72
                {
 
73
                        name = project.Name;
 
74
                        file = Path.Combine (project.BaseDirectory, name + ".md.pc");
 
75
                        is_project = true;
 
76
                        
 
77
                        ParsePackageEssentials ();
 
78
                }
 
79
                
 
80
                public Package ()
 
81
                {
 
82
                }
 
83
                
 
84
                void ParsePackageEssentials ()
 
85
                {
 
86
                        string line;
 
87
                        
 
88
                        using (StreamReader reader = new StreamReader (file)) {
 
89
                                while ((line = reader.ReadLine ()) != null) {
 
90
                                        if (line.StartsWith ("Name")) {
 
91
                                                name = line.Split (':')[1].TrimStart ();
 
92
                                                continue;
 
93
                                        }
 
94
                                        
 
95
                                        if (line.StartsWith ("Version")) {
 
96
                                                version = line.Split (':')[1].TrimStart ();
 
97
                                        }
 
98
                                }
 
99
                        }
 
100
                }
 
101
                
 
102
                bool parsed = false;
 
103
                public void ParsePackage ()
 
104
                {
 
105
                        if (parsed)
 
106
                                return;
 
107
                        
 
108
                        string line;
 
109
                        
 
110
                        using (StreamReader reader = new StreamReader (file)) {
 
111
                                while ((line = reader.ReadLine ()) != null) {
 
112
                                        if (line.StartsWith ("#"))
 
113
                                            continue;
 
114
                                            
 
115
                                        if (line.IndexOf ('=') >= 0)
 
116
                                                ParseVar (line);
 
117
                                        
 
118
                                        if (line.IndexOf (':') >= 0)
 
119
                                                ParseProperty (line);
 
120
                                }
 
121
                        }
 
122
                        
 
123
                        parsed = true;
 
124
                }
 
125
                
 
126
                void ParseVar (string line)
 
127
                {
 
128
                        int i = line.IndexOf ('=');
 
129
                        string key = line.Substring (0, i);
 
130
                        string value = line.Substring (i+1, line.Length - i-1).Trim ();
 
131
                        string parsedValue = StringParserService.Parse (value, CustomTags ());
 
132
                        
 
133
                        vars.Add (key, parsedValue);
 
134
                }
 
135
                
 
136
                void ParseProperty (string line)
 
137
                {
 
138
                        int i = line.IndexOf (':');
 
139
                        string key = line.Substring (0, i);
 
140
                        string value = StringParserService.Parse (line.Substring (i+1, line.Length - i-1).Trim (), CustomTags ());
 
141
                        
 
142
                        if (value.Length <= 0)
 
143
                                return;
 
144
                        
 
145
                        switch (key) {
 
146
                        case "Name":
 
147
                                name = value;
 
148
                                break;
 
149
                        case "Description":;
 
150
                                description = ProcessDescription (value);
 
151
                                break;
 
152
                        case "Version":
 
153
                                version = value;
 
154
                                break;
 
155
                        case "Requires":
 
156
                                ParseRequires (value);
 
157
                                break;
 
158
                        case "Libs":
 
159
                                ParseLibs (value);
 
160
                                break;
 
161
                        case "Cflags":
 
162
                                ParseCFlags (value);
 
163
                                break;
 
164
                        }
 
165
                }
 
166
                
 
167
                void ParseRequires (string reqsline)
 
168
                {
 
169
                        string[] splitted = reqsline.Split (' ');
 
170
                        
 
171
                        foreach (string str in splitted) {
 
172
                                if (str.Trim () == string.Empty)
 
173
                                        continue;
 
174
                                Requires.Add (str);
 
175
                        }
 
176
                }
 
177
                
 
178
                void ParseLibs (string libline)
 
179
                {
 
180
                        int i = 0;
 
181
                        string lib;
 
182
                        
 
183
                        while (true) {
 
184
                                i = libline.IndexOf ('-', i);
 
185
                                
 
186
                                if (i < 0)
 
187
                                        break;
 
188
                                
 
189
                                int count = 0;
 
190
                                
 
191
                                while (libline.Length > (count+i+2) && libline[count+i+2] != ' ')
 
192
                                        count++;
 
193
                                
 
194
                                lib = libline.Substring (i+2, count);
 
195
                                
 
196
                                if (libline[i+1] == 'L') {
 
197
                                        libPaths.Add (lib);
 
198
                                } else if (libline[i+1] == 'l') {
 
199
                                        libs.Add (lib);
 
200
                                }
 
201
                                
 
202
                                i++;
 
203
                        }
 
204
                }
 
205
                
 
206
                void ParseCFlags (string cflagsline)
 
207
                {
 
208
                        string[] splitted = cflagsline.Split (' ');
 
209
                        
 
210
                        foreach (string str in splitted) {
 
211
                                if (str.Trim () == string.Empty)
 
212
                                        continue;
 
213
                                CFlags.Add (str);
 
214
                        }
 
215
                }
 
216
                
 
217
                /// <summary>
 
218
                /// Insert '\n's to make sure string isn't too long.
 
219
                /// </summary>
 
220
                /// <param name="desc">
 
221
                /// The unprocessed description.
 
222
                /// A <see cref="System.String"/>
 
223
                /// </param>
 
224
                /// <returns>
 
225
                /// The processed description.
 
226
                /// A <see cref="System.String"/>
 
227
                /// </returns>
 
228
                string ProcessDescription (string desc)
 
229
                {
 
230
                        int length = 80;
 
231
                        
 
232
                        if (desc.Length <= length)
 
233
                                return desc;                    
 
234
                        
 
235
                        StringBuilder builder = new StringBuilder (desc);
 
236
                        int i = 0;
 
237
                        int lines = 1;
 
238
                        
 
239
                        while (i < desc.Length) {
 
240
                                i++;
 
241
                                
 
242
                                if (i > lines * length) {
 
243
                                        lines++;
 
244
                                        
 
245
                                        do {
 
246
                                                i--;
 
247
                                        } while (desc [i] != ' ');
 
248
                                        
 
249
                                        builder.Replace (' ', '\n', i, 1);
 
250
                                }
 
251
                        }
 
252
                        
 
253
                        return builder.ToString ();
 
254
                }
 
255
                
 
256
                string[,] CustomTags ()
 
257
                {
 
258
                        string[,] customTags = new string[vars.Count, 2];
 
259
                        int i = 0;
 
260
                        
 
261
                        foreach (KeyValuePair<string, string> kvp in vars) {
 
262
                                customTags[i, 0] = kvp.Key;
 
263
                                customTags[i, 1] = kvp.Value;
 
264
                                i++;
 
265
                        }
 
266
                        
 
267
                        return customTags;
 
268
                }
 
269
                
 
270
                public string File {
 
271
                        get { return file; }
 
272
                        set { file = value; }
 
273
                }
 
274
                
 
275
                public string Name {
 
276
                        get { return name; }
 
277
                        set { name = value; }
 
278
                }
 
279
                
 
280
                public bool IsProject {
 
281
                        get { return is_project; }
 
282
                        set { is_project = value; }
 
283
                }
 
284
                
 
285
                public string Version {
 
286
                        get { return version; }
 
287
                        set { version = value; }
 
288
                }
 
289
 
 
290
                public string Description {
 
291
                        get { return description; }
 
292
                }
 
293
 
 
294
                public List<string> Requires {
 
295
                        get { return requires; }
 
296
                }
 
297
 
 
298
                public List<string> LibPaths {
 
299
                        get { return libPaths; }
 
300
                }
 
301
 
 
302
                public List<string> Libs {
 
303
                        get { return libs; }
 
304
                }
 
305
 
 
306
                public List<string> CFlags {
 
307
                        get { return cflags; }
 
308
                }
 
309
                
 
310
                public override bool Equals (object o)
 
311
                {
 
312
                        Package other = o as Package;
 
313
                        
 
314
                        if (other == null) return false;
 
315
                        
 
316
                        return other.File.Equals (file);
 
317
                }
 
318
                
 
319
                public override int GetHashCode ()
 
320
                {
 
321
                        return (name + version).GetHashCode ();
 
322
                }
 
323
        }
 
324
}