dbexp.c
来自「db.* (pronounced dee-be star) is an adva」· C语言 代码 · 共 945 行 · 第 1/2 页
C
945 行
/*************************************************************************** * * * db.* * * open source database kernel * * * * Copyright (c) 2000 Centura Software Corporation. All rights reserved. * * * * Use of this software, whether in source code format, or in executable, * * binary object code form, is governed by the CENTURA OPEN SOURCE LICENSE * * which is fully described in the LICENSE.TXT file, included within this * * distribution of source code files. * * * * Except as provided herein, the contents of this file are subject to the * * Centura Open Source Public License Version 1.0 (the "License"); you may * * not use this file except in compliance with the License. A copy of the * * License will be provided to you by Club ITTIA. * * * * Software distributed under the License is distributed on an "AS IS" * * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the * * License for the specific language governing rights and limitations * * under the License. * * * * The Original Code is db.linux version 1.0, released February 29, 2000. * * * * The Initial Developer of the Original Code is Centura Software * * Corporation. Portions created by Centura Software Corporation are * * Copyright (C) 1984-2000 Centura Software Corporation. All Rights * * Reserved. * * * * This file contains modifications to the Original Code made by ITTIA. * * This file may only be used in accordance with the ITTIA DB.* V.2 * * License Agreement which is available at WWW.ITTIA.COM. * * * **************************************************************************//*----------------------------------------------------------------------- dbexp - Database export tool for db.* To execute: dbexp [-r] [-m] [-n] [-d] [-e esc_char] [-s separator] database [rectypes...] Dbexp converts the data within a db.* database into an ASCII format that can be moved to another computer, or used as input to any other tool which reads ASCII data. Data from each record type is written to a file that is named after the record type. The file name suffix is ".txt", which would mean that the "employee" data from a database would be written to the file named "employee.txt". The default usage of dbexp writes all records of all types to files. Every field is printed. Numeric fields are printed as 'printf' prints them, and textual fields are printed with quotes (") around them. All fields are separated by commas. Arrays and structured fields are printed sequencially as normal fields. The default usage of dbexp names the database only: dbexp corp If not all records are to be exported, the ones that are to be exported may be named following the database name: dbexp corp employee department The "-r" and "-m" options request database addresses to be printed as data fields. The "-r" option causes the database address of the record to be printed as the first field. The "-m" option causes the database addresses of the owners of all sets of which the record is a member to be printed. These are printed following the record's database address (if requested), and before any of the data fields. This is the same order in which they occur in the db.* record. The database address fields are normally of no use, but may be used to re-create or re-structure the database with the 'dbimp' database import tool. The "-d" option will cause database addresses to be printed in decimal format, rather than database address format (file:slot).-----------------------------------------------------------------------*/#define MOD dbexp#include "db.star.h"#include "dbexp.h"#include "xml.h"#include "version.h"/* Maximum number of export (output) files open at once */#define MAX_OPEN 5/* ********************** LOCAL FUNCTION DECLARATIONS **************** */static void usage(void);static int get_xopts(int, DB_TCHAR **, EXPOPTS *);static void free_xopts(EXPOPTS *);static void check_xnames(EXPOPTS *, DB_TASK *);static void init_xfiles(EXPFILE *, DB_TASK *);static int export_data_file(FILE_NO, EXPOPTS *, EXPFILE *, DB_TASK *);static void nospace(void);static int exp_xml_dtd(DB_TASK *, EXPOPTS *);static int exp_xml(DB_TASK *, EXPOPTS *);static int exp_init_file(FILE **fp, char *file_name, DB_BOOLEAN unicode);static int exp_term_file(FILE *fp);extern int xml_print_dtd(FILE *df, DB_TASK *task);int MAIN(int argc, DB_TCHAR *argv[]){ int i; int status; FILE_NO fno; EXPOPTS xopts; EXPFILE *xfiles; DB_TASK *task = NULL; vtprintf(DBSTAR_UTIL_DESC(DB_TEXT("Database Export"))); /* set up the options */ memset(&xopts, 0, sizeof(EXPOPTS)); xopts.sep_char = DB_TEXT(','); xopts.esc_char = DB_TEXT('\\'); psp_init(); if (get_xopts(argc, argv, &xopts) != 0) { psp_term(); return 1; } if ((status = d_opentask(&task)) != S_OKAY) { vftprintf(stderr, DB_TEXT("dbexp: Failed to open task\n")); goto exit; } if ((status = d_set_dberr(dbexp_dberr, task)) != S_OKAY) { vftprintf(stderr, DB_TEXT("dbexp: failed to setup error handling\n")); goto exit; } if ((status = d_on_opt(READNAMES, task)) != S_OKAY) { vftprintf(stderr, DB_TEXT("dbexp: failed to setup options\n")); goto exit; } status = d_open_sg(xopts.dbname, DB_TEXT("o"), xopts.sg, task); if (status != S_OKAY) { vftprintf(stderr, DB_TEXT("dbexp: failed to open database\n")); goto exit; } /* make sure that specified record types exist */ check_xnames(&xopts, task); /* generates dtd, if applicable */ if (DTD == xopts.validation) { status = exp_xml_dtd(task, &xopts); if (status != S_OKAY) { vftprintf(stderr, DB_TEXT("dbexp: failed to write DTD (error %d)\n"), status); d_close(task); goto exit; } } if (xopts.xml) { status = exp_xml(task, &xopts); if (status != S_OKAY) { vftprintf(stderr, DB_TEXT("dbexp: failed to write XML file (error %d)\n"), status); d_close(task); goto exit; } } else { /* allocate a file pointer for each record type */ xfiles = (EXPFILE *) psp_cGetMemory(task->size_rt * sizeof(EXPFILE), 0); if (!xfiles) { vftprintf(stderr, DB_TEXT("Unable to allocate memory for output file table\n")); d_close(task); goto exit; } init_xfiles(xfiles, task); /* for each data file in the database */ for (fno = 0; fno < task->size_ft; fno++) { /* make sure it is a data file */ if (task->file_table[fno].ft_type != DATA) continue; /* make sure it contains records to be exported */ if (xopts.n_recs) { for (i = 0; i < xopts.n_recs; i++) { if (xopts.rt_index[i] == -1) continue; if (fno == task->record_table[xopts.rt_index[i]].rt_file) break; } if (i == xopts.n_recs) { if (!xopts.silent) vtprintf(DB_TEXT("Skipping data file '%s'\n"), task->file_table[fno].ft_name); continue; } } /* report progress to user */ if (! xopts.silent) { vtprintf(DB_TEXT("Exporting contents of data file '%s'\n"), task->file_table[fno].ft_name); } /* export all the records in this file */ if ((status = export_data_file(fno, &xopts, xfiles, task)) != S_OKAY) { if (status == S_NOSPACE) nospace(); break; } } /* termination message */ if (!xopts.silent) vtprintf(DB_TEXT("dbexp completed\n")); psp_freeMemory(xfiles, 0); } d_close(task);exit: if (task) d_closetask(task); free_xopts(&xopts); psp_term(); return 0;}/**************************************************************************/static int get_xopts(int argc, DB_TCHAR *argv[], EXPOPTS *xo){ int i; int j;#if defined(SAFEGARDE) DB_TCHAR *cp; DB_TCHAR *password; int mode;#endif /* for each command line argument */ for (i = 1; i < argc; i++) { /* check for '-' */ if (argv[i][0] != DB_TEXT('-')) break; vtstrlwr(argv[i]); if (4 == vtstrlen(argv[i])) { if (0 == vtstrcmp(argv[i], "-xml")) { xo->xml = TRUE; continue; } else { if (0 == vtstrcmp(argv[i], "-dtd")) { xo->validation=DTD; continue; } } } switch (argv[i][1]) { case DB_TEXT('h'): case DB_TEXT('?'): usage(); goto error; case DB_TEXT('r'): xo->rec_addr = TRUE; break; case DB_TEXT('m'): xo->mem_addr = TRUE; break; case DB_TEXT('n'): xo->silent = TRUE; break; case DB_TEXT('d'): xo->decimal = TRUE; break; case DB_TEXT('x'): xo->extended = TRUE; break;#if defined(UNICODE) case DB_TEXT('u'): xo->unicode = TRUE; break;#endif case DB_TEXT('e'): if (argv[i][2]) xo->esc_char = argv[i][2]; else { if (++i < argc) xo->esc_char = *argv[i]; else { vftprintf(stderr, DB_TEXT("dbexp: character must follow -e option\n")); goto error; } } break; case DB_TEXT('s'): if (argv[i][2]) { if (vtotlower(argv[i][2]) == DB_TEXT('g')) { if (i == argc - 1) { vftprintf(stderr, DB_TEXT("dbexp: Invalid argument: %s\n"), argv[i]); usage(); goto error; }#if defined(SAFEGARDE) if ((cp = vtstrchr(argv[++i], DB_TEXT(','))) != NULL) *cp++ = DB_TEXT('\0'); if (cp) { if (vtstricmp(argv[i], DB_TEXT("low")) == 0) mode = LOW_ENC; else if (vtstricmp(argv[i], DB_TEXT("med")) == 0) mode = MED_ENC; else if (vtstricmp(argv[i], DB_TEXT("high")) == 0) mode = HIGH_ENC; else { vftprintf(stderr, DB_TEXT("dbexp: Invalid encryption mode\n")); usage(); goto error; } password = cp; } else { mode = MED_ENC; password = argv[i]; } if ((xo->sg = sg_create(mode, password)) == NULL) { vftprintf(stderr, DB_TEXT("dbexp: failed to create SafeGarde context\n")); goto error; }#else vftprintf(stderr, DB_TEXT("dbexp: SafeGarde not available in this version\n")); goto error;#endif } else xo->sep_char = argv[i][2]; } else { if (++i < argc) xo->sep_char = *argv[i]; else { vftprintf(stderr, DB_TEXT("dbexp: character must follow -s option\n")); goto error; } } break; default: vftprintf(stderr, DB_TEXT("dbexp: invalid option '%s'\n"), argv[i] + 1); goto error; } } if (i == argc) { vftprintf(stderr, DB_TEXT("dbexp: No database name specified\n")); usage(); goto error; } /* The database name is the first non-option argument */ xo->dbname = argv[i++]; if (i < argc) { xo->n_recs = argc - i; xo->recnames = (DB_TCHAR **) psp_cGetMemory(xo->n_recs * sizeof(DB_TCHAR *), 0); xo->rt_index = (int *) psp_cGetMemory(xo->n_recs * sizeof(int), 0); if (!xo->recnames || !xo->rt_index) { vftprintf(stderr, DB_TEXT("dbexp: out of memory\n")); goto error; } } for (j = 0; j < xo->n_recs; j++, i++) { /* Copy record name string and convert to upper case */ if ((xo->recnames[j] = psp_strdup(argv[i], 0)) == NULL) { vftprintf(stderr, DB_TEXT("dbexp: out of memory\n")); goto error; } vtstrupr(xo->recnames[j]); } if (vistdigit(xo->sep_char)) { vftprintf(stderr, DB_TEXT("dbexp: Separator character must not be numeric\n")); goto error; } if (vistdigit(xo->esc_char)) { vftprintf(stderr, DB_TEXT("dbexp: Escape character must not be numeric\n")); goto error; } return 0;error:
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?