Exemplo n.º 1
0
        public static object?Read(this IRead read, string filename)
        {
            if (filename.Contains("{"))
            {
                MemoryStream?memory = new MemoryStream(Encoding.UTF8.GetBytes(filename));
                return(read.Read(memory));
            }
            if (filename.Contains("(") && filename.Contains("*") && filename.Contains(".") && filename.Contains(")") && filename.Contains("|"))
            {
                // open file dialog filter
                Microsoft.Win32.OpenFileDialog?ofd = new Microsoft.Win32.OpenFileDialog {
                    Filter = filename
                };
                bool?result = ofd.ShowDialog();
                if (result == true)
                {
                    if (File.Exists(ofd.FileName))
                    {
                        FileStream?stream = new FileStream(ofd.FileName, FileMode.Open);
                        //stream.SetFileName(ofd.FileName);
                        object?instance = read.Read(stream);
                        stream.Close();
                        instance?.SetFileName(ofd.FileName);
                        return(instance);
                    }
                }
                else
                {
                    return(null);
                }
            }
            if (File.Exists(filename))
            {
                using FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
                object?item = read.Read(fs);
                fs.Close();
                SetPropertyValue(item, "FileName", filename);
                item?.SetFileName(filename);

                return(item);
            }
            else
            {
                StackTrace?stackTrace = new StackTrace();
                foreach (Assembly?assembly in stackTrace.GetAssemblies())
                {
                    Stream?stream = assembly.GetStream(filename);
                    if (stream != null)
                    {
                        object?item = read.Read(stream);
                        SetPropertyValue(item, "FileName", filename);
                        item?.SetFileName(filename);

                        return(item);
                    }
                }
                throw new Exception($"{filename} not found");
            }
        }
Exemplo n.º 2
0
 public static unsafe void Read <TElement>(this IRead <TElement> collection, TElement *elements, Int32 length) where TElement : unmanaged
 {
     for (Int32 i = 0; i < length; i++)
     {
         collection.Read(out elements[i]);
     }
 }
Exemplo n.º 3
0
        public void Copy()
        {
            var txt = reader.Read();

            writer.Write(txt);
            Console.ReadLine();
        }
Exemplo n.º 4
0
 public static object Read(IRead read, string value)
 {
     if (File.Exists(value))
     {
         using (FileStream stream = new FileStream(value, FileMode.Open))
         {
             var result = read.Read(stream);
             stream.Close();
             return result;
         }
     }
     using (MemoryStream memory = new MemoryStream(Encoding.UTF8.GetBytes(value)))
     {
         return read.Read(memory);
     }
 }
        public IEnumerable <IRow> DetermineDeletes()
        {
            var input = _inputReader.Read();

#if NETS10
            // no PLINQ
#else
            if (_context.Entity.Pipeline == "parallel.linq")
            {
                input = input.AsParallel();
            }
#endif

            // I believe this is here in case the primary key depends on transformations
            var transformed = _transforms.Aggregate(input, (current, transform) => current.Select(transform.Operate));

            var output = _outputReader.Read();
#if NETS10
            // no PLINQ
#else
            if (_context.Entity.Pipeline == "parallel.linq")
            {
                output = output.AsParallel();
            }
#endif

            return(output.Except(transformed, new KeyComparer(_context.Entity.GetPrimaryKey())));
        }
Exemplo n.º 6
0
 /// <summary>
 /// Reads until the <paramref name="elements"/> is filled.
 /// </summary>
 /// <param name="collection">This collection.</param>
 /// <param name="elements">The <see cref="Span{T}"/> of <typeparamref name="TElement"/> to fill.</param>
 public static void Read <TElement>(this IRead <TElement?> collection, Span <TElement?> elements)
 {
     for (Int32 i = 0; i < elements.Length; i++)
     {
         collection.Read(out elements[i]);
     }
 }
Exemplo n.º 7
0
        public static void Open(Window window, string[] fileExtensions, IRead<string> data)
        {
            var filter = FileExtensionModule.AddStar(fileExtensions);
            var filename = OpenFileModule.WithFilters(window, filter);
            if (filename == null) return;

            data.Read(filename);
        }
Exemplo n.º 8
0
        public static T FormatConfig <T>(string path) where T : class, new()
        {
            string str = File.ReadAllText(path);
            IRead  ir  = (IRead) new T();

            ir.Read(str);
            return(ir as T);
        }
Exemplo n.º 9
0
        // This will be async because the reader has a task in it.
        public static async Task Consumer(IRead <string> reader)
        {
            while (!reader.IsComplete())
            {
                var msg = await reader.Read();

                Print(msg);
            }
        }
        private void ModifyFields(IEnumerable <Field> fields)
        {
            var expanded = fields.ToArray();

            if (_context.Connection.MaxLength > 0)
            {
                foreach (var field in expanded)
                {
                    field.Length = _context.Connection.MaxLength.ToString();
                }
            }

            var checkLength = _context.Connection.MinLength > 0 || _context.Connection.MaxLength > 0;
            var checkTypes  = _context.Connection.Types.Any(t => t.Type != "string");

            if (checkTypes || checkLength)
            {
                var entity           = _process.Entities.First();
                var rowFactory       = new RowFactory(_context.RowCapacity, entity.IsMaster, false);
                var defaultTransform = new Transformalize.Transforms.System.DefaultTransform(new PipelineContext(_context.Logger, _process, entity), entity.GetAllFields());
                var rows             = defaultTransform.Operate(_reader.Read()).ToArray(); // can take a lot of memory

                if (rows.Length == 0)
                {
                    return;
                }

                if (checkLength)
                {
                    Parallel.ForEach(expanded, f => {
                        var length = _context.Connection.MaxLength == 0 ? rows.Max(row => row[f].ToString().Length) + 1 : Math.Min(rows.Max(row => row[f].ToString().Length) + 1, _context.Connection.MaxLength);
                        if (_context.Connection.MinLength > 0 && length < _context.Connection.MinLength)
                        {
                            length = _context.Connection.MinLength;
                        }
                        f.Length = length.ToString();
                    });
                }

                if (checkTypes)
                {
                    var canConvert = Constants.CanConvert();
                    Parallel.ForEach(expanded, f => {
                        foreach (var dataType in _context.Connection.Types.Where(t => t.Type != "string"))
                        {
                            if (rows.All(r => canConvert[dataType.Type](r[f].ToString())))
                            {
                                f.Type = dataType.Type;
                                break;
                            }
                        }
                    });
                }
            }
        }
Exemplo n.º 11
0
        public Contributor GetOrInsert(Contributor contributor)
        {
            var contributorSaved = contributorReader.Read(contributor.Email);

            if (contributorSaved is null)
            {
                contributorSaved = contributorWriter.Save(contributor);
            }

            return(contributorSaved);
        }
Exemplo n.º 12
0
 public static object Read(IRead read, Assembly assembly, string name)
 {
     foreach (var manifestResourceName in assembly.GetManifestResourceNames())
     {
         if (manifestResourceName.Contains(name))
         {
             return read.Read(assembly.GetManifestResourceStream(manifestResourceName));
         }
     }
     return null;
 }
 private object ExecuteHandler(object command, IRead executor)
 {
     try
     {
         return executor.Read(command);
     }
     finally
     {
         _container.Release(executor);
     }
 }
Exemplo n.º 14
0
 public static object Read(IRead read,string filename)
 {
     if(File.Exists(filename))
     {
         using (FileStream fs = new FileStream(filename, FileMode.Open))
         {
             return read.Read(fs);
         }
     }
     return null;
 }
Exemplo n.º 15
0
        /// <summary>
        /// Reads <paramref name="amount"/> of <typeparamref name="TElement"/>.
        /// </summary>
        /// <param name="collection">This collection.</param>
        /// <param name="amount">The amount of elements to read.</param>
        /// <param name="elements">The <typeparamref name="TElement"/> values that were read.</param>
        public static void Read <TElement>(this IRead <TElement?> collection, nint amount, out ReadOnlySpan <TElement?> elements)
        {
            TElement?[] buffer = new TElement?[amount];
            Int32       i      = 0;

            for (; i < amount; i++)
            {
                collection.Read(out buffer[i]);
            }
            elements = buffer.AsSpan(0, i);
        }
Exemplo n.º 16
0
        public static object?Read(this IRead read, Assembly assembly, string name)
        {
            foreach (string?manifestResourceName in assembly.GetManifestResourceNames())
            {
                if (manifestResourceName.Contains(name))
                {
#pragma warning disable CS8604 // Possible null reference argument.
                    return(read.Read(assembly.GetManifestResourceStream(manifestResourceName)));

#pragma warning restore CS8604 // Possible null reference argument.
                }
            }
            return(null);
        }
 public IEnumerable <IRow> Read()
 {
     using (var cn = _cf.GetConnection()) {
         cn.Open();
         foreach (var batch in _reader.Read().Partition(_input.Entity.ReadSize))
         {
             foreach (var row in _fieldsReader.Read(batch))
             {
                 _rowCount++;
                 yield return(row);
             }
         }
     }
     _input.Info("{0} from {1}", _rowCount, _input.Connection.Name);
 }
Exemplo n.º 18
0
        public T ReadDataFromFile <T>(string path)
        {
            dynamic result = null;

            try
            {
                if (!string.IsNullOrEmpty(path))
                {
                    if (File.Exists(path))
                    {
                        string contents = read.Read <string>(path);
                        result = jsonToCollec.Convert <T>(contents);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return((T)Convert.ChangeType(result, typeof(T)));
        }
Exemplo n.º 19
0
        public IEnumerable <SparseTwinIndex <float> > Assign()
        {
            var zoneArray     = this.Root.ZoneSystem.ZoneArray;
            var zoneSystem    = zoneArray.GetFlatData();
            var population    = this.Root.Population.Population.GetFlatData();
            var numberOfZones = zoneSystem.Length;

            foreach (var cat in Categories)
            {
                cat.InitializeDemographicCategory();
                var ret     = zoneArray.CreateSquareTwinArray <float>();
                var flatRet = ret.GetFlatData();
                Parallel.For(0, numberOfZones,
                             delegate(int i)
                {
                    var localPop = population[i];
                    int popLength;
                    if (localPop != null && (popLength = localPop.Length) > 0)
                    {
                        var iArray = flatRet[i];
                        EnsureGetDest(ref getDest, localPop[0]);
                        for (int j = 0; j < popLength; j++)
                        {
                            IZone destZone;
                            var person = localPop[j];
                            if (cat.IsContained(person))
                            {
                                getDest.Read(person, out destZone);
                                if (destZone != null)
                                {
                                    iArray[zoneArray.GetFlatIndex(destZone.ZoneNumber)] += Probability;
                                }
                            }
                        }
                    }
                });
                yield return(ret);
            }
        }
Exemplo n.º 20
0
    public void ConsumeRepository(string connectionString)
    {
        IRead       reader     = null;
        IRepository repository = null;

        if (RepositoryFactory.ConnectionStringIsReadOnly(connectionString))
        {
            reader = RepositoryFactory.GetReadOnlyRepository(connectionString);
        }
        else
        {
            repository = RepositoryFactory.GetRepository(connectionString);
            reader     = repository;
        }
        object o = reader.Read();

        // do something with o
        // if allowed then write o to repository
        if (repository != null)
        {
            repository.Write(o);
        }
    }
Exemplo n.º 21
0
 public IEnumerable <Row> DetermineDeletes()
 {
     return(_outputReader.Read().Except(_inputReader.Read(), new KeyComparer(_entity.GetPrimaryKey())));
 }
Exemplo n.º 22
0
 public IEnumerable <IRow> Read()
 {
     return(_reader.Read());
 }
Exemplo n.º 23
0
 public string Read(string file)
 {
     return(_read.Read(file));
 }
Exemplo n.º 24
0
 public IEnumerable <User> GetUsers()
 {
     return(_userReader.Read());
 }
Exemplo n.º 25
0
        public string ExecuteAsync(IRead fileReader, Document document)
        {
            Debug.Assert(document != null);
            Debug.Assert(document.Statement != null);

            var logo64Base = fileReader.GetImagebase64(document.Logo);

            var quoteTemplate = fileReader.Read(TEMPLATE);
            var serviceTemplate = fileReader.Read(SERVICE_TEMPLATE);
            var materialsTemplate = fileReader.Read(MATERIAL_TEMPLATE);

            var updatedTemplate1 = ApplyHeaderAsync(quoteTemplate, document.Statement, document.Customer, logo64Base);
            var updatedTemplate2 = ApplySummaryAsync(updatedTemplate1, document.Statement);

            var statement = document.Statement;
            var servicesHtml = ApplyServices(statement.Services, serviceTemplate);

            var html = updatedTemplate2;
            var htmlWithServices = html.Replace(serviceTemplate, servicesHtml);

            var materialsBuilder = new StringBuilder();
            var services = statement.Services;

            string htmlWithMaterial = null;
            bool hasMaterials = false;

            foreach (var service in services)
            {
                string materialsHtml = string.Empty;

                if (service.Materials == null)
                {
                    service.Materials = new ObservableCollection<Material>();
                }

                if (service.Materials.Count > 0)
                {
                    hasMaterials = true;

                    string materialsTable = ApplyMaterials(service, materialsTemplate);
                    materialsBuilder.Append(materialsTable);

                    materialsHtml = materialsBuilder.ToString();

                    htmlWithMaterial = htmlWithServices.Replace(materialsTemplate, materialsHtml).Replace("~SERVICE_NAME~", string.Empty);
                }
            }

            if (hasMaterials)
            {
                var destinationFile = string.Format("{0}{1}", Guid.NewGuid(), HtmlFile);
                return fileReader.CreateFile(htmlWithMaterial, destinationFile);
            }
            else
            {
                var data = fileReader.Read(SERVICE_MATERIAL_TEMPLATE);
                bool exists = htmlWithServices.Contains(data);

                string noMaterialsHtml = htmlWithServices.Replace(data, string.Empty);
                var destinationFile = string.Format("{0}{1}", Guid.NewGuid(), HtmlFile);
                return fileReader.CreateFile(noMaterialsHtml, destinationFile);
            }
        }
        public IEnumerable <IRow> DetermineDeletes()
        {
            var input = _transforms.Aggregate(_inputReader.Read(), (current, transform) => current.Select(transform.Operate));

            return(_outputReader.Read().Except(input, new KeyComparer(_context.Entity.GetPrimaryKey())));
        }
Exemplo n.º 27
0
 public T Read(int key)
 {
     return(_read.Read(key));
 }
Exemplo n.º 28
0
      public IEnumerable<IRow> Read() {

         IRow row;
         var rows = _parentReader.Read().ToArray();  // 1 or 0 records

         if (rows.Length == 0) {
            rows = _defaultRowReader.Read().ToArray();
            if (rows.Length == 0) {
               yield break;
            }
         }

         row = rows[0];

         foreach (var field in _context.Entity.GetAllFields()) {

            Parameter p = null;
            if (_parameters.ContainsKey(field.Alias)) {
               p = _parameters[field.Alias];
            } else if (_parameters.ContainsKey(field.Name)) {
               p = _parameters[field.Name];
            }

            if (p != null && p.Value != null) {

               if (Constants.CanConvert()[field.Type](p.Value)) {

                  row[field] = field.InputType == "file" && p.Value == string.Empty ? row[field] : field.Convert(p.Value);
                  var len = field.Length.Equals("max", StringComparison.OrdinalIgnoreCase) ? int.MaxValue : Convert.ToInt32(field.Length);
                  if (p.Value != null && p.Value.Length > len) {
                     if (field.ValidField != string.Empty) {
                        var validField = _context.Entity.CalculatedFields.First(f => f.Alias == field.ValidField);
                        row[validField] = false;
                     }
                     if (field.MessageField != string.Empty) {
                        var messageField = _context.Entity.CalculatedFields.First(f => f.Alias == field.MessageField);
                        row[messageField] = $"This field is limited to {len} characters.  Anything more than that is truncated.|";
                     }
                  }
               } else {
                  if (field.ValidField != string.Empty) {
                     var validField = _context.Entity.CalculatedFields.First(f => f.Alias == field.ValidField);
                     row[validField] = false;
                  }
                  if (field.MessageField != string.Empty) {
                     var messageField = _context.Entity.CalculatedFields.First(f => f.Alias == field.MessageField);
                     switch (field.Type) {
                        case "char":
                           row[messageField] = "Must be a single chracter.|";
                           break;
                        case "byte":
                           row[messageField] = "Must be a whole number (not a fraction) between 0 and 255.|";
                           break;
                        case "bool":
                        case "boolean":
                           row[messageField] = "Must be true of false.|";
                           break;
                        case "int":
                        case "int32":
                           row[messageField] = "Must be a whole number (not a fraction) between −2,147,483,648 and 2,147,483,647.|";
                           break;
                        case "short":
                        case "int16":
                           row[messageField] = "Must be a whole number (not a fraction) between -32768 and 32767.|";
                           break;
                        case "long":
                        case "int64":
                           row[messageField] = "Must be a whole number (not a fraction) between -9,223,372,036,854,775,808 and 9,223,372,036,854,775,807.|";
                           break;
                        case "double":
                           row[messageField] = "Must be a number no more than 15 digits and between between 3.4E-38 and 3.4E+38.|";
                           break;
                        case "float":
                        case "single":
                           row[messageField] = "Must be a number no more than 7 digits and between between 1.7E-308 and 1.7E+308.|";
                           break;
                        case "decimal":
                           row[messageField] = $"Must be a number no more than {field.Precision} total digits, with {field.Scale} digits to the right of the decimal point.|";
                           break;
                        default:
                           row[messageField] = $"Can not convert {p.Value} to a {field.Type}.|";
                           break;
                     }

                  }
               }

            }

         }
         yield return row;

      }
Exemplo n.º 29
0
 public Validator(IRead reader)
 {
     exp = reader.Read().Split(" ").ToList();
 }
Exemplo n.º 30
0
 public void Read()
 {
     action.Read();
 }
Exemplo n.º 31
0
        public string ExecuteAsync(IRead fileReader, Document document)
        {
            Debug.Assert(document != null);
            Debug.Assert(document.Statement != null);

            var logo64Base = fileReader.GetImagebase64(document.Logo);

            var quoteTemplate     = fileReader.Read(TEMPLATE);
            var serviceTemplate   = fileReader.Read(SERVICE_TEMPLATE);
            var materialsTemplate = fileReader.Read(MATERIAL_TEMPLATE);

            var updatedTemplate1 = ApplyHeaderAsync(quoteTemplate, document.Statement, document.Customer, logo64Base);
            var updatedTemplate2 = ApplySummaryAsync(updatedTemplate1, document.Statement);

            var statement    = document.Statement;
            var servicesHtml = ApplyServices(statement.Services, serviceTemplate);

            var html             = updatedTemplate2;
            var htmlWithServices = html.Replace(serviceTemplate, servicesHtml);

            var materialsBuilder = new StringBuilder();
            var services         = statement.Services;

            string htmlWithMaterial = null;
            bool   hasMaterials     = false;

            foreach (var service in services)
            {
                string materialsHtml = string.Empty;

                if (service.Materials == null)
                {
                    service.Materials = new ObservableCollection <Material>();
                }

                if (service.Materials.Count > 0)
                {
                    hasMaterials = true;

                    string materialsTable = ApplyMaterials(service, materialsTemplate);
                    materialsBuilder.Append(materialsTable);

                    materialsHtml = materialsBuilder.ToString();

                    htmlWithMaterial = htmlWithServices.Replace(materialsTemplate, materialsHtml).Replace("~SERVICE_NAME~", string.Empty);
                }
            }

            if (hasMaterials)
            {
                var destinationFile = string.Format("{0}{1}", Guid.NewGuid(), HtmlFile);
                return(fileReader.CreateFile(htmlWithMaterial, destinationFile));
            }
            else
            {
                var  data   = fileReader.Read(SERVICE_MATERIAL_TEMPLATE);
                bool exists = htmlWithServices.Contains(data);

                string noMaterialsHtml = htmlWithServices.Replace(data, string.Empty);
                var    destinationFile = string.Format("{0}{1}", Guid.NewGuid(), HtmlFile);
                return(fileReader.CreateFile(noMaterialsHtml, destinationFile));
            }
        }
Exemplo n.º 32
0
 public string Read(string file) => _reader.Read(file);
 public IEnumerable <IRow> Read()
 {
     return(_internalReader.Read());
 }
Exemplo n.º 34
0
 public IEnumerable <IRow> Read()
 {
     return(_streamWriter.Read());
 }