File: cl_dump_data.c

package info (click to toggle)
cldump 0.11~dfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 216 kB
  • sloc: ansic: 2,267; makefile: 19
file content (142 lines) | stat: -rw-r--r-- 3,791 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * cldump - Dumps Clarion databases to text, SQL and CSV formats
 *
 * Copyright (C) 2004-2006 Julien BLACHE <jb@jblache.org>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; version 2 of the License.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * $Id: cl_dump_data.c 65 2010-11-27 10:20:08Z julien $
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <endian.h>
#include <byteswap.h>

#include "cldump.h"

void
clarion_dump_data (ClarionHandle *cl)
{
  int i;
  int nrecs, nflds;
  FILE *fp = cl->data;
  ClarionHeader *clh = cl->clm.clh;
  ClarionFieldDesc *clfd = cl->clm.clfd;
  ClarionRecordHeader clrh;
  uint8_t *buf;
  char *rhd[8] = {
    "NEW RECORD",
    "OLD RECORD",
    "REVISED RECORD",
    "*** UNDEFINED (3) ***",
    "DELETED RECORD",
    "*** UNDEFINED (5) ***",
    "RECORD HELD",
    "*** UNDEFINED (7) ***"
  };

  buf = (uint8_t *) malloc(clh->reclen * sizeof(uint8_t));

  for (nrecs = 0; nrecs < clh->numrecs; nrecs++)
    {      
      fread(&clrh.rhd, 1, 1, fp);
      fread(&clrh.rptr, 4, 1, fp);

      if ((clrh.rhd & CL_RECORD_DELETED) && (cl->opts & CL_OPT_DUMP_ACTIVE))
	{
	  fseek(cl->data, (clh->reclen - 5), SEEK_CUR);
	  continue;
	}

      fprintf(stderr, "=== RECORD %d:\n", (nrecs + 1));
      fprintf(stderr, "rhd  : 0x%02x\n", clrh.rhd);
      fprintf(stderr, "\tAttributes set:");
      if (clrh.rhd)
	{
	  for (i = 0; i < 8; i++)
	    {
	      if ((clrh.rhd >> i) & 0x01)
		fprintf(stderr, " [%s]", rhd[i]);
	    }
	}
      else
	{
	  fprintf(stderr, " NONE");
	}
      fprintf(stderr, "\n");
      fprintf(stderr, "rptr : 0x%08x\n", clrh.rptr);
      fprintf(stderr, "\n");

      fflush(stderr);

      fprintf(stdout, "=== RECORD %d:\n", (nrecs + 1));

      for (nflds = 0; nflds < clh->numflds; nflds++)
	{
	  /*
	   * A field with type CL_FIELD_GROUP is a pseudo-field
	   * used to indicate that the next clfd[i].length fields
	   * are grouped together.
	   */
	  if (clfd[nflds].fldtype == CL_FIELD_GROUP)
	    {
	      continue;
	    }

	  fprintf(stdout, "%8s : ", (clfd[nflds].fldname + 4));
	  switch (clfd[nflds].fldtype)
	    {
	      case CL_FIELD_LONG:
		clarion_dump_field_long(buf, &clfd[nflds], cl->data, NULL);
		break;
	      case CL_FIELD_REAL:
		clarion_dump_field_real(buf, &clfd[nflds], cl->data, NULL);
		break;
	      case CL_FIELD_STRING:
	      case CL_FIELD_STRING_PIC_TOK:
		clarion_dump_field_string(buf, &clfd[nflds], cl->data, NULL, cl->charset);
		break;
	      case CL_FIELD_BYTE:
		clarion_dump_field_byte(buf, &clfd[nflds], cl->data, NULL);
		break;
	      case CL_FIELD_SHORT:
		clarion_dump_field_short(buf, &clfd[nflds], cl->data, NULL);
		break;
	      case CL_FIELD_DECIMAL:
		clarion_dump_field_decimal(buf, &clfd[nflds], cl->data, NULL);
		break;
	      default:
	      fprintf(stderr, "Unknown field type %d\n", clfd[nflds].fldtype);
	      break;
	    }
	  fprintf(stdout, "\n");
	}

      if ((clh->sfatr & CL_MEMO_FILE_EXISTS) && (!(cl->opts & CL_OPT_NO_MEMO)))
	{
	  fprintf(stdout, "MEMO ENTRY   : ");
	  clarion_dump_memo_entry(&clrh, cl->memo, NULL, cl->charset);
	  fprintf(stdout, "\n");
	}

      fprintf(stdout, "\n");

      fflush(stdout);
    }

  free(buf);
}