00001 /* 00002 * This file is part of the Standard Portable Library (SPL). 00003 * 00004 * SPL is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation, either version 3 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * SPL is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with SPL. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 /*using System; 00018 using System.Collections; 00019 using System.Diagnostics; 00020 using System.Text; 00021 00025 public class FixedFieldDef 00026 { 00035 public FixedFieldDef( string name, int len, int recposition, int ordnal, char fill ) 00036 { 00037 m_name = name; 00038 m_len = len; 00039 m_recpos = recposition; 00040 m_fieldpos = ordnal; 00041 m_templateData = new string( fill, m_len ); 00042 } 00043 00052 public FixedFieldDef( string name, int len, int recposition, int ordnal, string defaultData ) 00053 { 00054 Debug.Assert( defaultData.Length == len ); 00055 if ( defaultData.Length != len ) 00056 { 00057 throw new Exception(name + " incorrect length" ); 00058 } 00059 m_name = name; 00060 m_len = len; 00061 m_recpos = recposition; 00062 m_fieldpos = ordnal; 00063 m_templateData = defaultData; 00064 } 00065 00069 public int Length 00070 { 00071 get 00072 { 00073 return m_len; 00074 } 00075 } 00076 00080 public string Name 00081 { 00082 get 00083 { 00084 return m_name; 00085 } 00086 } 00087 00091 public string DefaultData 00092 { 00093 get 00094 { 00095 return m_templateData; 00096 } 00097 } 00098 00102 public int OrdinalPosition 00103 { 00104 get 00105 { 00106 return m_fieldpos; 00107 } 00108 } 00109 00115 public string Parse( string record ) 00116 { 00117 Debug.Assert( m_recpos + m_len <= record.Length, m_name + " record too short" ); 00118 return record.Substring(m_recpos, m_len); 00119 } 00120 00121 protected string m_name; 00122 protected int m_len; 00123 protected int m_recpos; 00125 protected int m_fieldpos; 00126 protected string m_templateData; 00127 } 00128 00132 public class FixedRecordDef 00133 { 00138 public FixedRecordDef( int linelen ) 00139 { 00140 m_linelen = linelen; 00141 } 00142 00149 public void AddField( string name, int len, string defData ) 00150 { 00151 FixedFieldDef fld = new FixedFieldDef( name, len, m_recordEnd, m_fields.Count, defData ); 00152 AddField( fld ); 00153 } 00154 00161 public void AddField( string name, int len, char fill ) 00162 { 00163 FixedFieldDef fld = new FixedFieldDef( name, len, m_recordEnd, m_fields.Count, fill ); 00164 AddField( fld ); 00165 } 00166 00171 private void AddField( FixedFieldDef fld ) 00172 { 00173 m_recordEnd += fld.Length; 00174 Debug.Assert( m_recordEnd <= m_linelen, "Field exceeds max record size" ); 00175 00176 m_fields.Add( fld ); 00177 m_fieldIndex.Add( fld.Name, fld ); 00178 } 00179 00185 public FixedRecord Parse( string record ) 00186 { 00187 FixedFieldData[] data = new FixedFieldData[ m_fields.Count ]; 00188 00189 for ( int x = 0; x < m_fields.Count; x++ ) 00190 { 00191 data[x] = new FixedFieldData( (FixedFieldDef)m_fields[x], record ); 00192 } 00193 return new FixedRecord( this, data ); 00194 } 00195 00200 public FixedRecord Create( ) 00201 { 00202 FixedFieldData[] data = new FixedFieldData[ m_fields.Count ]; 00203 00204 for ( int x = 0; x < m_fields.Count; x++ ) 00205 { 00206 data[x] = new FixedFieldData( (FixedFieldDef)m_fields[x] ); 00207 } 00208 return new FixedRecord( this, data ); 00209 } 00210 00215 public FixedRecord CreateCached() 00216 { 00217 lock ( m_cache ) 00218 { 00219 if ( m_cache.Count == 0 ) 00220 { 00221 return Create(); 00222 } 00223 FixedRecord rcd = (FixedRecord)m_cache[ m_cache.Count-1 ]; 00224 m_cache.RemoveAt( m_cache.Count - 1 ); 00225 return rcd; 00226 } 00227 } 00228 00233 public void ReleaseCached( FixedRecord rcd ) 00234 { 00235 lock( m_cache ) 00236 { 00237 rcd.Reset(); 00238 m_cache.Add( rcd ); 00239 } 00240 } 00241 00247 public int GetFieldOrdinal( string name ) 00248 { 00249 return ((FixedFieldDef)m_fieldIndex[ name ]).OrdinalPosition; 00250 } 00251 00252 protected Hashtable m_fieldIndex = new Hashtable(); 00253 protected ArrayList m_fields = new ArrayList(); 00254 protected int m_linelen; 00255 protected int m_recordEnd; 00256 00257 protected ArrayList m_cache = new ArrayList(); 00258 } 00259 00263 public class FixedFieldData 00264 { 00269 public FixedFieldData( FixedFieldDef field ) 00270 { 00271 m_field = field; 00272 m_data = field.DefaultData; 00273 } 00274 00280 public FixedFieldData( FixedFieldDef field, string record ) 00281 { 00282 m_field = field; 00283 m_data = field.Parse( record ); 00284 } 00285 00289 public string Value 00290 { 00291 get 00292 { 00293 return m_data; 00294 } 00295 } 00296 00301 public Date ParseDate() 00302 { 00303 Debug.Assert( m_field.Length == 6 ); 00304 string mm = m_data.Substring(0, 2); 00305 string dd = m_data.Substring(2, 2); 00306 string yy = m_data.Substring(4, 2); 00307 00308 int iyy = Int32.Parse( yy ); 00309 if ( iyy > 50 ) 00310 { 00311 iyy += 1900; 00312 } 00313 else 00314 { 00315 iyy += 2000; 00316 } 00317 return new Date( iyy, Int32.Parse(mm), Int32.Parse(dd) ); 00318 } 00319 00324 public int ParseInt() 00325 { 00326 string i = StripZeros(m_data).Trim(); 00327 if ( i.Length == 0 ) 00328 { 00329 i = "0"; 00330 } 00331 return Int32.Parse( i ); 00332 } 00333 00338 public decimal ParseMoney() 00339 { 00340 char sign = m_data[0]; 00341 Debug.Assert( sign == '+' || sign == '-', "Invalid money format " + m_data ); 00342 decimal amt = Decimal.Parse( m_data.Substring( 1 ) ) / 100; 00343 if ( sign == '-' ) 00344 { 00345 return -amt; 00346 } 00347 return amt; 00348 } 00349 00354 public decimal ParseDecimal() 00355 { 00356 return Decimal.Parse( StripZeros(m_data) ); 00357 } 00358 00363 public DateTime ParseDateTime() 00364 { 00365 int yyyy = Int32.Parse( m_data.Substring(0, 4) ); 00366 int mm = Int32.Parse( m_data.Substring(5, 2) ); 00367 int dd = Int32.Parse( m_data.Substring(8, 2) ); 00368 00369 string timepart = m_data.Substring(11); 00370 string[] tps = timepart.Split(new char[] {':'}); 00371 return new DateTime(yyyy, mm, dd, Int32.Parse(tps[0]), Int32.Parse(tps[1]), (int)Double.Parse(tps[2])); 00372 } 00373 00378 public int ParseYear() 00379 { 00380 Debug.Assert( m_field.Length == 2 || m_field.Length == 4 ); 00381 if ( m_field.Length == 4 ) 00382 { 00383 return ParseInt(); 00384 } 00385 int yy = Int32.Parse( m_data ); 00386 if ( yy > 50 ) 00387 { 00388 return yy + 1900; 00389 } 00390 return yy + 2000; 00391 } 00392 00397 public void SetDate( Date dt ) 00398 { 00399 Debug.Assert( m_field.Length == 6 ); 00400 m_data = dt.Format("MMDDYY"); 00401 } 00402 00407 public void SetIntLeft( int i ) 00408 { 00409 m_data = Right((m_field.DefaultData + i.ToString()), m_field.Length); 00410 00411 Debug.Assert( m_data.Length == m_field.Length ); 00412 if ( m_data.Length != m_field.Length ) 00413 { 00414 throw new Exception( m_field.Name + " incorrect length" ); 00415 } 00416 } 00417 00418 public void SetSignedIntLeft( int i ) 00419 { 00420 char sign = '+'; 00421 if ( i < 0 ) 00422 { 00423 sign = '-'; 00424 i = -i; 00425 } 00426 m_data = sign + Right(m_field.DefaultData + i.ToString(), m_field.Length-1); 00427 Debug.Assert(m_data.Length == m_field.Length, "Assumption failure"); 00428 00429 Debug.Assert( m_data.Length == m_field.Length ); 00430 if ( m_data.Length != m_field.Length ) 00431 { 00432 throw new Exception( m_field.Name + " incorrect length" ); 00433 } 00434 } 00435 00440 public void Set( string val ) 00441 { 00442 Debug.Assert( val.Length == m_field.Length ); 00443 m_data = val; 00444 00445 Debug.Assert( m_data.Length == m_field.Length ); 00446 if ( m_data.Length != m_field.Length ) 00447 { 00448 throw new Exception( m_field.Name + " incorrect length" ); 00449 } 00450 } 00451 00456 public void SetMoney( decimal val ) 00457 { 00458 char sign = '+'; 00459 if ( val < 0 ) 00460 { 00461 sign = '-'; 00462 val = -val; 00463 } 00464 m_data = sign + Right((m_field.DefaultData + val.ToString("#0.00").Replace(".", "")), m_field.Length-1); 00465 Debug.Assert(m_data.Length == m_field.Length, "Assumption failure"); 00466 00467 Debug.Assert( m_data.Length == m_field.Length ); 00468 if ( m_data.Length != m_field.Length ) 00469 { 00470 throw new Exception( m_field.Name + " incorrect length" ); 00471 } 00472 } 00473 00478 public void SetLeft( string val ) 00479 { 00480 m_data = Right((m_field.DefaultData + val), m_field.Length); 00481 00482 Debug.Assert( m_data.Length == m_field.Length ); 00483 if ( m_data.Length != m_field.Length ) 00484 { 00485 throw new Exception( m_field.Name + " incorrect length" ); 00486 } 00487 } 00488 00493 public void SetDateTime( DateTime dtm ) 00494 { 00495 Debug.Assert( m_field.Length == 26, "Invalid field size" ); 00496 m_data = dtm.ToString("yyyy-MM-dd:HH:mm:ss.000000"); 00497 Debug.Assert( m_data.Length == 26 ); 00498 00499 Debug.Assert( m_data.Length == m_field.Length ); 00500 if ( m_data.Length != m_field.Length ) 00501 { 00502 throw new Exception( m_field.Name + " incorrect length" ); 00503 } 00504 } 00505 00510 public void SetDateTime( Date dt ) 00511 { 00512 Debug.Assert( m_field.Length == 26, "Invalid field size" ); 00513 m_data = dt.Year + "-" + dt.Month.ToString("00") + "-" + dt.Day.ToString("00") + ":00:00:00.000000"; 00514 Debug.Assert( m_data.Length == 26 ); 00515 00516 Debug.Assert( m_data.Length == m_field.Length ); 00517 if ( m_data.Length != m_field.Length ) 00518 { 00519 throw new Exception( m_field.Name + " incorrect length" ); 00520 } 00521 } 00522 00527 public void SetDecimal ( decimal val ) 00528 { 00529 m_data = Right((m_field.DefaultData + val.ToString()), m_field.Length); 00530 00531 Debug.Assert( m_data.Length == m_field.Length ); 00532 if ( m_data.Length != m_field.Length ) 00533 { 00534 throw new Exception( m_field.Name + " incorrect length" ); 00535 } 00536 } 00537 00543 public void SetSpecial( string val ) 00544 { 00545 val = val.Trim(); 00546 if ( val.Length == 0 ) 00547 { 00548 m_data = m_field.DefaultData; 00549 return; 00550 } 00551 int ival = 0; 00552 if ( IsNumeric( val, ref ival ) ) 00553 { 00554 // left justify 00555 SetIntLeft( ival ); 00556 } 00557 else 00558 { 00559 // right justify 00560 m_data = (val + m_field.DefaultData).Substring(0, m_field.Length); 00561 } 00562 00563 Debug.Assert( m_data.Length == m_field.Length ); 00564 if ( m_data.Length != m_field.Length ) 00565 { 00566 throw new Exception( m_field.Name + " incorrect length" ); 00567 } 00568 } 00569 00576 internal static bool IsNumeric( string str, ref int i ) 00577 { 00578 try 00579 { 00580 i = Int32.Parse( str ); 00581 return true; 00582 } 00583 catch ( Exception *ex ) 00584 { 00585 delete ex; 00586 return false; 00587 } 00588 } 00589 00596 internal static string Right( string str, int len ) 00597 { 00598 if ( str.Length <= len ) 00599 { 00600 return str; 00601 } 00602 return str.Substring(str.Length - len); 00603 } 00604 00612 internal static string PadSubstring( string str, int start, int len ) 00613 { 00614 if ( len + start > str.Length ) 00615 { 00616 string part = str.Substring( start, str.Length - start ); 00617 return part + (new string( ' ', len - part.Length )); 00618 } 00619 return str.Substring( start, len ); 00620 } 00621 00627 internal static string StripZeros( string str ) 00628 { 00629 string ret = ""; 00630 bool copying = false; 00631 00632 for ( int x = 0; x < str.Length; x++ ) 00633 { 00634 if ( !copying && str[x] == '0' ) 00635 { 00636 ret += ' '; 00637 } 00638 else 00639 { 00640 copying = true; 00641 ret += str[x]; 00642 } 00643 } 00644 return ret; 00645 } 00646 00650 public void Reset() 00651 { 00652 m_data = m_field.DefaultData; 00653 Debug.Assert( m_data.Length == m_field.Length ); 00654 00655 if ( m_data.Length != m_field.Length ) 00656 { 00657 throw new Exception( m_field.Name + " incorrect length" ); 00658 } 00659 } 00660 00661 protected FixedFieldDef m_field; 00662 protected string m_data; 00663 } 00664 00668 public class FixedRecord 00669 { 00675 public FixedRecord ( FixedRecordDef def, FixedFieldData[] fields ) 00676 { 00677 m_recdef = def; 00678 m_data = fields; 00679 } 00680 00684 public int Count 00685 { 00686 get 00687 { 00688 return m_data.Length; 00689 } 00690 } 00691 00695 public FixedFieldData this[ int index ] 00696 { 00697 get 00698 { 00699 return m_data[index]; 00700 } 00701 } 00702 00706 public FixedFieldData this[ string name ] 00707 { 00708 get 00709 { 00710 return m_data[ m_recdef.GetFieldOrdinal( name ) ]; 00711 } 00712 } 00713 00717 public void Reset() 00718 { 00719 for( int x = 0; x < m_data.Length; x++ ) 00720 { 00721 m_data[x].Reset(); 00722 } 00723 } 00724 00729 public override string ToString() 00730 { 00731 StringBuilder buf = new StringBuilder(); 00732 for( int x = 0; x < m_data.Length; x++ ) 00733 { 00734 buf.Append( m_data[x].Value ); 00735 } 00736 return buf.ToString() + "\r\n"; 00737 } 00738 00739 protected FixedRecordDef m_recdef; 00740 protected FixedFieldData[] m_data; 00741 } 00742 00743 */ 00744