home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1999 March / PCWK3A99.iso / Linux / DDD331 / DDD-3_1_.000 / DDD-3_1_ / ddd-3.1.1 / ddd / huffdecode.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-25  |  2.3 KB  |  102 lines

  1. // $Id: huffdecode.C,v 1.10 1998/03/25 12:46:03 zeller Exp $ -*- C++ -*-
  2. // Huffman-decode a text encoded with `huffencode'
  3.  
  4. // Copyright (C) 1996 Technische Universitaet Braunschweig, Germany.
  5. // Written by Andreas Zeller <zeller@ips.cs.tu-bs.de>.
  6. // 
  7. // This file is part of DDD.
  8. // 
  9. // DDD is free software; you can redistribute it and/or
  10. // modify it under the terms of the GNU General Public
  11. // License as published by the Free Software Foundation; either
  12. // version 2 of the License, or (at your option) any later version.
  13. // 
  14. // DDD is distributed in the hope that it will be useful,
  15. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  17. // See the GNU General Public License for more details.
  18. // 
  19. // You should have received a copy of the GNU General Public
  20. // License along with DDD -- see the file COPYING.
  21. // If not, write to the Free Software Foundation, Inc.,
  22. // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. // 
  24. // DDD is the data display debugger.
  25. // For details, see the DDD World-Wide-Web page, 
  26. // `http://www.cs.tu-bs.de/softech/ddd/',
  27. // or send a mail to the DDD developers <ddd@ips.cs.tu-bs.de>.
  28.  
  29. static char huffdecode_rcsid[] = 
  30.     "$Id: huffdecode.C,v 1.10 1998/03/25 12:46:03 zeller Exp $";
  31.  
  32. #ifdef __GNUG__
  33. #pragma implementation
  34. #endif
  35.  
  36. #include "strclass.h"
  37.  
  38. struct HuffCode { 
  39.     const HuffCode *left;    // Left child  (0 if leaf)
  40.     const HuffCode *right;    // Right child (value if leaf)
  41. };
  42.  
  43. #ifdef HUFFTEXT
  44. #include HUFFTEXT
  45. #endif
  46.  
  47. static void huffdecode(ostream& os)
  48. {
  49.     (void) huffdecode_rcsid;      // Use it
  50.  
  51.     int p = 0;              // Run across hufftext
  52.     int i = 7;              // Run across bits
  53.     const HuffCode *h = huffcode; // Run around in the code
  54.  
  55.     int n = 0;
  56.  
  57.     while (n < hufflength)
  58.     {
  59.     if (h->left == 0)
  60.     {
  61.         // Arrived at character
  62.         os << (char)h->right;
  63.         n++;
  64.         h = huffcode;
  65.     }
  66.  
  67.     if (h->left != 0)
  68.     {
  69.         // Find next position
  70.         if (((unsigned char)(hufftext[p])) & (1 << i))
  71.         h = h->right;
  72.         else
  73.         h = h->left;
  74.     }
  75.  
  76.     if (--i < 0)
  77.     {
  78.         // Find next bit
  79.         p++;
  80.         i = 7;
  81.     }
  82.     }
  83. }
  84.  
  85. #ifdef MAIN
  86. #include <stdlib.h>
  87.  
  88. #ifndef EXIT_SUCCESS
  89. #define EXIT_SUCCESS 0
  90. #endif
  91.  
  92. #ifndef EXIT_FAILURE
  93. #define EXIT_FAILURE 1
  94. #endif
  95.  
  96. int main()
  97. {
  98.     huffdecode(cout);
  99.     return EXIT_SUCCESS;
  100. }
  101. #endif
  102.