Implement HTTP ini download, Ini parser. WIP.

develop
LEdoian 3 years ago
parent 63146ec4b7
commit ec2ab8f619

@ -1,5 +1,8 @@
using System;
using System.Net.Http;
using System.IO;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace QuickPlay
{
@ -73,7 +76,7 @@ namespace QuickPlay
public PlayerConfiguration GetPlayerConfig()
{
// TODO: decide sensibly
var cfgProvider = new CurlConfigurationProvider(playerConfigUrl);
var cfgProvider = new HttpConfigurationProvider(playerConfigUrl);
return cfgProvider.GetConfiguration();
}
@ -97,9 +100,23 @@ namespace QuickPlay
/// </summary>
public class PlayerConfiguration
{
List<IPlayable> songs;
public static PlayerConfiguration FromFile(StreamReader reader)
{
return null; // FIXME: Implement
var parser = new IniParser(reader);
var ini = parser.Parse();
foreach (string key in ini.Keys)
{
// Two options: either general options, or song descriptions
if (key == "general")
{
} else
{
// Song
}
}
}
public IPlayer GetPlayer()
@ -113,16 +130,19 @@ namespace QuickPlay
PlayerConfiguration GetConfiguration();
}
sealed class CurlConfigurationProvider : IPlayerConfigurationProvider
sealed class HttpConfigurationProvider : IPlayerConfigurationProvider
{
readonly string configUrl;
public CurlConfigurationProvider(string url)
public HttpConfigurationProvider(string url)
{
configUrl = url;
}
public PlayerConfiguration GetConfiguration()
public async Task<PlayerConfiguration> GetConfiguration()
{
throw new NotImplementedException();
var client = new HttpClient() ;
var resp = await client.GetStreamAsync(configUrl);
var sr = new StreamReader(resp);
return PlayerConfiguration.FromFile(sr);
}
}

@ -0,0 +1,67 @@
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
namespace QuickPlay
{
/// <summary>
/// This class implements (yet another) INI-style parser.
///
/// INI specification: everything is in sections, comments using both '#'
/// or ';', space around '=' is trimmed, last value assigned overrides
/// previous assignments, sections may not repeat. Section names may only
/// contain reasonable characters (at least [a-zA-Z0-9. ], not really
/// enforced). Everything is case-sensitive.
///
/// Both CRLF and just LF should be supported as line ends.
/// </summary>
class IniParser
{
StreamReader reader;
string currentSection = null;
public IniParser(StreamReader sr)
{
reader = sr;
}
// The return type is dictionary by sections, which in turn holds the section key-value dictionary
// Maybe the code is a bit more Pythonic than it should be...
public Dictionary<string, Dictionary<string, string>> Parse()
{
Dictionary<string, Dictionary<string, string>> result = new Dictionary<string, Dictionary<string, string>>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
line = StripComments(line).Trim();
if (line.Length == 0) continue;
if (line.StartsWith('['))
{
// This does not really do the right thing, but for QuickPlay's use it is good enough.
currentSection = line.Split(new char[] { '[', ']' })[1];
if (result.ContainsKey(currentSection)) throw new InvalidOperationException("Multiple sections with same name");
result[currentSection] = new Dictionary<string, string>();
}
else
{
// Other lines may only be assignments
int equalSignPosition = line.IndexOf('=');
string key = line.Substring(0, equalSignPosition).Trim();
string value = line.Substring(equalSignPosition + 1).Trim();
result[currentSection][key] = value;
}
}
return result;
}
string StripComments(string line)
{
return line.Split(new char[] { ';', '#' })[0];
}
}
}

@ -43,6 +43,7 @@
<BundleAssemblies>false</BundleAssemblies>
<AndroidKeyStore>false</AndroidKeyStore>
<AndroidUseAapt2>false</AndroidUseAapt2>
<AndroidHttpClientHandlerType>Xamarin.Android.Net.AndroidClientHandler</AndroidHttpClientHandlerType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugSymbols>True</DebugSymbols>

@ -7,7 +7,6 @@ using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace QuickPlay

Loading…
Cancel
Save