internal static void Show(Boolean isThisRecognize, List <string> newFunctions, string[] filesWithFunc,
                                  bool[] found, string[] sourceFiles)
        {
            var diag = new AdditionalFunctionToFileDialog(isThisRecognize, newFunctions, filesWithFunc, found,
                                                          sourceFiles);

            diag.ShowDialog();
        }
Example #2
0
        public static void checkForFunctions(Boolean isThisRecognize, grammarRule SelectedRule,
                                             List <string> newFunctions)
        {
            var numNewFuncs = newFunctions.Count;

            if (numNewFuncs == 0)
            {
                return;
            }
            var filesWithFunc = new string[numNewFuncs];
            var found         = new Boolean[numNewFuncs];
            var sourceFiles   = Directory.GetFiles(GSApp.settings.RulesDirAbs, "*.cs");

            for (int i = 0; i < numNewFuncs; i++)
            {
                found[i] = false;
                var funcName = newFunctions[i];
                foreach (string file in sourceFiles)
                {
                    var r          = new StreamReader(new FileStream(file, FileMode.Open, FileAccess.Read), Encoding.Default);
                    var funcString = "public ";
                    if (isThisRecognize)
                    {
                        funcString += "double ";
                    }
                    else
                    {
                        funcString += "void ";
                    }
                    funcString += funcName + "(";
                    if (r.ReadToEnd().Contains(funcString))
                    {
                        filesWithFunc[i] = file;
                        found[i]         = true;
                        r.Close();
                        break;
                    }
                    else
                    {
                        r.Close();
                    }
                }
                if (!found[i] && !isThisRecognize)
                {
                    foreach (string file in sourceFiles)
                    {
                        var r          = new StreamReader(new FileStream(file, FileMode.Open, FileAccess.Read), Encoding.Default);
                        var funcString = "public designGraph " + funcName + "(";
                        var fileString = r.ReadToEnd();
                        r.Close();
                        var position = fileString.IndexOf(funcString, StringComparison.Ordinal);
                        if (position > 0)
                        {
                            filesWithFunc[i] = file;
                            found[i]         = true;
                            fileString       = fileString.Remove(position + 7, 11);
                            fileString       = fileString.Insert(position + 7, "void");
                            position         = fileString.IndexOf("return host;", position, StringComparison.Ordinal);
                            fileString       = fileString.Remove(position, 12);

                            var w = new StreamWriter(new FileStream(file, FileMode.Create, FileAccess.Write), Encoding.Default);
                            w.Write(fileString);
                            w.Flush();
                            w.Close();
                            break;
                        }
                    }
                }
            }
            AdditionalFunctionToFileDialog.Show(isThisRecognize, newFunctions, filesWithFunc, found, sourceFiles);
            if (filesWithFunc.Any(f => f == null))
            {
                return;
            }
            for (int i = 0; i < numNewFuncs; i++)
            {
                if (found[i])
                {
                    continue;
                }
                if (isThisRecognize)
                {
                    createRecognizeFunctionTemplate(filesWithFunc[i], SelectedRule, newFunctions[i]);
                }
                else
                {
                    createApplyFunctionTemplate(filesWithFunc[i], SelectedRule, newFunctions[i]);
                }
            }
        }