예제 #1
0
        private void LoadButton_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            //openFileDialog.InitialDirectory = @"C:\";
            openFileDialog.Title            = "Load";
            openFileDialog.DefaultExt       = "txt";
            openFileDialog.Filter           = "Text files (*.txt)|*.txt";
            openFileDialog.RestoreDirectory = true;
            if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                var    reader  = new StreamReader(openFileDialog.FileName);
                Preset preset  = new Preset();
                var    tokens2 = openFileDialog.FileName.Split(new string[] { "\\" }, StringSplitOptions.None);
                var    tokens3 = tokens2[tokens2.Length - 1].Split(new string[] { "." }, StringSplitOptions.None);
                preset.Name = tokens3[0];
                if (reader.ReadLine() == "BatchRename")
                {
                    var n = int.Parse(reader.ReadLine());
                    operationsList.Clear();
                    for (int i = 1; i <= n; i++)
                    {
                        var             tokens = reader.ReadLine().Split(new string[] { " - " }, StringSplitOptions.None);
                        StringOperation operation;
                        switch (tokens[0])
                        {
                        case "Replace":
                            operation = new ReplaceOperation
                            {
                                Args = new ReplaceArgs
                                {
                                    From = tokens[1],
                                    To   = tokens[2]
                                }
                            };
                            operationsList.Add(operation);
                            preset.stringOperations.Add(operation.Clone());
                            break;

                        case "Change Case":
                            operation = new NewCaseStringOperation
                            {
                                Args = new CaseArgs
                                {
                                    Case = tokens[1]
                                }
                            };
                            operationsList.Add(operation);
                            preset.stringOperations.Add(operation.Clone());
                            break;

                        case "Move":
                            operation = new MoveOperation
                            {
                                Args = new MoveArgs
                                {
                                    Mode   = tokens[1],
                                    Number = int.Parse(tokens[2])
                                }
                            };
                            operationsList.Add(operation);
                            preset.stringOperations.Add(operation.Clone());
                            break;

                        case "Fullname normalize":
                            operationsList.Add(new FullnameNormalizeOperation());
                            preset.stringOperations.Add(new FullnameNormalizeOperation());
                            break;

                        case "Unique name":
                            operationsList.Add(new UniqueNameOperation());
                            preset.stringOperations.Add(new UniqueNameOperation());
                            break;
                        }
                    }
                    loadedPresets.Add(preset);
                    PresetsList.SelectedIndex = loadedPresets.Count() - 1;
                }
                else
                {
                    System.Windows.Forms.MessageBox.Show("This txt file is not BatchRename's data");
                }
                reader.Close();
            }
        }
        public override string OperateString(string origin)
        {
            //string result = origin;

            ////Remove space at head
            //for (int i = 0; result[i] == ' ';)
            //    result.Remove(i, 1);

            ////Remove space at tail
            //for (int i = result.Length - 1; result[i] == ' ';)
            //    result.Remove(i, 1);

            //List<int> indexsDeleted = new List<int>();
            //for (int i = 0; i < result.Length;)
            //{
            //    int countSpace = 0;
            //    if (result[i] == ' ')
            //    {
            //        int j = 0;
            //        while (result[i + j] == ' ') j++;
            //        countSpace = j;
            //    }

            //    if (countSpace > 1)
            //    {
            //        indexsDeleted.Add(i);
            //        i += countSpace;
            //    }
            //    else
            //        i++;

            //}

            //foreach (var index in indexsDeleted)
            //    result.Remove(index, 1);

            //// Uppper first Letter
            //string tmp = "";
            //if (result[0] >= 'a' && result[0] <= 'z')
            //{
            //    tmp += (char)('A' + (result[0] - 'a'));
            //}

            //for (int i = 1; i < result.Length; i++)
            //{
            //    if (result[i] >= 'a' && result[i] <= 'z' && result[i - 1] == ' ')
            //        tmp += (char)('A' + (result[i] - 'a'));
            //    else
            //        tmp += result[i];
            //}

            //result = tmp;
            string result = origin;

            result = result.Trim();
            result = String.Join(" ", origin.Split(new char[] { ' ' },
                                                   StringSplitOptions.RemoveEmptyEntries));

            NewCaseStringOperation operation = new NewCaseStringOperation()
            {
                Args = new CaseArgs()
                {
                    Case = "Upper First Letter",
                }
            };

            result = operation.OperateString(result);

            return(result);
        }