Main Page | Class Hierarchy | Class List | File List | Class Members | File Members

tmerge.c

Go to the documentation of this file.
00001 /* 00002 * Copyright (c) 1985 - 1999, Microsoft Corporation 00003 */ 00004 00005 #include "precomp.h" 00006 #pragma hdrstop 00007 #pragma hdrstop 00008 00009 // run-length encoded data structure for attributes 00010 00011 typedef struct _ATTR_PAIR { 00012 WORD Length; // number of attribute appears 00013 WORD Attr; // attribute 00014 } ATTR_PAIR, *PATTR_PAIR; 00015 00016 00017 #define SOURCE1_LENGTH 4 00018 #define MERGE1_LENGTH 1 00019 #define TARGET1_LENGTH 4 00020 ATTR_PAIR Source1[SOURCE1_LENGTH] = { 2, 10, 00021 5, 20, 00022 3, 30, 00023 1, 40 00024 }; 00025 ATTR_PAIR Merge1[MERGE1_LENGTH] = { 2, 20 }; 00026 00027 ATTR_PAIR Target1[TARGET1_LENGTH] = { 2, 10, 00028 5, 20, 00029 3, 30, 00030 1, 40 00031 }; 00032 #define START_INDEX1 4 00033 #define END_INDEX1 5 00034 00035 //****************************************** 00036 00037 #define SOURCE2_LENGTH 4 00038 #define MERGE2_LENGTH 2 00039 #define TARGET2_LENGTH 6 00040 ATTR_PAIR Source2[SOURCE2_LENGTH] = { 2, 10, 00041 5, 20, 00042 3, 30, 00043 1, 40 00044 }; 00045 ATTR_PAIR Merge2[MERGE2_LENGTH] = { 1, 20, 00046 1, 30 00047 }; 00048 ATTR_PAIR Target2[TARGET2_LENGTH] = { 2, 10, 00049 3, 20, 00050 1, 30, 00051 1, 20, 00052 3, 30, 00053 1, 40 00054 }; 00055 #define START_INDEX2 4 00056 #define END_INDEX2 5 00057 00058 //****************************************** 00059 00060 #define SOURCE3_LENGTH 4 00061 #define MERGE3_LENGTH 2 00062 #define TARGET3_LENGTH 5 00063 ATTR_PAIR Source3[SOURCE3_LENGTH] = { 2, 10, 00064 5, 20, 00065 3, 30, 00066 1, 40 00067 }; 00068 ATTR_PAIR Merge3[MERGE3_LENGTH] = { 1, 20, 00069 1, 30 00070 }; 00071 ATTR_PAIR Target3[TARGET3_LENGTH] = { 2, 10, 00072 5, 20, 00073 2, 30, 00074 1, 20, 00075 1, 30 00076 }; 00077 #define START_INDEX3 9 00078 #define END_INDEX3 10 00079 00080 //****************************************** 00081 00082 #define SOURCE4_LENGTH 4 00083 #define MERGE4_LENGTH 4 00084 #define TARGET4_LENGTH 4 00085 ATTR_PAIR Source4[SOURCE4_LENGTH] = { 2, 10, 00086 5, 20, 00087 3, 30, 00088 1, 40 00089 }; 00090 ATTR_PAIR Merge4[MERGE4_LENGTH] = { 1, 20, 00091 1, 30, 00092 3, 20, 00093 6, 60 00094 }; 00095 ATTR_PAIR Target4[TARGET4_LENGTH] = { 1, 20, 00096 1, 30, 00097 3, 20, 00098 6, 60 00099 }; 00100 #define START_INDEX4 0 00101 #define END_INDEX4 10 00102 00103 //****************************************** 00104 00105 #define SOURCE5_LENGTH 4 00106 #define MERGE5_LENGTH 3 00107 #define TARGET5_LENGTH 6 00108 ATTR_PAIR Source5[SOURCE5_LENGTH] = { 2, 10, 00109 5, 20, 00110 3, 30, 00111 1, 40 00112 }; 00113 ATTR_PAIR Merge5[MERGE5_LENGTH] = { 1, 10, 00114 1, 70, 00115 1, 30 00116 }; 00117 ATTR_PAIR Target5[TARGET5_LENGTH] = { 3, 10, 00118 1, 70, 00119 1, 30, 00120 2, 20, 00121 3, 30, 00122 1, 40 00123 }; 00124 #define START_INDEX5 2 00125 #define END_INDEX5 4 00126 00127 //****************************************** 00128 00129 #define SOURCE6_LENGTH 4 00130 #define MERGE6_LENGTH 3 00131 #define TARGET6_LENGTH 6 00132 ATTR_PAIR Source6[SOURCE6_LENGTH] = { 2, 10, 00133 5, 20, 00134 3, 30, 00135 1, 40 00136 }; 00137 ATTR_PAIR Merge6[MERGE6_LENGTH] = { 1, 10, 00138 1, 70, 00139 1, 30 00140 }; 00141 ATTR_PAIR Target6[TARGET6_LENGTH] = { 2, 10, 00142 2, 20, 00143 1, 10, 00144 1, 70, 00145 4, 30, 00146 1, 40 00147 }; 00148 #define START_INDEX6 4 00149 #define END_INDEX6 6 00150 00151 //****************************************** 00152 00153 #define SOURCE7_LENGTH 1 00154 #define MERGE7_LENGTH 2 00155 #define TARGET7_LENGTH 2 00156 ATTR_PAIR Source7[SOURCE7_LENGTH] = { 3, 10 00157 }; 00158 ATTR_PAIR Merge7[MERGE7_LENGTH] = { 1, 20, 00159 2, 70 00160 }; 00161 ATTR_PAIR Target7[TARGET7_LENGTH] = { 1, 20, 00162 2, 70 00163 }; 00164 #define START_INDEX7 0 00165 #define END_INDEX7 2 00166 00167 //****************************************** 00168 00169 #define SOURCE8_LENGTH 1 00170 #define MERGE8_LENGTH 2 00171 #define TARGET8_LENGTH 3 00172 ATTR_PAIR Source8[SOURCE8_LENGTH] = { 3, 10 00173 }; 00174 ATTR_PAIR Merge8[MERGE8_LENGTH] = { 1, 20, 00175 1, 80 00176 }; 00177 ATTR_PAIR Target8[TARGET8_LENGTH] = { 1, 20, 00178 1, 80, 00179 1, 10 00180 }; 00181 #define START_INDEX8 0 00182 #define END_INDEX8 1 00183 00184 //****************************************** 00185 00186 #define SOURCE9_LENGTH 1 00187 #define MERGE9_LENGTH 1 00188 #define TARGET9_LENGTH 2 00189 ATTR_PAIR Source9[SOURCE9_LENGTH] = { 3, 10 00190 }; 00191 ATTR_PAIR Merge9[MERGE9_LENGTH] = { 1, 20 00192 }; 00193 ATTR_PAIR Target9[TARGET9_LENGTH] = { 1, 20, 00194 2, 10 00195 }; 00196 #define START_INDEX9 0 00197 #define END_INDEX9 0 00198 00199 00200 //****************************************** 00201 00202 #define SOURCE10_LENGTH 1 00203 #define MERGE10_LENGTH 1 00204 #define TARGET10_LENGTH 3 00205 ATTR_PAIR Source10[SOURCE10_LENGTH] = { 3, 10 00206 }; 00207 ATTR_PAIR Merge10[MERGE10_LENGTH] = { 1, 20 00208 }; 00209 ATTR_PAIR Target10[TARGET10_LENGTH] = { 1, 10, 00210 1, 20, 00211 1, 10 00212 }; 00213 #define START_INDEX10 1 00214 #define END_INDEX10 1 00215 00216 00217 MergeAttrStrings( 00218 PATTR_PAIR Source, 00219 WORD SourceLength, 00220 PATTR_PAIR Merge, 00221 WORD MergeLength, 00222 OUT PATTR_PAIR *Target, 00223 OUT LPWORD TargetLength, 00224 IN WORD StartIndex, 00225 IN WORD EndIndex 00226 ) 00227 00228 /*++ 00229 00230 --*/ 00231 { 00232 PATTR_PAIR SrcAttr,TargetAttr,SrcEnd; 00233 PATTR_PAIR NewString; 00234 WORD NewStringLength=0; 00235 WORD i; 00236 00237 NewString = (PATTR_PAIR) LocalAlloc(LMEM_FIXED,(SourceLength+MergeLength+1)*sizeof(ATTR_PAIR)); 00238 00239 // 00240 // copy the source string, up to the start index. 00241 // 00242 00243 SrcAttr = Source; 00244 SrcEnd = Source + SourceLength; 00245 TargetAttr = NewString; 00246 if (StartIndex != 0) { 00247 for (i=0;i<StartIndex;) { 00248 i += SrcAttr->Length; 00249 *TargetAttr++ = *SrcAttr++; 00250 } 00251 00252 // 00253 // back up to the last pair copied, in case the attribute in the first 00254 // pair in the merge string matches. also, adjust TargetAttr->Length 00255 // based on i, the attribute 00256 // counter, back to the StartIndex. i will be larger than the 00257 // StartIndex in the case where the last attribute pair copied had 00258 // a length greater than the number needed to reach StartIndex. 00259 // 00260 00261 TargetAttr--; 00262 if (i>StartIndex) { 00263 TargetAttr->Length -= i-StartIndex; 00264 } 00265 if (Merge->Attr == TargetAttr->Attr) { 00266 TargetAttr->Length += Merge->Length; 00267 MergeLength-=1; 00268 Merge++; 00269 } 00270 TargetAttr++; 00271 } else { 00272 i=0; 00273 } 00274 00275 // 00276 // copy the merge string. 00277 // 00278 00279 RtlCopyMemory(TargetAttr,Merge,MergeLength*sizeof(ATTR_PAIR)); 00280 TargetAttr += MergeLength; 00281 00282 // 00283 // figure out where to resume copying the source string. 00284 // 00285 00286 while (i<=EndIndex) { 00287 i += SrcAttr->Length; 00288 SrcAttr++; 00289 } 00290 00291 // 00292 // if not done, copy the rest of the source 00293 // 00294 00295 if (SrcAttr != SrcEnd || i!=(EndIndex+1)) { 00296 00297 // 00298 // see if we've gone past the right attribute. if so, back up and 00299 // copy the attribute and the correct length. 00300 // 00301 00302 TargetAttr--; 00303 if (i>(SHORT)(EndIndex+1)) { 00304 SrcAttr--; 00305 if (TargetAttr->Attr == SrcAttr->Attr) { 00306 TargetAttr->Length += i-(EndIndex+1); 00307 } else { 00308 TargetAttr++; 00309 TargetAttr->Attr = SrcAttr->Attr; 00310 TargetAttr->Length = i-(EndIndex+1); 00311 } 00312 SrcAttr++; 00313 } 00314 00315 // 00316 // see if we can merge the source and target. 00317 // 00318 00319 else if (TargetAttr->Attr == SrcAttr->Attr) { 00320 TargetAttr->Length += SrcAttr->Length; 00321 i += SrcAttr->Length; 00322 SrcAttr++; 00323 } 00324 TargetAttr++; 00325 00326 // 00327 // copy the rest of the source 00328 // 00329 00330 RtlCopyMemory(TargetAttr,SrcAttr,(SourceLength*sizeof(ATTR_PAIR)) - ((ULONG)SrcAttr - (ULONG)Source)); 00331 } 00332 00333 TargetAttr = (PATTR_PAIR)((ULONG)TargetAttr + (SourceLength*sizeof(ATTR_PAIR)) - ((ULONG)SrcAttr - (ULONG)Source)); 00334 *TargetLength = (WORD)(TargetAttr - NewString); 00335 *Target = NewString; 00336 return; 00337 } 00338 00339 DWORD 00340 main( 00341 int argc, 00342 char *argv[] 00343 ) 00344 { 00345 PATTR_PAIR Target; 00346 WORD TargetLength; 00347 WORD i; 00348 00349 MergeAttrStrings(Source1, 00350 SOURCE1_LENGTH, 00351 Merge1, 00352 MERGE1_LENGTH, 00353 &Target, 00354 &TargetLength, 00355 START_INDEX1, 00356 END_INDEX1 00357 ); 00358 if (TargetLength != TARGET1_LENGTH) { 00359 printf("merge 1 failed\n"); 00360 printf("TargetLength is %d. expected %d\n",TargetLength,TARGET1_LENGTH); 00361 } 00362 if (memcmp(Target,Target1,TargetLength)) { 00363 printf("Target didn't match\n"); 00364 for (i=0;i<TargetLength;i++) { 00365 printf("returned attr is %d\n",Target[i].Attr); 00366 printf("returned length is %d\n",Target[i].Length); 00367 printf("expected attr is %d\n",Target1[i].Attr); 00368 printf("expected length is %d\n",Target1[i].Length); 00369 } 00370 } 00371 MergeAttrStrings(Source2, 00372 SOURCE2_LENGTH, 00373 Merge2, 00374 MERGE2_LENGTH, 00375 &Target, 00376 &TargetLength, 00377 START_INDEX2, 00378 END_INDEX2 00379 ); 00380 if (TargetLength != TARGET2_LENGTH) { 00381 printf("merge 2 failed\n"); 00382 printf("TargetLength is %d. expected %d\n",TargetLength,TARGET2_LENGTH); 00383 } 00384 if (memcmp(Target,Target2,TargetLength)) { 00385 printf("Target didn't match\n"); 00386 for (i=0;i<TargetLength;i++) { 00387 printf("returned attr is %d\n",Target[i].Attr); 00388 printf("returned length is %d\n",Target[i].Length); 00389 printf("expected attr is %d\n",Target2[i].Attr); 00390 printf("expected length is %d\n",Target2[i].Length); 00391 } 00392 } 00393 MergeAttrStrings(Source3, 00394 SOURCE3_LENGTH, 00395 Merge3, 00396 MERGE3_LENGTH, 00397 &Target, 00398 &TargetLength, 00399 START_INDEX3, 00400 END_INDEX3 00401 ); 00402 if (TargetLength != TARGET3_LENGTH) { 00403 printf("merge 3 failed\n"); 00404 printf("TargetLength is %d. expected %d\n",TargetLength,TARGET3_LENGTH); 00405 } 00406 if (memcmp(Target,Target3,TargetLength)) { 00407 printf("Target didn't match\n"); 00408 for (i=0;i<TargetLength;i++) { 00409 printf("returned attr is %d\n",Target[i].Attr); 00410 printf("returned length is %d\n",Target[i].Length); 00411 printf("expected attr is %d\n",Target3[i].Attr); 00412 printf("expected length is %d\n",Target3[i].Length); 00413 } 00414 } 00415 MergeAttrStrings(Source4, 00416 SOURCE4_LENGTH, 00417 Merge4, 00418 MERGE4_LENGTH, 00419 &Target, 00420 &TargetLength, 00421 START_INDEX4, 00422 END_INDEX4 00423 ); 00424 if (TargetLength != TARGET4_LENGTH) { 00425 printf("merge 4 failed\n"); 00426 printf("TargetLength is %d. expected %d\n",TargetLength,TARGET4_LENGTH); 00427 } 00428 if (memcmp(Target,Target4,TargetLength)) { 00429 printf("Target didn't match\n"); 00430 for (i=0;i<TargetLength;i++) { 00431 printf("returned attr is %d\n",Target[i].Attr); 00432 printf("returned length is %d\n",Target[i].Length); 00433 printf("expected attr is %d\n",Target4[i].Attr); 00434 printf("expected length is %d\n",Target4[i].Length); 00435 } 00436 } 00437 MergeAttrStrings(Source5, 00438 SOURCE5_LENGTH, 00439 Merge5, 00440 MERGE5_LENGTH, 00441 &Target, 00442 &TargetLength, 00443 START_INDEX5, 00444 END_INDEX5 00445 ); 00446 if (TargetLength != TARGET5_LENGTH) { 00447 printf("merge 5 failed\n"); 00448 printf("TargetLength is %d. expected %d\n",TargetLength,TARGET5_LENGTH); 00449 } 00450 if (memcmp(Target,Target5,TargetLength)) { 00451 printf("Target didn't match\n"); 00452 for (i=0;i<TargetLength;i++) { 00453 printf("returned attr is %d\n",Target[i].Attr); 00454 printf("returned length is %d\n",Target[i].Length); 00455 printf("expected attr is %d\n",Target5[i].Attr); 00456 printf("expected length is %d\n",Target5[i].Length); 00457 } 00458 } 00459 MergeAttrStrings(Source6, 00460 SOURCE6_LENGTH, 00461 Merge6, 00462 MERGE6_LENGTH, 00463 &Target, 00464 &TargetLength, 00465 START_INDEX6, 00466 END_INDEX6 00467 ); 00468 if (TargetLength != TARGET6_LENGTH) { 00469 printf("merge 6 failed\n"); 00470 printf("TargetLength is %d. expected %d\n",TargetLength,TARGET6_LENGTH); 00471 } 00472 if (memcmp(Target,Target6,TargetLength)) { 00473 printf("Target didn't match\n"); 00474 for (i=0;i<TargetLength;i++) { 00475 printf("returned attr is %d\n",Target[i].Attr); 00476 printf("returned length is %d\n",Target[i].Length); 00477 printf("expected attr is %d\n",Target6[i].Attr); 00478 printf("expected length is %d\n",Target6[i].Length); 00479 } 00480 } 00481 MergeAttrStrings(Source7, 00482 SOURCE7_LENGTH, 00483 Merge7, 00484 MERGE7_LENGTH, 00485 &Target, 00486 &TargetLength, 00487 START_INDEX7, 00488 END_INDEX7 00489 ); 00490 if (TargetLength != TARGET7_LENGTH) { 00491 printf("merge 7 failed\n"); 00492 printf("TargetLength is %d. expected %d\n",TargetLength,TARGET7_LENGTH); 00493 } 00494 if (memcmp(Target,Target7,TargetLength)) { 00495 printf("Target didn't match\n"); 00496 for (i=0;i<TargetLength;i++) { 00497 printf("returned attr is %d\n",Target[i].Attr); 00498 printf("returned length is %d\n",Target[i].Length); 00499 printf("expected attr is %d\n",Target7[i].Attr); 00500 printf("expected length is %d\n",Target7[i].Length); 00501 } 00502 } 00503 MergeAttrStrings(Source8, 00504 SOURCE8_LENGTH, 00505 Merge8, 00506 MERGE8_LENGTH, 00507 &Target, 00508 &TargetLength, 00509 START_INDEX8, 00510 END_INDEX8 00511 ); 00512 if (TargetLength != TARGET8_LENGTH) { 00513 printf("merge 8 failed\n"); 00514 printf("TargetLength is %d. expected %d\n",TargetLength,TARGET8_LENGTH); 00515 } 00516 if (memcmp(Target,Target8,TargetLength)) { 00517 printf("Target didn't match\n"); 00518 for (i=0;i<TargetLength;i++) { 00519 printf("returned attr is %d\n",Target[i].Attr); 00520 printf("returned length is %d\n",Target[i].Length); 00521 printf("expected attr is %d\n",Target8[i].Attr); 00522 printf("expected length is %d\n",Target8[i].Length); 00523 } 00524 } 00525 MergeAttrStrings(Source9, 00526 SOURCE9_LENGTH, 00527 Merge9, 00528 MERGE9_LENGTH, 00529 &Target, 00530 &TargetLength, 00531 START_INDEX9, 00532 END_INDEX9 00533 ); 00534 if (TargetLength != TARGET9_LENGTH) { 00535 printf("merge 9 failed\n"); 00536 printf("TargetLength is %d. expected %d\n",TargetLength,TARGET9_LENGTH); 00537 } 00538 if (memcmp(Target,Target9,TargetLength)) { 00539 printf("Target didn't match\n"); 00540 for (i=0;i<TargetLength;i++) { 00541 printf("returned attr is %d\n",Target[i].Attr); 00542 printf("returned length is %d\n",Target[i].Length); 00543 printf("expected attr is %d\n",Target9[i].Attr); 00544 printf("expected length is %d\n",Target9[i].Length); 00545 } 00546 } 00547 MergeAttrStrings(Source10, 00548 SOURCE10_LENGTH, 00549 Merge10, 00550 MERGE10_LENGTH, 00551 &Target, 00552 &TargetLength, 00553 START_INDEX10, 00554 END_INDEX10 00555 ); 00556 if (TargetLength != TARGET10_LENGTH) { 00557 printf("merge 10 failed\n"); 00558 printf("TargetLength is %d. expected %d\n",TargetLength,TARGET10_LENGTH); 00559 } 00560 if (memcmp(Target,Target10,TargetLength)) { 00561 printf("Target didn't match\n"); 00562 for (i=0;i<TargetLength;i++) { 00563 printf("returned attr is %d\n",Target[i].Attr); 00564 printf("returned length is %d\n",Target[i].Length); 00565 printf("expected attr is %d\n",Target10[i].Attr); 00566 printf("expected length is %d\n",Target10[i].Length); 00567 } 00568 } 00569 }

Generated on Sat May 15 19:42:00 2004 for test by doxygen 1.3.7