Example #1
0
        private static string GetSortOrderBy(string[] args)
        {
            string orderBy = "";

            if (args == null || args.Length <= 0)
            {
                return(orderBy);
            }

            //CommandlineParameterFinder.DefaultLogicMatchingType = StringArraySearcher.DefaultLogicMatchingTypes.Equals;
            //Func<string, string, bool> matchLogic = CommandlineParameterFinder.DefaultMatchLogic;

            bool caseSensitive = false;
            //int occurrence = 1;
            int           indexOffset   = 1;
            SearchDetails searchDetails = null;

            searchDetails = CommandlineParameterFinder.FindMatch(args, "--order-by", caseSensitive, indexOffset);             //  indexOffset);
            if (searchDetails.Matched)
            {
                orderBy = searchDetails.Value;
            }

            return(orderBy);
        }
Example #2
0
        private static DelimitedTextReader CreateTextReader(string[] args)
        {
            DelimitedTextReader textReader = new DelimitedTextReader();

            if (args == null || args.Length <= 0)
            {
                return(textReader);
            }


            //CommandlineParameterFinder.DefaultLogicMatchingType = StringArraySearcher.DefaultLogicMatchingTypes.Equals;
            //Func<string, string, bool> matchLogic = CommandlineParameterFinder.DefaultMatchLogic;

            bool caseSensitive = false;
            //int occurrence = 1;
            int           indexOffset   = 1;
            SearchDetails searchDetails = null;


            //searchDetails = CommandlineParameterFinder.FindMatch(args, "--file", caseSensitive, indexOffset); //  indexOffset);
            //if(searchDetails.Matched) file = searchDetails.Value;


            searchDetails = CommandlineParameterFinder.FindMatch(args, "--field-delimiter", caseSensitive, indexOffset);
            if (searchDetails.Matched)
            {
                textReader.FieldDelimiter = searchDetails.Value;
            }

            textReader.FieldDelimiter = textReader.FieldDelimiter.Replace("\\r", "\r").Replace("\\n", "\n").Replace("\\t", "\t").Replace("\\s", " ");

            searchDetails = CommandlineParameterFinder.FindMatch(args, "--record-delimiter", caseSensitive, indexOffset);
            if (searchDetails.Matched)
            {
                textReader.RecordDelimiter = searchDetails.Value;
            }

            textReader.RecordDelimiter = textReader.RecordDelimiter.Replace("\\r", "\r").Replace("\\n", "\n").Replace("\\t", "\t").Replace("\\s", " ");

            searchDetails = CommandlineParameterFinder.FindMatch(args, "--header-index", caseSensitive, indexOffset);
            if (searchDetails.Matched)
            {
                string headerIndexStr = searchDetails.Value;
                if (headerIndexStr != null && headerIndexStr.Trim().Length > 0)
                {
                    int  headerIndex = -1;
                    bool success     = Int32.TryParse(headerIndexStr, out headerIndex);
                    if (success)
                    {
                        textReader.HeaderRecordIndex = headerIndex;
                    }
                }
            }


            return(textReader);
        }
        // primary
        public List <SearchDetails> FindMatches(string[] args, string matchpattern, bool _caseSensitive
                                                //, int occurrence
                                                , int _indexOffsetFromMatch
                                                , Func <string, string, bool> matchLogic
                                                )
        {
            this.CaseSensitive        = _caseSensitive;
            this.IndexOffsetFromMatch = _indexOffsetFromMatch;


            List <SearchDetails> parameters = new List <SearchDetails>();

            //string retval = "";

            if (matchpattern == null || "".Equals(matchpattern))
            {
                return(parameters);
            }

            if (args == null || args.Length <= 0)
            {
                return(parameters);
            }

            if (matchLogic == null)
            {
                matchLogic = this.DefaultMatchLogic;
            }

            // If still null, simply return an empty list...
            if (matchLogic == null)
            {
                return(parameters);
            }


            int occurrenceCount = 0;

            for (int c = 0; c < args.Length; c++)
            {
                if (args[c] == null)
                {
                    continue;
                }


                bool matched = false;


                string finalmatchpattern = matchpattern;
                string matchtext         = args[c];



                ////if(AsRegex)
                ////{
                ////	if(!caseSensitive) this.RegexOptions |= System.Text.RegularExpressions.RegexOptions.IgnoreCase;

                ////	System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(matchpattern, this.RegexOptions); // use original pattern w/out lowering, etc.
                ////	matched = regex.IsMatch(matchtext); // use original text w/out lowering, etc.

                ////	//// TESTING
                ////	//if(matched) System.Console.WriteLine("***** REGEX MATCHED! *****");
                ////}
                ////else
                ////{

                ////	if (!caseSensitive)
                ////	{
                ////		finalmatchpattern = finalmatchpattern.ToLower();
                ////		matchtext = matchtext.ToLower();
                ////	}

                ////	if(matchLogic != null)
                ////	{
                ////		matched = matchLogic.Invoke(finalmatchpattern, matchtext);
                ////	}
                ////	else
                ////	{
                ////		matched = matchtext.Equals(finalmatchpattern);
                ////	}
                ////}

                matched = matchLogic.Invoke(finalmatchpattern, matchtext);


                if (matched)
                {
                    ++occurrenceCount;

                    SearchDetails stringParameterDetails = new SearchDetails();
                    stringParameterDetails.Occurrence = occurrenceCount;
                    stringParameterDetails.MatchValue = matchtext;
                    stringParameterDetails.MatchIndex = c;
                    parameters.Add(stringParameterDetails);

                    if (this.IndexOffsetFromMatch != 0)
                    {
                        int offsetIndex = this.IndexOffsetFromMatch + c;
                        if (offsetIndex >= 0 && offsetIndex < args.Length)
                        {
                            //retval = args[offsetIndex];
                            stringParameterDetails.MatchOffsetValue = args[offsetIndex];                             // retval;
                            stringParameterDetails.MatchOffsetIndex = offsetIndex;
                            //parameters.Add(stringParameterDetails); // when returning all matched elements
                        }
                        ////else
                        ////{
                        ////	retval = String.Empty; // "ERROR: (indexOffsetFromMatch + c) -> (" + indexOffsetFromMatch + " + " + c + ")"; //  String.Empty; // otherwise, return empty string
                        ////	parameters.Add(stringParameterDetails); // when returning all matched elements
                        ////}
                    }

                    ////if( occurrence >= 0 && occurrenceCount == occurrence) break; // skip this check when returning all matched elements
                }
            }             // loop



            return(parameters);
        }
        // primary
        public SearchDetails FindMatch(string[] args, string paramName, bool _caseSensitive
                                       , int _occurrence
                                       , int _indexOffsetFromMatch
                                       , Func <string, string, bool> matchLogic
                                       )
        {
            SearchDetails retval = new SearchDetails();

            this.CaseSensitive        = _caseSensitive;
            this.Occurrence           = _occurrence;
            this.IndexOffsetFromMatch = _indexOffsetFromMatch;



            if (paramName == null || "".Equals(paramName))
            {
                return(retval);
            }

            if (args == null || args.Length <= 0)
            {
                return(retval);
            }

            if (matchLogic == null)
            {
                matchLogic = this.DefaultMatchLogic;
            }


            //List<StringParameterDetails> parameters = this.FindMatchs(args, paramName, caseSensitive, occurrence, indexOffsetFromMatch, matchLogic);
            List <SearchDetails> parameters = this.FindMatches(args, paramName, this.CaseSensitive, this.IndexOffsetFromMatch, matchLogic);

            if (parameters != null && parameters.Count > 0)
            {
                if (this.Occurrence == 0)
                {
                    this.Occurrence = 1;
                }                                                                  // only if occurance == 0 then set to 1

                int occurrenceCounter = 0;

                // start from beginning (ASC order)
                if (this.Occurrence > 0)
                {
                    int x = 0;
                    for (; x < parameters.Count; ++x)
                    {
                        ++occurrenceCounter;
                        if (occurrenceCounter == this.Occurrence)
                        {
                            retval = parameters[x];
                            break;
                        }
                    }
                }
                // else if occurrence < 0, start from end (DESC order) and work backwards...
                else
                {
                    int x = parameters.Count - 1;
                    for (; x >= 0; --x)
                    {
                        --occurrenceCounter;
                        if (occurrenceCounter == this.Occurrence)
                        {
                            retval = parameters[x];
                            break;
                        }
                    }
                }

                //retval = parameters[0];
            }


            if (retval == null)
            {
                retval = new SearchDetails();
            }

            return(retval);
        }