PageRenderTime 25ms CodeModel.GetById 15ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/Spss/SpssStringVariable.cs

#
C# | 152 lines | 89 code | 20 blank | 43 comment | 11 complexity | 3af31c00c374f31859727a4ebaf5606c MD5 | raw file
  1//-----------------------------------------------------------------------
  2// <copyright file="SpssStringVariable.cs" company="Andrew Arnott">
  3//     Copyright (c) Andrew Arnott. All rights reserved.
  4//     Copyright (c) Brigham Young University
  5// </copyright>
  6//-----------------------------------------------------------------------
  7
  8namespace Spss {
  9	using System;
 10	using System.Collections.Generic;
 11	using System.Linq;
 12
 13	/// <summary>
 14	/// Represents an SPSS data variable that stores character string information.
 15	/// </summary>
 16	public class SpssStringVariable : SpssVariable {
 17		private readonly SpssStringVariableValueLabelsDictionary valueLabels;
 18
 19		private int length = -1;
 20
 21		/// <summary>
 22		/// Creates an instance of the <see cref="SpssStringVariable"/> class,
 23		/// for use when defining a new variable.
 24		/// </summary>
 25		public SpssStringVariable() {
 26			this.valueLabels = new SpssStringVariableValueLabelsDictionary(this);
 27			this.MissingValues = new List<string>(3);
 28		}
 29
 30		/// <summary>
 31		/// Creates an instance of the <see cref="SpssStringVariable"/> class,
 32		/// for use in loading variables from an existing SPSS data file.
 33		/// </summary>
 34		/// <param name="variables">The containing collection.</param>
 35		/// <param name="varName">The name of the variable being loaded.</param>
 36		/// <param name="length">The length of the string variable.  This is the same as SpssType</param>
 37		protected internal SpssStringVariable(SpssVariablesCollection variables, string varName, int length)
 38			: base(variables, varName) {
 39			this.valueLabels = new SpssStringVariableValueLabelsDictionary(this);
 40			this.length = length;
 41
 42			MissingValueFormatCode formatCode;
 43			string[] missingValues = new string[3];
 44			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);
 45			if (result == ReturnCode.SPSS_OK) {
 46				this.MissingValues = new List<string>(missingValues.Take((int)formatCode));
 47			} else {
 48				this.MissingValues = new List<string>(0);
 49			}
 50		}
 51
 52		/// <summary>
 53		/// Gets the maximum length a string in this variable can be.
 54		/// </summary>
 55		public int Length {
 56			get {
 57				return length >= 0 ? length : ColumnWidth;
 58			}
 59
 60			set {
 61				VerifyNotCommittedVariable();
 62				if (value < 0) throw new ArgumentOutOfRangeException("Length", value, "Must be a non-negative number.");
 63
 64				length = value;
 65			}
 66		}
 67
 68		/// <summary>
 69		/// Gets or sets the missing values for this variable.
 70		/// </summary>
 71		/// <value>The missing values.</value>
 72		/// <remarks>
 73		/// A maximum of three maximum values may be supplied.
 74		/// </remarks>
 75		public IList<string> MissingValues { get; private set; }
 76
 77		/// <summary>
 78		/// Gets the SPSS type for the variable.
 79		/// </summary>
 80		public override int SpssType {
 81			get {
 82				return Length;
 83			}
 84		}
 85
 86		/// <summary>
 87		/// Gets or sets the data value of this variable within a specific case.
 88		/// </summary>
 89		internal new string Value {
 90			get {
 91				string v;
 92				SpssException.ThrowOnFailure(SpssSafeWrapper.spssGetValueChar(FileHandle, Handle, out v), "spssGetValueChar");
 93				return v;
 94			}
 95			set {
 96				if (value == null) value = string.Empty;
 97				if (value.Length > Length)
 98					throw new ArgumentOutOfRangeException("Value", value, "String too long for variable " + Name + ".  Maximum length is: " + Length);
 99				SpssSafeWrapper.spssSetValueChar(FileHandle, Handle, value);
100			}
101		}
102
103		/// <summary>
104		/// The set of value labels (response values and labels) that are defined.
105		/// </summary>
106		public IDictionary<string, string> ValueLabels {
107			get { return this.valueLabels; }
108		}
109
110		/// <summary>
111		/// Updates the changed attributes of the variable within SPSS.
112		/// </summary>
113		protected override void Update() {
114			base.Update();
115
116			if (!IsInCollection) return; // we'll get to do this later
117
118			this.valueLabels.Update();
119			string[] missingValues = new string[3];
120			this.MissingValues.Take(missingValues.Length).ToArray().CopyTo(missingValues, 0);
121
122			// We allow failure due to long string var types only if we're not actually setting any missing values.
123			ReturnCode[] allowedReturnCodes = this.MissingValues.Count > 0 ? new ReturnCode[0] : new ReturnCode[] { ReturnCode.SPSS_SHORTSTR_EXP };
124			SpssException.ThrowOnFailure(SpssSafeWrapper.spssSetVarCMissingValues(
125				this.FileHandle,
126				this.Name,
127				(MissingValueFormatCode)Math.Min(this.MissingValues.Count, missingValues.Length),
128				missingValues[0],
129				missingValues[1],
130				missingValues[2]), "spssSetVarCMissingValues", allowedReturnCodes);
131		}
132
133		public override SpssVariable Clone() {
134			SpssStringVariable other = new SpssStringVariable();
135			CloneTo(other);
136			return other;
137		}
138
139		protected override void CloneTo(SpssVariable spssVar) {
140			base.CloneTo(spssVar);
141			SpssStringVariable other = spssVar as SpssStringVariable;
142			if (other == null)
143				throw new ArgumentException("Must be of type " + GetType().Name + ".", "other");
144			other.Length = Length;
145			this.valueLabels.CopyTo(other.valueLabels);
146		}
147
148		protected override bool IsApplicableFormatTypeCode(FormatTypeCode formatType) {
149			return formatType == FormatTypeCode.SPSS_FMT_A;
150		}
151	}
152}