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])); } }
public void spssGetNumberofCases() { int count = 0; SpssSafeWrapper.spssGetNumberofCases(handle, out count); Assert.Equal(138, count); }
/// <summary> /// Updates the SPSS data file with changes made to the collection. /// </summary> protected internal override void Update() { foreach (var pair in this) { SpssSafeWrapper.spssSetVarNValueLabel(FileHandle, Variable.Name, pair.Key, pair.Value); } }
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); }
private void AssumeIdentity(string varName) { if (varName == null || varName.Length == 0) { throw new ArgumentNullException("varName"); } ReturnCode result = SpssSafeWrapper.spssGetVarHandle(this.FileHandle, varName, out this.variableHandle); switch (result) { case ReturnCode.SPSS_OK: break; case ReturnCode.SPSS_DICT_NOTCOMMIT: // Header not yet saved, but that's ok. // Just remember the name of the variable break; case ReturnCode.SPSS_VAR_NOTFOUND: throw new ArgumentOutOfRangeException("varName", varName, "SPSS returned: " + result); default: throw new SpssException(result, "spssGetVarHandle"); } this.name = varName; }
/// <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; }
public void Dispose() { if (handle != 0) { SpssSafeWrapper.spssCloseRead(handle); handle = 0; } }
public void spssConvertDate() { const double expectedDate = 12598070400.0; double spssDate; SpssSafeWrapper.spssConvertDate(1, 1, 1982, out spssDate); Assert.Equal(expectedDate, spssDate); }
public void spssGetReleaseInfo() { int[] relInfo; ReturnCode result = SpssSafeWrapper.spssGetReleaseInfo(handle, out relInfo); Assert.Equal(12, relInfo[0]); Assert.Equal(0, relInfo[1]); Assert.Equal(0, relInfo[7]); }
/// <summary> /// Writes a newly added row to the data file. /// </summary> /// <remarks> /// This method should be called after a new row has had all its values set. /// After calling this method, variable values may not be changed. /// Without calling this method, the row is never actually written to the /// data file. /// </remarks> /// <example> /// To add a case to an existing SPSS file called mydata.sav, the following code could apply: /// <code> /// using( SpssDataDocument doc = SpssDataDocument.Open("mydata.sav", SpssFileAccess.Append) ) /// { /// SpssCase Case = doc.Cases.New(); /// Case["var1"] = 5; /// Case["var2"] = 3; /// Case["name"] = "Andrew"; /// Case.Commit(); /// } /// </code> /// </example> public void Commit() { this.Cases.Document.EnsureNotClosed(); if (this.Cases.IsReadOnly) { throw new InvalidOperationException("Not available when in read-only mode."); } SpssSafeWrapper.spssCommitCaseRecordImpl(this.Cases.FileHandle); this.Cases.OnCaseCommitted(); }
private static DateTime ConvertDoubleToDateTime(double v) { int sD, sM, sY, sd, sh, sm, ss, sms; double smsDbl; SpssSafeWrapper.spssConvertSPSSDateImpl(out sD, out sM, out sY, v); SpssSafeWrapper.spssConvertSPSSTimeImpl(out sd, out sh, out sm, out smsDbl, v); ss = (int)smsDbl; sms = (int)((smsDbl % 1.0) * 1000); return(new DateTime(sY, sM, sD, sh, sm, ss, sms)); }
/// <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(); }
public void spssGetVarInfo() { const int varIdx = 0; string varName = new string(' ', SpssSafeWrapper.SPSS_MAX_VARNAME + 1); int varType; ReturnCode result = SpssSafeWrapper.spssGetVarInfo(handle, varIdx, out varName, out varType); Assert.Equal(ReturnCode.SPSS_OK, result); Assert.Equal(Test1VarNames[varIdx], varName); Assert.Equal(0, varType); }
/// <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"); }
public void spssGetVarNames() { string[] varNames; int[] formatTypes; SpssSafeWrapper.spssGetVarNames(handle, out varNames, out formatTypes); Assert.Equal(Test1VarNames.Length, varNames.Length); Assert.Equal(Test1VarNames.Length, formatTypes.Length); for (int i = 0; i < Test1VarNames.Length; i++) { Assert.Equal(Test1VarNames[i], varNames[i]); } }
private static double ConvertDateTimeToDouble(DateTime value) { double d, t = 0; SpssSafeWrapper.spssConvertDate(value.Day, value.Month, value.Year, out d); double seconds = (double)(value.Second) + (value.Millisecond / 1000.0); SpssSafeWrapper.spssConvertTimeImpl(0, value.Hour, value.Minute, seconds, out t); double total = d + t; return(total); }
/// <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; }
/// <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; }
public void spssGetVarCValueLabels() { string varName = "charLabels"; string[] values; string[] labels; SpssSafeWrapper.spssGetVarCValueLabels(handle, varName, out values, out labels); Assert.Equal(2, values.Length); Assert.Equal(2, labels.Length); Assert.Equal("b", values[0].TrimEnd()); Assert.Equal("goodbye", labels[0]); Assert.Equal("h", values[1].TrimEnd()); Assert.Equal("hello", labels[1]); }
public void spssGetVarNValueLabels() { string varName = "numLabels"; double[] values; string[] labels; SpssSafeWrapper.spssGetVarNValueLabels(handle, varName, out values, out labels); Assert.Equal(23, values.Length); Assert.Equal(23, labels.Length); Assert.Equal(1d, values[0]); Assert.Equal("fattening", labels[0]); Assert.Equal(2d, values[1]); Assert.Equal("men", labels[1]); }
/// <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 }
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"); }
/// <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; }
/// <summary> /// When overridden in a derived class, executes the code required to free the handle. /// </summary> /// <returns> /// true if the handle is released successfully; otherwise, in the event of a catastrophic failure, false. In this case, it generates a releaseHandleFailed MDA Managed Debugging Assistant. /// </returns> protected override bool ReleaseHandle() { switch (this.accessMode) { case SpssFileAccess.Read: ReturnCode result = SpssSafeWrapper.spssCloseRead(this.handle.ToInt32()); return(result == ReturnCode.SPSS_OK); case SpssFileAccess.Append: result = SpssSafeWrapper.spssCloseAppendImpl(this.handle.ToInt32()); return(result == ReturnCode.SPSS_OK); case SpssFileAccess.Create: result = SpssSafeWrapper.spssCloseWriteImpl(this.handle.ToInt32()); return(result == ReturnCode.SPSS_OK || result == ReturnCode.SPSS_DICT_NOTCOMMIT); default: return(false); } }
/// <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); } }
/// <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]); } } }
/// <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); }
/// <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"); }
private void Document_DictionaryCommitted(object sender, EventArgs e) { // Set the variable handle SpssException.ThrowOnFailure(SpssSafeWrapper.spssGetVarHandle(this.FileHandle, this.Name, out this.variableHandle), "spssGetVarHandle"); }
public SpssSafeWrapperTest() { ReturnCode result = SpssSafeWrapper.spssOpenRead(TestBase.GoodFilename, out handle); Assert.Equal(ReturnCode.SPSS_OK, result); // Error opening SPSS file. }