Example C Application: Code Comparison

his is part of a single file that was translated from C to Java with Jazillian. The original C code is on the left, and the Java code produced by Jazillian is on the right.

See also the complete Example Application.

C CodeJava Code
  1. #ifndef lint
  2. static char sccsid[] = "@(#)cards.c 5.4 (Berkeley) 6/1/90";
  3. #endif /* not lint */
  4. # include "monop.ext"
  5. # include "pathnames.h"
  6. /*
  7. * These routine deal with the card decks
  8. */
  9. # define GOJF 'F' /* char for get-out-of-jail-free cards */
  10. # ifndef DEV
  11. static char *cardfile = _PATH_CARDS;
  12. # else
  13. static char *cardfile = "cards.pck";
  14. # endif
  15. static FILE *deckf;
  16. /*
  17. * This routine initializes the decks from the data file,
  18. * which it opens.
  19. */
  20. void init_decks(void) {
  21. if ((deckf=fopen(cardfile, "r")) == NULL) {
  22. file_err:
  23. perror(cardfile);
  24. exit(1);
  25. }
  26. if (fread(deck, sizeof (DECK), 2, deckf) != 2) {
  27. // goto file_err;
  28. perror(cardfile);
  29. exit(1);
  30. }
  31. set_up(&CC_D);
  32. set_up(&CH_D);
  33. }
  34. /*
  35. * This routine sets up the offset pointers for the given deck.
  36. */
  37. void set_up(DECK *dp)
  38. {
  39. reg int r1, r2;
  40. int i;
  41. dp->offsets = (long *) calloc(sizeof (long), dp->num_cards);
  42. if (fread(dp->offsets, sizeof(long), dp->num_cards, deckf) != dp->num_cards) {
  43. perror(cardfile);
  44. exit(1);
  45. }
  46. dp->last_card = 0;
  47. dp->gojf_used = FALSE;
  48. for (i = 0; i < dp->num_cards; i++) {
  49. reg long temp;
  50. r1 = roll(1, dp->num_cards) - 1;
  51. r2 = roll(1, dp->num_cards) - 1;
  52. temp = dp->offsets[r2];
  53. dp->offsets[r2] = dp->offsets[r1];
  54. dp->offsets[r1] = temp;
  55. }
  56. }
  1. import java.io.*;
  2. public class CardReader {
  3. public final static char GOJF = 'F';
  4. static String cardfile = Pathnames._PATH_CARDS;
  5. static DataObjectInputStream deckf;
  6. static void init_decks() {
  7. try {
  8. try {
  9. deckf = new DataObjectInputStream(new FileInputStream(cardfile));
  10. }
  11. catch (FileNotFoundException e) {
  12. System.err.println("FileNotFoundException:" + e);
  13. }
  14. }
  15. catch (Exception e) {
  16. System.err.println(cardfile);
  17. System.exit(1);
  18. }
  19. try {
  20. InitDeck.deck = (Dk_st[]) deckf.readArray(InitDeck.deck, 2);
  21. System.err.println(cardfile);
  22. System.exit(1);
  23. }
  24. catch (Exception e) {
  25. }
  26. set_up(InitDeck.deck[0]);
  27. set_up(InitDeck.deck[1]);
  28. }
  29. static void set_up(Dk_st dp) {
  30. int r1 = 0;
  31. int r2 = 0;
  32. int i = 0;
  33. dp.offsets = new long[dp.num_cards];
  34. try {
  35. dp.offsets = (long[]) deckf.readArray(dp.offsets, dp.num_cards);
  36. System.err.println(cardfile);
  37. System.exit(1);
  38. }
  39. catch (Exception e) {
  40. }
  41. dp.last_card = 0;
  42. dp.gojf_used = false;
  43. for (i = 0;i < dp.num_cards;i++) {
  44. long temp = 0L;
  45. r1 = Roll.roll(1, dp.num_cards) - 1;
  46. r2 = Roll.roll(1, dp.num_cards) - 1;
  47. temp = dp.offsets[r2];
  48. dp.offsets[r2] = dp.offsets[r1];
  49. dp.offsets[r1] = temp;
  50. }
  51. }
  52. }

Explanation of Changes: