⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 file_ops.cpp

📁 ASN.1样例工程代码
💻 CPP
字号:
/*****************************************************************************
File:     file_ops.C
Contents: Functions to get/put files from/to files.
System:   ASN development.
Created:
Author:   Charles W. Gardiner <gardiner@bbn.com>

Remarks:

COPYRIGHT 1995 BBN Systems and Technologies, A Division of BBN Inc.
150 CambridgePark Drive
Cambridge, Ma. 02140
617-873-4000
*****************************************************************************/

#ifndef lint
const char rcsid[]="$Header: /nfs/sub-rosa/u2/IOS_Project/ASN/Dev/rcs/lib/asn_obj/file_ops.C,v 1.7 1995/08/22 21:50:21 gardiner Exp $";
const char sfcsid[] = "@(#)file_ops.C 261P";
#endif

#include "includes.h"
#include "asn_error.h"
#include "asn_obj.h"

static int read_in(int fd, uchar *b, unsigned int size)
{
return read(fd, (char *)b, size);
}

static int write_out(int fd, char *c, unsigned int size)
{
if ((unsigned int)write(fd, c, size) != size) return -1;
return size;
}

int AsnObj::get_file(const char *filename)
{
int fd, ansr;
if ((fd = open(filename, O_RDONLY, 0755)) < 0) ansr = -errno;
else
    {
    ansr = get_file(fd);
    close (fd);
    }
return ansr;
}

int AsnObj::get_file(int fd)
{
long size, lth;
struct stat tstat;
uchar *a, *b, *c;
if (fstat(fd, &tstat)) return -errno;
if ((tstat.st_mode & S_IFREG) == S_IFREG) size = tstat.st_size;
else size = 1024;
if (!(b = c = (uchar *)calloc(size + 2, 1))) return asn_obj_err(ASN_MEM_ERR);
do
    {
    a = &c[size];
    lth = read_in(fd, b, (unsigned)(a - b));
    if ((tstat.st_mode & S_IFREG) == S_IFREG)
	{
	if (read_in(fd, &b[lth], 1)) return -1;
	b += lth;
        break;
	}
    if (lth >= (a - b))     /* can't be >; may need more space */
	{
        if (!(c = (uchar *)realloc(c, size + 1024)))
            return asn_obj_err(ASN_MEM_ERR);
        b = &c[size];       /* end of last read_in */
	size += 1024;
        }
    else b += lth;
    }
while (lth > 0);
size = (b - c);
close(fd);
if ((lth = decode(c)) != size)
    {
    if ((size = lth) > 0)
	{
        asn_obj_err(ASN_FILE_ERR);
        size = -lth;
	}
    }
free(c);
return size;
}

int AsnObj::put_file(int fd) const
{
	int ansr;
	char *c;
	if ((ansr = this->size()) < 0) 
		return ansr;

	if (!(c = (char *)calloc(ansr, 1))) 
		return asn_obj_err(ASN_MEM_ERR);
	else if (this->encode((uchar *)c) != ansr ||
		write_out(fd, c, (unsigned int)ansr) != ansr) ansr = -1;
	free(c);
	return ansr;
}

int AsnObj::put_file(const char *fname) const
{
int ansr, fd;

if ((fd = open(fname, O_WRONLY | O_TRUNC | O_CREAT, 0755)) < 0)
    ansr = -errno;
else ansr = this->put_file(fd);
close(fd);
return ansr;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -