示例#1
0
        /// <summary>
        /// Parse all infomation of a state
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static UserState ParseState(string fileName)
        {
            UserState state       = new UserState();
            string    contentFile = File.ReadAllText(fileName);
            string    FuncDefine  = ParseTransitionFunction(contentFile);

            // Keep track with file parsed
            state.FileSource = fileName;

            if (string.IsNullOrEmpty(FuncDefine))
            {
                return(null);
            }

            string[] lines = FuncDefine.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
            int      i;
            int      numLines          = lines.Length;
            string   Node              = "";
            string   transitonFunction = "";

            // Find transition function name
            transitonFunction    = lines[0].Substring(lines[0].IndexOf(TRANS_RET_TYPE) + TRANS_RET_TYPE.Length).Trim();
            transitonFunction    = transitonFunction.Substring(0, transitonFunction.IndexOf('(')).Trim();
            state.TransitionFunc = transitonFunction;

            for (i = 1; i < numLines; i++)
            {
                if (lines[i].Contains(NODE_DEFINE))
                {
                    string defineNode = lines[i];
                    string s          = defineNode.Substring(defineNode.IndexOf(NODE_DEFINE) + NODE_DEFINE.Length);
                    s    = s.Replace(" ", "").Replace(" ", "").Replace(";", "");
                    Node = s;
                    break;
                }
            }

            if (!string.IsNullOrEmpty(Node))
            {
                for (i = 1; i < numLines; i++)
                {
                    string s = lines[i].Trim();
                    // not a comment line
                    if (s.Length >= 2 && s[0] != '/' && s[1] != '/')
                    {
                        if (s.IndexOf(Node + ".") == 0)
                        {
                            // Found Name of state
                            if (s.Contains(Node + ".name"))
                            {
                                s = s.Substring((Node + ".name").Length);
                                int assignIdx = s.IndexOf("\"");
                                s = s.Substring(assignIdx + 1);

                                int breakIdx = s.IndexOf("\"");
                                if (assignIdx >= 0 && breakIdx >= 0)
                                {
                                    s = s.Substring(0, breakIdx);

                                    state.Name = s;
                                }
                                else
                                {
                                    string nextLine = lines[i + 1].Trim();

                                    assignIdx = nextLine.IndexOf("\"");
                                    s         = nextLine.Substring(assignIdx + 1);
                                    breakIdx  = s.IndexOf("\"");

                                    s          = s.Substring(0, breakIdx);
                                    state.Name = s;
                                }
                            }
                            else
                            {
                                // override handle event
                                UserEvent userEvent     = new UserEvent();
                                string    eventName     = "";
                                string    eventFunc     = "";
                                string    eventDescript = "";

                                int idxEqual = s.IndexOf("=");
                                int idxBreak = s.IndexOf(";");

                                // TODO: check list default event include this event name
                                eventName = s.Substring(s.IndexOf(Node + ".") + (Node + ".").Length).Trim();
                                eventName = eventName.Substring(0, eventName.IndexOf("=")).Trim();
                                // the line may be break into 2 lines
                                if (idxBreak >= 0)
                                {
                                    eventFunc = s.Substring(idxEqual + 1).Trim();
                                    eventFunc = eventFunc.Substring(0, eventFunc.IndexOf(";")).Trim();
                                }
                                else
                                {
                                    // assign fucntion is new line
                                    string nextLine = lines[i + 1].Trim();
                                    idxBreak  = nextLine.IndexOf(";");
                                    eventFunc = nextLine.Substring(0, idxBreak);
                                }

                                if (!string.IsNullOrEmpty(eventFunc))
                                {
                                    eventDescript = DefaultEventParser.ParseFunc(eventFunc, contentFile);
                                }

                                userEvent.EventName       = eventName;
                                userEvent.EventFunc       = eventFunc;
                                userEvent.EventDesciption = eventDescript;

                                state.OverrideEvents.Add(userEvent);
                            }
                        }
                    }
                }
            }


            return(state);
        }
示例#2
0
        private void buttonBrowse_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderDlg = new FolderBrowserDialog();

            folderDlg.ShowNewFolderButton = true;
            folderDlg.Description         = "Choose (user_handler) foder";


            if (!string.IsNullOrEmpty(FoderLastSelected))
            {
                if (Directory.Exists(FoderLastSelected))
                {
                    folderDlg.SelectedPath = FoderLastSelected;
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(Setting.FoderUser))
                {
                    FoderLastSelected = Setting.FoderUser;
                    if (Directory.Exists(FoderLastSelected))
                    {
                        folderDlg.SelectedPath = FoderLastSelected;
                    }
                }
            }
            // Show the FolderBrowserDialog.
            DialogResult result = folderDlg.ShowDialog();

            if (result == DialogResult.OK)
            {
                // Parse default implementation
                string fileDefaultaHandler = folderDlg.SelectedPath + "/user_handler_events.c";
                if (!File.Exists(fileDefaultaHandler))
                {
                    fileDefaultaHandler = folderDlg.SelectedPath + "/user_handler_events.cpp";
                }
                if (File.Exists(fileDefaultaHandler))
                {
                    Database.DefaultImplemt = DefaultEventParser.ParseDefaultImplementationEvent(fileDefaultaHandler);
                }
                else
                {
                    MessageBox.Show("Cannot open file user_handler_events");
                }

                // Parse for every state
                textBoxUserHandleFoder.Text = folderDlg.SelectedPath;
                FoderLastSelected           = folderDlg.SelectedPath;

                string[] AllFiles = Directory.GetFiles(folderDlg.SelectedPath, "*", SearchOption.AllDirectories);
                int      numFiles = AllFiles.Length;

                Database.AllUserState.Clear();

                for (int i = 0; i < numFiles; i++)
                {
                    if (AllFiles[i].Contains(".c") || AllFiles[i].Contains(".cpp"))
                    {
                        UserState state = UserStateParser.ParseState(AllFiles[i]);

                        if (state != null)
                        {
                            UserState mergeState = state.MergeWithDefaultHandle(Database.DefaultImplemt);
                            Database.AllUserState.Add(mergeState);
                        }
                    }
                }
                // Assign data to UI of every user_state
                AssignAllState();

                // Save current foder for user
                if (FoderLastSelected != Setting.FoderUser)
                {
                    Setting.WriteSetting(FoderLastSelected);
                }
            }
        }