home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / c / ctw11.zip / CTW-1.1 / rotate.c < prev    next >
Text File  |  1992-02-24  |  2KB  |  90 lines

  1. /**********************************************************************/
  2. /*   Function  to  rotate a block of memory. Can be used for example  */
  3. /*   with an array of pointers.                          */
  4. /**********************************************************************/
  5. # include    <malloc.h>
  6. # include    <memory.h>
  7.  
  8. # if defined(MSDOS) || defined(__MSDOS__)
  9. #     define    TMP_SIZE    1024
  10. # else
  11. #     define    TMP_SIZE    8192
  12. # endif
  13.  
  14. static void    my_memcpy();
  15.  
  16. /**********************************************************************/
  17. /*   If amount > 0 then copy from start=>start+amount.              */
  18. /*   If amount < 0 then copy from start+amount=>start.              */
  19. /**********************************************************************/
  20. void
  21. rotate_mem(ptr, ptr_end, amount)
  22. char    *ptr;
  23. char    *ptr_end;
  24. int    amount;
  25. {    char    tmpbuf[TMP_SIZE];
  26.     char    *tmp;
  27.     int    abs_amount = amount < 0 ? -amount : amount;
  28.     int    size = ptr_end - ptr;
  29.  
  30.     if (amount == 0 || size == 0)
  31.         return;
  32.  
  33.     if (abs_amount <= TMP_SIZE)
  34.         tmp = tmpbuf;
  35.     else
  36.         tmp = malloc(abs_amount);
  37.     if (amount < 0) {
  38.         memcpy(tmp, ptr, abs_amount);
  39.         memcpy(ptr, ptr + abs_amount, size - abs_amount);
  40.         memcpy(ptr + size - abs_amount, tmp, abs_amount);
  41.         }
  42.     else {
  43.         memcpy(tmp, ptr + size - abs_amount, abs_amount);
  44.         my_memcpy(ptr + abs_amount, ptr, size - abs_amount);
  45.         memcpy(ptr, tmp, abs_amount);
  46.         }
  47.     if (tmp != tmpbuf)
  48.         free(tmp);
  49. }
  50. static void
  51. my_memcpy(p1, p2, len)
  52. char    *p1;
  53. char    *p2;
  54. int    len;
  55. {
  56.     p1 += len;
  57.     p2 += len;
  58.     if (((int) p1 & 3) == 0 && ((int) p2 & 3) == 0 && (len & 3) == 0
  59.        && sizeof(long) == 4) {
  60.         while (len >= sizeof(long)) {
  61.             p1 -= sizeof(long);
  62.             p2 -= sizeof(long);
  63.             *(long *) p1 = *(long *) p2;
  64.             len -= sizeof(long);
  65.             }
  66.         }
  67.     while (len-- > 0)
  68.         *--p1 = *--p2;
  69. }
  70. # if TEST
  71. char    *test1 = "abcdefghijklmnopq";
  72. int    test2[] = {
  73.     1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0
  74.     };
  75. main(argc, argv)
  76. int    argc;
  77. char    **argv;
  78. {    int    n = atoi(argv[1]);
  79.     int    i;
  80.     rotate_mem(test1, test1 + strlen(test1), n);
  81.     printf("Test1: %s\n", test1);
  82.     rotate_mem(test2, &test2[10], n * sizeof(int));
  83.     printf("Test2: ");
  84.     for (i = 0; i < 11; i++)
  85.         printf(" %d ", test2[i]);
  86.     printf("\n");
  87. }
  88. # endif
  89.  
  90.