Beispiel #1
0
        internal static SpssVariable LoadVariable(SpssVariablesCollection parent, string varName, int varType)
        {
            FormatTypeCode writeFormat, printFormat;
            int            writeDecimal, writeWidth, printDecimal, printWidth;

            SpssException.ThrowOnFailure(SpssSafeWrapper.spssGetVarWriteFormat(parent.Document.Handle, varName, out writeFormat, out writeDecimal, out writeWidth), "spssGetVarWriteFormat");
            SpssException.ThrowOnFailure(SpssSafeWrapper.spssGetVarPrintFormat(parent.Document.Handle, varName, out printFormat, out printDecimal, out printWidth), "spssGetVarPrintFormat");

            SpssVariable variable;

            switch (varType)
            {
            case 0:
                // This may be a date or a numeric
                if (SpssDateVariable.IsDateVariable(writeFormat))
                {
                    variable = new SpssDateVariable(parent, varName, writeFormat, writeWidth, printFormat, printWidth);
                }
                else
                {
                    variable = new SpssNumericVariable(parent, varName, writeFormat, writeDecimal, writeWidth, printFormat, printDecimal, printWidth);
                }

                break;

            default:
                Debug.Assert(varType == printWidth);
                variable = new SpssStringVariable(parent, varName, varType);
                break;
            }

            return(variable);
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SpssDataDocument"/> class
        /// and opens an existing SPSS file, or creates a new one.
        /// </summary>
        /// <param name="filename">The path of the file to open/create.</param>
        /// <param name="access">The desired file access.</param>
        protected SpssDataDocument(string filename, SpssFileAccess access)
        {
            this.Filename   = filename;
            this.AccessMode = access;
            int handle;

            switch (access)
            {
            case SpssFileAccess.Read:
                SpssException.ThrowOnFailure(SpssSafeWrapper.spssOpenRead(filename, out handle), "spssOpenRead");
                break;

            case SpssFileAccess.Append:
                SpssException.ThrowOnFailure(SpssSafeWrapper.spssOpenAppend(filename, out handle), "spssOpenAppend");
                break;

            case SpssFileAccess.Create:
                SpssException.ThrowOnFailure(SpssSafeWrapper.spssOpenWrite(filename, out handle), "spssOpenWrite");
                break;

            default:
                throw new ApplicationException("Unrecognized access level: " + access);
            }

            this.Handle = new SpssSafeHandle(handle, access);
            if (access == SpssFileAccess.Create)
            {
                this.IsCompressed = true;
            }

            this.IsAuthoringDictionary = true;
            this.Variables             = new SpssVariablesCollection(this);
            this.Cases = new SpssCasesCollection(this);
            this.IsAuthoringDictionary = access == SpssFileAccess.Create;
        }
Beispiel #3
0
        private void InitializeVariablesList()
        {
            Debug.Assert(this.FileHandle >= 0, "Must be working with an open file.");
            int initialSize;

            SpssException.ThrowOnFailure(SpssSafeWrapper.spssGetNumberofVariables(this.FileHandle, out initialSize), "spssGetNumberofVariables");
            this.variables       = new List <SpssVariable>(initialSize);
            this.variablesLookup = new SpssVariableKeyedCollection();

            string[]   varNames;
            int[]      varTypes;
            ReturnCode result = SpssException.ThrowOnFailure(SpssSafeWrapper.spssGetVarNames(this.FileHandle, out varNames, out varTypes), "spssGetVarNames", ReturnCode.SPSS_INVALID_FILE);

            if (result == ReturnCode.SPSS_INVALID_FILE)
            {
                // brand new file
                return;
            }

            Debug.Assert(varNames.Length == varTypes.Length);
            for (int i = 0; i < varNames.Length; i++)
            {
                this.Add(SpssVariable.LoadVariable(this, varNames[i], varTypes[i]));
            }
        }
Beispiel #4
0
        /// <summary>
        /// Commits the dictionary of a newly created SPSS file.
        /// </summary>
        /// <remarks>
        /// This method should be called after all variables
        /// have been added to the data file.
        /// </remarks>
        public void CommitDictionary()
        {
            this.EnsureAuthoringDictionary();
            this.Variables.Commit();

            SpssException.ThrowOnFailure(SpssSafeWrapper.spssCommitHeaderImpl(this.Handle), "spssCommitHeader");

            this.IsAuthoringDictionary = false;

            this.OnDictionaryCommitted();
        }
Beispiel #5
0
 /// <summary>
 /// Updates the changed attributes of the variable within SPSS.
 /// </summary>
 protected virtual void Update()
 {
     if (!IsInCollection)
     {
         return;                  // we'll get to do this later
     }
     SpssException.ThrowOnFailure(SpssSafeWrapper.spssSetVarLabel(FileHandle, Name, Label), "spssSetVarLabel");
     SpssException.ThrowOnFailure(SpssSafeWrapper.spssSetVarColumnWidth(FileHandle, Name, ColumnWidth), "spssSetVarColumnWidth");
     SpssException.ThrowOnFailure(SpssSafeWrapper.spssSetVarMeasureLevel(this.FileHandle, this.Name, this.MeasurementLevel), "spssSetVarMeasureLevel");
     SpssException.ThrowOnFailure(SpssSafeWrapper.spssSetVarAlignment(this.FileHandle, this.Name, this.Alignment), "spssSetVarAlignment");
 }
Beispiel #6
0
        /// <summary>
        /// Writes the variable's metadata out to the dictionary of the SPSS data file.
        /// </summary>
        protected internal void CommitToDictionary()
        {
            if (this.Handle >= 0)
            {
                throw new InvalidOperationException("Already committed.");
            }

            // Create the variable.
            SpssException.ThrowOnFailure(SpssSafeWrapper.spssSetVarName(this.FileHandle, this.Name, this.SpssType), "spssSetVarName");

            // Call the descending class to finish the details.
            this.Update();
            this.committedThisSession = true;
        }
Beispiel #7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SpssDateVariable"/> class.
        /// for use in loading variables from an existing SPSS data file.
        /// </summary>
        /// <param name="variables">The containing collection.</param>
        /// <param name="varName">The name of the variable.</param>
        /// <param name="writeFormat">The write format.</param>
        /// <param name="writeWidth">Width of the write.</param>
        /// <param name="printFormat">The print format.</param>
        /// <param name="printWidth">Width of the print.</param>
        protected internal SpssDateVariable(SpssVariablesCollection variables, string varName, FormatTypeCode writeFormat, int writeWidth, FormatTypeCode printFormat, int printWidth)
            : base(variables, varName)
        {
            MissingValueFormatCode formatCode;

            double[]   missingValues = new double[3];
            ReturnCode result        = SpssException.ThrowOnFailure(SpssSafeWrapper.spssGetVarNMissingValues(this.FileHandle, this.Name, out formatCode, out missingValues[0], out missingValues[1], out missingValues[2]), "spssGetVarNMissingValues");

            this.MissingValueFormat = formatCode;
            this.MissingValues      = new List <DateTime>(missingValues.Take(Math.Abs((int)formatCode)).Select(v => ConvertDoubleToDateTime(v)));
            this.writeFormat        = writeFormat;
            this.writeWidth         = writeWidth;
            this.printFormat        = printFormat;
            this.printWidth         = printWidth;
        }
        /// <summary>
        /// Initializes the value labels dictionary from the SPSS data file.
        /// </summary>
        protected override void LoadFromSpssFile()
        {
            double[]   values;
            string[]   labels;
            ReturnCode result = SpssException.ThrowOnFailure(SpssSafeWrapper.spssGetVarNValueLabels(FileHandle, Variable.Name, out values, out labels), "spssGetVarNValueLabels", ReturnCode.SPSS_NO_LABELS);

            if (result == ReturnCode.SPSS_OK)
            {
                Debug.Assert(values.Length == labels.Length);
                for (int i = 0; i < values.Length; i++)
                {
                    Add(values[i], labels[i]);
                }
            }

            // SPSS_NO_LABELs is nothing special -- just no labels to add
        }
Beispiel #9
0
        protected override void Update()
        {
            base.Update();

            double[] missingValues = new double[3];
            this.MissingValues.Select(v => ConvertDateTimeToDouble(v)).Take(missingValues.Length).ToArray().CopyTo(missingValues, 0);
            SpssException.ThrowOnFailure(SpssSafeWrapper.spssSetVarNMissingValues(
                                             this.FileHandle,
                                             this.Name,
                                             this.MissingValueFormat,
                                             missingValues[0],
                                             missingValues[1],
                                             missingValues[2]), "spssSetVarNMissingValues");

            SpssException.ThrowOnFailure(SpssSafeWrapper.spssSetVarPrintFormat(FileHandle, Name, this.PrintFormat, 4, this.PrintWidth), "spssSetVarPrintFormat");
            SpssException.ThrowOnFailure(SpssSafeWrapper.spssSetVarWriteFormat(FileHandle, Name, this.WriteFormat, 4, this.WriteWidth), "spssSetVarWriteFormat");
        }
Beispiel #10
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SpssNumericVariable"/> class
        /// for use in loading variables from an existing SPSS data file.
        /// </summary>
        /// <param name="variables">The containing collection.</param>
        /// <param name="varName">The name of the variable.</param>
        /// <param name="writeFormat">The write format.  The default is <see cref="FormatTypeCode.SPSS_FMT_F"/></param>
        /// <param name="writeDecimal">The write decimal.</param>
        /// <param name="writeWidth">Width of the write.</param>
        /// <param name="printFormat">The print format.  The default is <see cref="FormatTypeCode.SPSS_FMT_F"/></param>
        /// <param name="printDecimal">The print decimal.</param>
        /// <param name="printWidth">Width of the print.</param>
        protected internal SpssNumericVariable(SpssVariablesCollection variables, string varName, FormatTypeCode writeFormat, int writeDecimal, int writeWidth, FormatTypeCode printFormat, int printDecimal, int printWidth)
            : base(variables, varName)
        {
            this.valueLabels = new SpssNumericVariableValueLabelsDictionary(this);

            MissingValueFormatCode formatCode;

            double[]   missingValues = new double[3];
            ReturnCode result        = SpssException.ThrowOnFailure(SpssSafeWrapper.spssGetVarNMissingValues(this.FileHandle, this.Name, out formatCode, out missingValues[0], out missingValues[1], out missingValues[2]), "spssGetVarNMissingValues");

            this.MissingValueFormat = formatCode;
            this.MissingValues      = new List <double>(missingValues.Take(Math.Abs((int)formatCode)));
            this.writeDecimal       = writeDecimal;
            this.writeWidth         = writeWidth;
            this.writeFormat        = writeFormat;
            this.printDecimal       = printDecimal;
            this.printWidth         = printWidth;
            this.printFormat        = printFormat;
        }
Beispiel #11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SpssStringVariable"/> class
        /// for use in loading variables from an existing SPSS data file.
        /// </summary>
        /// <param name="variables">The containing collection.</param>
        /// <param name="varName">The name of the variable being loaded.</param>
        /// <param name="length">The length of the string variable.  This is the same as SpssType</param>
        protected internal SpssStringVariable(SpssVariablesCollection variables, string varName, int length)
            : base(variables, varName)
        {
            this.valueLabels = new SpssStringVariableValueLabelsDictionary(this);
            this.length      = length;

            MissingValueFormatCode formatCode;

            string[]   missingValues = new string[3];
            ReturnCode result        = SpssException.ThrowOnFailure(SpssSafeWrapper.spssGetVarCMissingValues(this.FileHandle, this.Name, out formatCode, out missingValues[0], out missingValues[1], out missingValues[2]), "spssGetVarCMissingValues", ReturnCode.SPSS_SHORTSTR_EXP);

            if (result == ReturnCode.SPSS_OK)
            {
                this.MissingValues = new List <string>(missingValues.Take((int)formatCode));
            }
            else
            {
                this.MissingValues = new List <string>(0);
            }
        }
Beispiel #12
0
        /// <summary>
        /// Initializes the value labels dictionary from the SPSS data file.
        /// </summary>
        protected override void LoadFromSpssFile()
        {
            if (!this.Applies)
            {
                return;
            }

            string[]   values;
            string[]   labels;
            ReturnCode result = SpssException.ThrowOnFailure(SpssSafeWrapper.spssGetVarCValueLabels(this.FileHandle, this.Variable.Name, out values, out labels), "spssGetVarCValueLabels", ReturnCode.SPSS_NO_LABELS);

            if (result == ReturnCode.SPSS_OK)
            { // ReturnCode.SPSS_NO_LABELS is nothing special -- just no labels to add
                Debug.Assert(values.Length == labels.Length);
                for (int i = 0; i < values.Length; i++)
                {
                    this.Add(values[i], labels[i]);
                }
            }
        }
Beispiel #13
0
        /// <summary>
        /// Updates the changed attributes of the variable within SPSS.
        /// </summary>
        protected override void Update()
        {
            base.Update();

            if (!IsInCollection)
            {
                return;                  // we'll get to do this later
            }
            this.valueLabels.Update();
            string[] missingValues = new string[3];
            this.MissingValues.Take(missingValues.Length).ToArray().CopyTo(missingValues, 0);

            // We allow failure due to long string var types only if we're not actually setting any missing values.
            ReturnCode[] allowedReturnCodes = this.MissingValues.Count > 0 ? new ReturnCode[0] : new ReturnCode[] { ReturnCode.SPSS_SHORTSTR_EXP };
            SpssException.ThrowOnFailure(SpssSafeWrapper.spssSetVarCMissingValues(
                                             this.FileHandle,
                                             this.Name,
                                             (MissingValueFormatCode)Math.Min(this.MissingValues.Count, missingValues.Length),
                                             missingValues[0],
                                             missingValues[1],
                                             missingValues[2]), "spssSetVarCMissingValues", allowedReturnCodes);
        }
Beispiel #14
0
        /// <summary>
        /// Updates details of the variable.
        /// </summary>
        protected override void Update()
        {
            base.Update();

            if (!this.IsInCollection)
            {
                return; // we'll get to do this later
            }

            this.valueLabels.Update();
            double[] missingValues = new double[3];
            this.MissingValues.Take(missingValues.Length).ToArray().CopyTo(missingValues, 0);
            SpssException.ThrowOnFailure(SpssSafeWrapper.spssSetVarNMissingValues(
                                             this.FileHandle,
                                             this.Name,
                                             this.MissingValueFormat,
                                             missingValues[0],
                                             missingValues[1],
                                             missingValues[2]), "spssSetVarNMissingValues");

            SpssException.ThrowOnFailure(SpssSafeWrapper.spssSetVarPrintFormat(this.FileHandle, this.Name, this.PrintFormat, this.PrintDecimal, this.PrintWidth), "spssSetVarPrintFormat");
            SpssException.ThrowOnFailure(SpssSafeWrapper.spssSetVarWriteFormat(this.FileHandle, this.Name, this.WriteFormat, this.WriteDecimal, this.WriteWidth), "spssSetVarWriteFormat");
        }
Beispiel #15
0
 private void Document_DictionaryCommitted(object sender, EventArgs e)
 {
     // Set the variable handle
     SpssException.ThrowOnFailure(SpssSafeWrapper.spssGetVarHandle(this.FileHandle, this.Name, out this.variableHandle), "spssGetVarHandle");
 }