home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Programming Contest / ~Solutions Submitted / Problem 04 - Three and One fix / Solution.p < prev   
Encoding:
Text File  |  1998-06-19  |  4.0 KB  |  119 lines  |  [TEXT/CWIE]

  1. (*
  2. Problem 04 - Text Processing
  3.  
  4. const
  5.     kMaxHandleSize = 1000000;
  6. const
  7.     kActionMove = 1;
  8.     kActionInsert = 2;
  9.     kActionDelete = 3;
  10.     kActionSearch = 4;
  11.     kActionSearchAndReplace = 5;
  12. const
  13.     kFlagCaseSensitiveBit = 0;
  14.     kFlagGlobalBit = 1;
  15.     kFlagBackwardsBit = 2;
  16. type
  17.   ActionRecord = record
  18.     action: UInt32;
  19.     amount: SInt32;
  20.     flags: UInt32;
  21.     search: Str255;
  22.     replace: Str255;
  23.   end;
  24.   ActionRecordArray = array[0..0] of ActionRecord;
  25.   ActionRecordArrayPtr = ^ActionRecordArray;
  26.  
  27. procedure TextProcess( data: Handle; action_count: UInt32; actions:
  28. ActionRecordArrayPtr );
  29.  
  30. Given an unlocked handle to a block of text, you need to apply a sequence of
  31. actions to the data.  You will be required to insert text, delete text, search
  32. for text, search for and replace text.  All required actions take place at the
  33. position of the current selection pointer, which starts at zero, before the
  34. first character in the data.  In response to each of the action commands you
  35. need to perform the following:
  36.  
  37. kActionMove - move the internal position forward by the amount field (which may
  38. be negative).  (Example, kActionMove with amount set to kMaxHandleSize will set
  39. the internal pointer to be GetHandleSize( data )).
  40.  
  41. kActionInsert - Insert the replace string at the current location.  (Example,
  42. if the internal pointer is GetHandleSize( data ), then the replace string will
  43. be appended to the handle.
  44.  
  45. kActionDelete - Delete the number of characters specified by the amount field. 
  46. The internal position pointer is never moved.  If the amount field is greater
  47. than the number of characters after the internal pointer, then fewer characters
  48. are deleted such that the handle is truncated at the current value of the
  49. selection pointer.
  50.  
  51. kActionSearch - search forward (backwards if the kFlagBackwardsBit is set in
  52. the flags field) for the search field (case sensitively if the
  53. kFlagCaseSenstitiveBit is set in the flags field).  The position pointer should
  54. be set to point before the first character of the string if it is found, or at
  55. GetHandleSize (zero if backwards) if not found.  If there is a match at the
  56. initial position, it is not counted (ie, kActionMove -kMaxHandleSize, kSearch
  57. 'hello', kSearch 'hello' finds the second occurance of 'hello').
  58.  
  59. kActionSearchAndReplace - search forward (or backward if the kFlagBackwardsBit
  60. is set in the flags field), but when a match is found, replace it with the
  61. replace string, leaving the position pointer unmoved.  If the kFlagGlobalBit is
  62. set in the flags field, continue searching as long matches continues to be
  63. found.  Repeated searches begin after the previous replace string (i.e., never
  64. replace any characters of the replace string).  For example, if you replace
  65. 'aaa' with 'ABA' (case insensitively) in 'aaaaaaaa' you will get 'ABAABAaa',
  66. not 'ABABABAa'.  Replace never moves the internal position pointer.
  67.  
  68. The internal selection pointer is constrained to be in the range of 0 and
  69. GetHandleSize(data), inclusive, at all times.  Should any action cause the
  70. selection pointer to become less than zero (or greater than GetHandleSize),
  71. then you must reset it to zero (or GetHandleSize, respectively).  The handle
  72. size will never exceed 1 Meg, and you will have plenty of memory to play with.  
  73.  
  74. Characters from chr(127)-chr(255) will never appear in the handle or any
  75. strings.  
  76. *)
  77.  
  78. unit Solution;
  79.  
  80. interface
  81.  
  82. // Do not modify the interface
  83.  
  84.     uses
  85.         Types, Files;
  86.         
  87.     const
  88.         kMaxHandleSize = 1000000;
  89.     const
  90.         kActionMove = 1;
  91.         kActionInsert = 2;
  92.         kActionDelete = 3;
  93.         kActionSearch = 4;
  94.         kActionSearchAndReplace = 5;
  95.     const
  96.         kFlagCaseSenstitiveBit = 0;
  97.         kFlagGlobalBit = 1;
  98.         kFlagBackwardsBit = 2;
  99.     type
  100.       ActionRecord = record
  101.         action: UInt32;
  102.         amount: SInt32;
  103.         flags: UInt32;
  104.         search: Str255;
  105.         replace: Str255;
  106.       end;
  107.       ActionRecordArray = array[0..0] of ActionRecord;
  108.       ActionRecordArrayPtr = ^ActionRecordArray;
  109.  
  110.     procedure TextProcess( data: Handle; action_count: UInt32; actions: ActionRecordArrayPtr );
  111.  
  112. implementation
  113.  
  114. // Fill in your solution and then submit this folder
  115.  
  116. // Team Name: FILL IN YOUR TEAM NAME!
  117.  
  118. end.
  119.