// // (c) 2009, Sigbjorn Finne // using System; using System.Collections; using System.Text.RegularExpressions; namespace HsWrap { // // The WrapQ types handles the management of outstanding // types for which we don't have yet a wrapper for. // public class WrapQ { protected System.Collections.Queue m_q; protected Blacklist m_blacklist; static protected String m_prevAnswer = ""; public WrapQ() { m_q = new System.Collections.Queue(); m_blacklist = new Blacklist(); } public bool IsExternalModule(String t) { // if unqualified or Data. prefixed, we know its provenance. return (t.StartsWith("Data.") || (t.IndexOf(".") == -1)); } public bool IsElem(String t) { return IsElem(t,false); } public bool IsElem(String t, bool prefixMatch) { foreach(Object[] ent in m_q) { if ( t == ((String)ent[0]) || (prefixMatch && ((String)ent[0]).StartsWith(t)) ) { return true; } } return false; } public Blacklist Blacklist { get { return m_blacklist; } set { m_blacklist = value; } } public bool IsAGo(String s) { if ( !m_blacklist.IsTypeOfInterest(s,false) ) { return false; } if ( IsElem(s,true) ) { return false; } if ( Config.InteractiveMode && !Config.DryRunMode) { while (true) { String ynString="yne"; if ( m_prevAnswer == "n" ) { ynString="yNe"; } if ( m_prevAnswer == "y" ) { ynString="Yne"; } Console.Write("Generate wrapper for: {0} ? [{1}] ", s, ynString); String yn = Console.ReadLine(); if ( yn == "e" ) { Console.Write("Add ignore pattern: "); String pat=Console.ReadLine(); if ( pat != "" ) { AddException(pat); } } else { if ( yn == "" ) { yn = m_prevAnswer; } m_prevAnswer=yn; return ( yn == "y" ); } } } return true; } public void AddException(String pat) { m_blacklist.AddIgnorePattern(pat); } public void AddType(String ty) { Object[] arr = {ty,( Config.OutputDir=="" ? "" : (Config.OutputDir + "\\")) + ty.Replace('.','\\')+".hs"}; if ( !IsElem(ty) ) { m_q.Enqueue(arr); } } public void AddType(String ty,String modu) { Object[] arr = {ty,modu}; if ( !IsElem(ty) ) { m_q.Enqueue(arr); } } public String GetNextModule(out String outFile) { return GetNextModule(true,out outFile); } public String GetNextModule(bool useBlackList, out String outFile) { try { while (true) { Object[] res = (Object[])m_q.Dequeue(); if ( res==null && m_q.Count == 0 ) { outFile=""; return null; } String tyName = (String)res[0]; outFile = (String)res[1]; if ( useBlackList && m_blacklist.IsTypeKnown(tyName) && !m_blacklist.IsTypeOfInterest(tyName) ) { Console.WriteLine("NOTE: Type {0} will be ignored", tyName); } else { // m_blacklist.AddType(tyName,true/*of interest*/); return tyName; } } } catch (System.InvalidOperationException) { outFile=""; return null; } } }; }