-
Notifications
You must be signed in to change notification settings - Fork 528
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce the RtxtParser and RtxtWriter
- Loading branch information
1 parent
fa72962
commit bb1694a
Showing
6 changed files
with
179 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/Xamarin.Android.Build.Tasks/Utilities/RtxtParser.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using Microsoft.Android.Build.Tasks; | ||
using Microsoft.Build.Utilities; | ||
|
||
namespace Xamarin.Android.Tasks | ||
{ | ||
public enum RType { | ||
Integer, | ||
Array, | ||
} | ||
public struct R { | ||
public RType Type = RType.Integer; | ||
public int Id; | ||
public int [] Ids; | ||
public string Identifier; | ||
public string ResourceType; | ||
|
||
public override string ToString () { | ||
if (Type == RType.Integer) | ||
return $"int {ResourceType} {Identifier} 0x{Id.ToString ("x")}"; | ||
return $"int[] {ResourceType} {Identifier} {{{String.Join (",", Ids.Select (x => $"0x{x.ToString ("x")}"))}}}"; | ||
} | ||
} | ||
|
||
public class RtxtParser { | ||
|
||
TaskLoggingHelper log; | ||
Dictionary<string, string> map; | ||
|
||
Dictionary<string, string> resource_fixup = new Dictionary<string, string> (StringComparer.OrdinalIgnoreCase); | ||
public Dictionary<string, R> Parse (string file, TaskLoggingHelper logger, Dictionary<string, string> mapping){ | ||
log = logger; | ||
map = mapping; | ||
var result = new Dictionary<string, R> (); | ||
if (File.Exists (file)) | ||
ProcessRtxtFile (file, result); | ||
return result; | ||
} | ||
|
||
void ProcessRtxtFile (string file, Dictionary<string, R> result) | ||
{ | ||
var lines = File.ReadLines (file); | ||
foreach (var line in lines) { | ||
var items = line.Split (new char [] { ' ' }, 4); | ||
int value = items [1] != "styleable" ? Convert.ToInt32 (items [3], 16) : -1; | ||
string itemName = ResourceIdentifier.GetResourceName(items [1], items [2], resource_fixup, log); | ||
switch (items [1]) { | ||
case "anim": | ||
case "animator": | ||
case "attr": | ||
case "array": | ||
case "bool": | ||
case "color": | ||
case "dimen": | ||
case "drawable": | ||
case "font": | ||
case "id": | ||
case "integer": | ||
case "interpolator": | ||
case "layout": | ||
case "menu": | ||
case "mipmap": | ||
case "plurals": | ||
case "raw": | ||
case "string": | ||
case "style": | ||
case "transition": | ||
case "xml": | ||
result[ items [1]] = new R () { | ||
ResourceType = items[1], | ||
Identifier = itemName, | ||
Id = value, | ||
}; | ||
break; | ||
case "styleable": | ||
switch (items [0]) { | ||
case "int": | ||
result[ items [1]] = new R () { | ||
ResourceType = items[1], | ||
Identifier = itemName, | ||
Id = Convert.ToInt32 (items [3], 10), | ||
}; | ||
break; | ||
case "int[]": | ||
var arrayValues = items [3].Trim (new char [] { '{', '}' }) | ||
.Replace (" ", "") | ||
.Split (new char [] { ',' }); | ||
|
||
result[ items [1]] = new R () { | ||
ResourceType = items[1], | ||
Type = RType.Array, | ||
Identifier = itemName, | ||
Ids = arrayValues.Select (x => string.IsNullOrEmpty (x) ? -1 : Convert.ToInt32 (x, 16)).ToArray (), | ||
}; | ||
break; | ||
} | ||
break; | ||
// for custom views | ||
default: | ||
result[ items [1]] = new R () { | ||
ResourceType = items[1], | ||
Identifier = itemName, | ||
Id = value, | ||
}; | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using System.Linq; | ||
using Microsoft.Android.Build.Tasks; | ||
using Microsoft.Build.Utilities; | ||
|
||
namespace Xamarin.Android.Tasks | ||
{ | ||
/// Write a list of Item to a file | ||
/// | ||
public class RtxtWriter { | ||
public void Write (string file, Dictionary<string, R> items) { | ||
File.WriteAllLines (file, items.Values.Select (x => x.ToString ()), Encoding.UTF8); | ||
} | ||
} | ||
} |