* Makefile.am:
[monodevelop.git] / main / src / addins / CBinding / Project / Package.cs
blob33a91ce3b061a77c39a829ecc7edefcf51e9efe5
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:
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.
32 using System;
33 using System.IO;
34 using System.Text;
35 using System.Collections.Generic;
37 using Mono.Addins;
39 using MonoDevelop.Core;
40 using MonoDevelop.Core.Serialization;
42 namespace CBinding
44 public class Package
46 [ItemProperty ("file")]
47 private string file;
49 [ItemProperty ("name")]
50 private string name;
52 [ItemProperty ("IsProject")]
53 private bool is_project;
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> ();
63 public Package (string file)
65 this.file = file;
66 this.is_project = false;
68 ParsePackageEssentials ();
71 public Package (CProject project)
73 name = project.Name;
74 file = Path.Combine (project.BaseDirectory, name + ".md.pc");
75 is_project = true;
77 ParsePackageEssentials ();
80 public Package ()
84 void ParsePackageEssentials ()
86 string line;
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;
95 if (line.StartsWith ("Version")) {
96 version = line.Split (':')[1].TrimStart ();
102 bool parsed = false;
103 public void ParsePackage ()
105 if (parsed)
106 return;
108 string line;
110 using (StreamReader reader = new StreamReader (file)) {
111 while ((line = reader.ReadLine ()) != null) {
112 if (line.StartsWith ("#"))
113 continue;
115 if (line.IndexOf ('=') >= 0)
116 ParseVar (line);
118 if (line.IndexOf (':') >= 0)
119 ParseProperty (line);
123 parsed = true;
126 void ParseVar (string line)
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 ());
133 vars.Add (key, parsedValue);
136 void ParseProperty (string line)
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 ());
142 if (value.Length <= 0)
143 return;
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;
167 void ParseRequires (string reqsline)
169 string[] splitted = reqsline.Split (' ');
171 foreach (string str in splitted) {
172 if (str.Trim () == string.Empty)
173 continue;
174 Requires.Add (str);
178 void ParseLibs (string libline)
180 int i = 0;
181 string lib;
183 while (true) {
184 i = libline.IndexOf ('-', i);
186 if (i < 0)
187 break;
189 int count = 0;
191 while (libline.Length > (count+i+2) && libline[count+i+2] != ' ')
192 count++;
194 lib = libline.Substring (i+2, count);
196 if (libline[i+1] == 'L') {
197 libPaths.Add (lib);
198 } else if (libline[i+1] == 'l') {
199 libs.Add (lib);
202 i++;
206 void ParseCFlags (string cflagsline)
208 string[] splitted = cflagsline.Split (' ');
210 foreach (string str in splitted) {
211 if (str.Trim () == string.Empty)
212 continue;
213 CFlags.Add (str);
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)
230 int length = 80;
232 if (desc.Length <= length)
233 return desc;
235 StringBuilder builder = new StringBuilder (desc);
236 int i = 0;
237 int lines = 1;
239 while (i < desc.Length) {
240 i++;
242 if (i > lines * length) {
243 lines++;
245 do {
246 i--;
247 } while (desc [i] != ' ');
249 builder.Replace (' ', '\n', i, 1);
253 return builder.ToString ();
256 Dictionary<string, string> CustomTags ()
258 Dictionary<string, string> customTags = new Dictionary<string, string> (StringComparer.InvariantCultureIgnoreCase);
259 int i = 0;
261 foreach (KeyValuePair<string, string> kvp in vars) {
262 customTags.Add (kvp.Key, kvp.Value);
263 i++;
266 return customTags;
269 public string File {
270 get { return file; }
271 set { file = value; }
274 public string Name {
275 get { return name; }
276 set { name = value; }
279 public bool IsProject {
280 get { return is_project; }
281 set { is_project = value; }
284 public string Version {
285 get { return version; }
286 set { version = value; }
289 public string Description {
290 get { return description; }
293 public List<string> Requires {
294 get { return requires; }
297 public List<string> LibPaths {
298 get { return libPaths; }
301 public List<string> Libs {
302 get { return libs; }
305 public List<string> CFlags {
306 get { return cflags; }
309 public override bool Equals (object o)
311 Package other = o as Package;
313 if (other == null) return false;
315 return other.File.Equals (file);
318 public override int GetHashCode ()
320 return (name + version).GetHashCode ();