Beispiel #1
0
        public static List <ShortValueLabels> GenerateLabelIndexes(IEnumerable <VariableWrapper> variables)
        {
            var result          = new List <ShortValueLabels>();
            var dictionaryIndex = 1;

            foreach (var variable in variables)
            {
                var isString = variable.FormatType == FormatType.A;
                if (variable.ValueLabels != null && variable.ValueLabels.Any() && (!isString || variable.ValueLength <= 8))
                {
                    var valueLabel = new ShortValueLabels
                    {
                        Labels        = variable.ValueLabels !.ToDictionary(p => p.Key, p => p.Value),
                        VariableIndex = new List <int> {
                            dictionaryIndex
                        }
                    };
                    result.Add(valueLabel);
                }

                dictionaryIndex += isString ? SpssMath.GetNumberOf32ByteBlocks(variable.ValueLength) : 1;
            }

            return(result);
        }
    }
        public void ReadVariableRecord()
        {
            var valueLength      = _reader.ReadInt32();
            var hasVariableLabel = _reader.ReadInt32() == 1;
            var missingValueType = _reader.ReadInt32();

            _reader.ReadInt32(); //print format
            var decimalPlaces = _reader.ReadByte();
            var spssWidth     = _reader.ReadByte();
            var formatType    = _reader.ReadByte();

            _reader.ReadByte();
            var shortName = _reader.ReadBytes(8);

            var properties = new VariableProperties {
                FormatType = (FormatType)formatType, Index = _currentIndex, MissingValueType = missingValueType, ShortName = shortName, DecimalPlaces = decimalPlaces
            };

            if (hasVariableLabel)
            {
                properties.Label = ReadLabel();
            }
            if (Math.Abs(missingValueType) != 0)
            {
                ReadMissing(properties);
            }

            var blockWidth = ReadBlankRecords(valueLength);

            properties.ValueLength = blockWidth;
            properties.SpssWidth   = formatType == (int)FormatType.A ? blockWidth : spssWidth;
            _metadataInfo.Variables.Add(properties);
            _currentIndex += SpssMath.GetNumberOf32ByteBlocks(blockWidth);
        }
Beispiel #3
0
        public static List <DisplayParameter> GenerateDisplayValues(List <VariableWrapper> variables)
        {
            List <DisplayParameter> displayValues = new List <DisplayParameter>();

            foreach (var variable in variables)
            {
                var displayValue = new DisplayParameter {
                    Measure = variable.Measure, Columns = variable.Columns, Alignment = variable.Alignment
                };
                var namedVariables = SpssMath.GetNumberOfGhostVariables(variable.ValueLength) + 1;
                displayValues.AddRange(Enumerable.Range(1, namedVariables).Select(_ => displayValue));
            }

            return(displayValues);
        }
        private int ReadBlankRecords(int valueLength)
        {
            var lengthTotal = 0;

            if (valueLength <= 8)
            {
                return(8);
            }
            var skip = SpssMath.GetAllocatedSize(valueLength);

            lengthTotal += skip == 256 ? 252 : valueLength;
            skip        -= 8;
            _reader.BaseStream.Seek(skip * 4, SeekOrigin.Current);
            return(lengthTotal);
        }
Beispiel #5
0
        private void WriteString(string value, int valueLength)
        {
            var rawVariableLength = SpssMath.GetAllocatedSize(valueLength);
            var bytes             = _encoding.GetBytes(value.TrimEnd(), valueLength);
            var byteLength        = bytes.Length;

            WriteStringBytes(bytes);
            _writer.AddPadding();
            var writtenBytes = byteLength / 255 * 256 + (byteLength % 255 + 7) / 8 * 8;

            while (writtenBytes < rawVariableLength)
            {
                _writer.WriteCompressedCode(CompressedCode.SpaceCharsBlock);
                writtenBytes += 8;
            }
        }
Beispiel #6
0
        public void WriteHeaderRecord(Metadata metadata, List <VariableWrapper> variablesCount)
        {
            var count = variablesCount.Select(x => SpssMath.GetNumberOf32ByteBlocks(x.ValueLength)).Aggregate((result, blocks) => result + blocks);

            _writer.Write((int)RecordType.HeaderRecord);
            _writer.Write(_encoding.GetPaddedValueAsByteArray("@(#) IBM SPSS STATISTICS MS Windows 25.0.0.0", 60));
            _writer.Write(2); //LayoutCode
            _writer.Write(count);
            _writer.Write(1); //compressed
            _writer.Write(0); //WeightIndex
            _writer.Write(metadata.Cases);
            _writer.Write((double)metadata.Bias);
            _writer.Write(DateTime.Now.ToString("dd MMM yyHH:mm:ss", _invariantCultureDateTimeFormat).ToCharArray());
            _writer.Write(_encoding.GetPaddedValueAsByteArray(string.Empty, 64));
            _writer.Write(new byte[3]);
        }
Beispiel #7
0
        private void WriteBlankAndGhostRecords(VariableWrapper variable)
        {
            var extraRecordCount = variable.FormatType != FormatType.A ? 0 : SpssMath.GetNumberOf32ByteBlocks(variable.ValueLength);

            for (var i = 1; i < extraRecordCount; i++)
            {
                if (i % 32 != 0)
                {
                    WriteBlankRecord();
                    continue;
                }

                var ghostIndex = i / 32;
                var len        = ghostIndex == variable.GhostNames.Count ? variable.LastGhostVariableLength : 255;
                WriteGhostRecord(variable.GhostNames[ghostIndex - 1], len, variable.Label, variable.MissingValuesObject);
            }
        }
Beispiel #8
0
 private void GenerateVariableGhostNames(List <VariableWrapper> records)
 {
     foreach (var record in records)
     {
         if (record.ValueLength < 256)
         {
             continue;
         }
         var ghostVariables = SpssMath.GetNumberOfGhostVariables(record.ValueLength);
         record.LastGhostVariableLength = record.ValueLength % 252;
         for (var j = 0; j < ghostVariables; j++)
         {
             var sn = _encoding.GetString(GetShortNameByteArray(record.Name)).TrimEnd(); const int maxLengthPrefix = 5;
             var prefix    = _encoding.GetString(sn, maxLengthPrefix);
             var shortName = _encoding.GetPaddedValueAsByteArray(prefix + '0', 8);
             record.GhostNames.Add(GetUniqueShortName(shortName, index => $"{prefix}{GetGhostSuffix(index)}"));
         }
     }
 }