tinycobol/cobroutines/files.c

409 lines
7.7 KiB
C

/*
* Copyright (C) 2003 Wesley Oliveira, Aline Oliveira, Hudson Reis,
* Husni Ali Husni.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1,
* or (at your option) any later version.
*
* This library 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; see the file COPYING.LIB. If
* not, write to the Free Software Foundation, Inc., 59 Temple Place,
* Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include "globals.h"
#ifndef MAXLINE
#define MAXLINE 1024
#define MINLINE 256
#endif
#if defined(SunOS)
#include <curses.h>
#else
# if defined(__CYGWIN__)
# include <ncurses/ncurses.h>
# else
# if defined(__MINGW32__)
# include <curses.h> //# include <pdcurses.h>
# else
# include <curses.h>
# endif
# endif
#endif
char cobpri[MAXLINE], cobpro[MAXLINE];
struct cbl_stream {
char path[MAXLINE];
FILE *fd;
int handle;
} cbl_stream[MAXLINE];
/* Prototypes */
cbl_t cbl_copy_file(char *entrada, char *saida);
cbl_t cbl_rename_file(char *old, char *new);
cbl_t cbl_delete_file(char *arquivo);
cbl_t cbl_check_file_exist(char *arquivo);
cbl_t cbl_move_file(char *origem,char *destino);
void cbl_open_file(char *arquivo,char *access_mode,
char *deny_mode, int file_handle,
cbl_t *retorno);
void cbl_create_file(char *arquivo, unsigned char *access_mode,
unsigned char *deny_mode, char file_handle[5],
cbl_t *retorno);
cbl_t cbl_close_file(int file_handle);
cbl_t cbl_flush_file(int file_handle);
void cbl_read_file(int file_handle, int number_bytes,
char *buffer_read, cbl_t *retorno);
void cbl_write_file(int file_handle,int number_bytes,char *buffer_writed,cbl_t *retorno);
/* Functions */
cbl_t cbl_copy_file(char *entrada, char *saida) {
FILE *fd_in, *fd_out;
int c,valor;
if ( cbl_align(entrada) != 0 ) {
return 1;
}
if( cbl_align(saida) != 0 ) {
return 1;
}
if ( (valor=cbl_parm(entrada,saida)) == -2 ) {
return 0;
}
if ( valor == 1 ) {
return 1;
}
if ( (fd_in = fopen(cobpri,"rb")) == NULL ) {
return 3;
}
else {
if ( (fd_out=fopen(cobpro,"wb")) == NULL ) {
fclose(fd_in);
return 3;
}
}
while ( (c = fgetc(fd_in)) != EOF ) {
fputc(c,fd_out);
}
#ifdef __MSVCRT__
chmod(cobpro,0666);
#else
cbl_get_per(cobpro);
#endif
fclose(fd_in);
fclose(fd_out);
return 0;
}
cbl_t cbl_rename_file(char *old, char *new) {
int valor;
if ( cbl_align(old) != 0 ) {
return 1;
}
if (cbl_align(new) != 0) {
return 1;
}
valor = cbl_parm(old,new);
if(valor == 1) {
return valor;
}
if (rename(cobpri,cobpro) == 0) {
return 0;
}
else {
return 3;
}
}
cbl_t cbl_delete_file(char *arquivo) {
int valor;
if ( cbl_align(arquivo) != 0 ) {
return 1;
}
if ( (valor=cbl_parm(arquivo,""))== -2 ) {
return 0;
}
if ( valor == 1 ) {
return valor;
}
chdir(cobpri);
if (remove(cobpri) != 0) {
return 2;
}
return 0;
}
cbl_t cbl_check_file_exist(char *arquivo) {
int valor;
//DIR *dir;
if (cbl_align(arquivo) != 0) {
return 1;
}
valor = cbl_parm(arquivo,"");
if ( opendir(cobpri) != NULL ) {
return(-1);
}
if ( access(cobpri,F_OK) != 0 ) {
return 1;
}
return 0;
}
cbl_t cbl_move_file(char *origem, char *destino) {
int valor;
if ( cbl_align(origem) != 0 ) {
return 1;
}
if ( cbl_align(destino) != 0 ) {
return 1;
}
valor = cbl_parm(origem,destino);
if ( valor == 1 ) {
return valor;
}
if (cbl_copy_file(cobpri,cobpro) == 0) {
if (cbl_delete_file(cobpri) != 0) {
return 5;
}
}
else {
return 3;
}
return 0;
}
void cbl_open_file(char *arquivo, char *access_mode,
char *deny_mode,int file_handle,
cbl_t *retorno) {
register int i;
int valor;
cbl_align(arquivo);
cbl_align(access_mode);
cbl_align(deny_mode);
valor=cbl_parm(arquivo,"");
// if(valor == 1) { return valor; } walter
if ( valor == 1 ) {
*retorno = 1;
return;
}
if ( !strcmp(access_mode,deny_mode) ) {
*retorno = 1;
return;
}
for ( i=1 ; i<MAXLINE ; i++ ) {
if ( !cbl_stream[i].fd ) {
cbl_stream[i].handle=i;
if ( (cbl_stream[i].fd = fopen(cobpri,access_mode) ) == NULL ) {
*retorno = 1;
}
strcpy(cbl_stream[i].path,cobpri);
*retorno=0;
file_handle=cbl_stream[i].handle;
break;
}
}
}
void cbl_read_file(int file_handle,int number_bytes,char *buffer_read, cbl_t *retorno) {
*retorno = 0;
if ( fread(buffer_read,sizeof(char),number_bytes,cbl_stream[file_handle].fd) == 0 ) {
*retorno = 1;
}
}
void cbl_write_file(int file_handle, int number_bytes,char *buffer_writed, cbl_t *retorno) {
*retorno = 0;
if( fwrite(buffer_writed,sizeof(char),number_bytes,cbl_stream[file_handle].fd) != number_bytes ) {
*retorno = 1;
}
}
cbl_t cbl_close_file(int file_handle) {
register int i;
fclose(cbl_stream[file_handle].fd);
for ( i=0 ; i<MAXLINE ; i++ ) {
cbl_stream[file_handle].path[i]=' ';
}
return 0;
}
cbl_t cbl_flush_file(int file_handle) {
fflush(cbl_stream[file_handle].fd);
return 0;
}
// show file
void show(char *arquivo) {
FILE *file;
char line[1025];
unsigned int max_col, max_lines, p, fline, fcol, ncol, pnt;
char *text, *c, *narq;
int show, ca;
// copy the filename
narq = malloc(sizeof(char) * 256); *narq = 0;
c = arquivo;
p = 0;
while ( *(c+p) != ' ') {
*(narq+p) = *(c+p);
p++;
}
*(narq+p) = 0;
// How many lines and the gratest line ?
file = fopen(narq, "r");
if ( !file )
return;
max_col = 0;
max_lines = 0;
fgets(line, 1024, file);
while ( !feof(file) ) {
if(strlen(line) > max_col)
max_col = strlen(line);
max_lines++;
fgets(line, 1024, file);
}
fclose(file);
file = fopen(narq, "r");
// Alloc memory to put text
text = malloc( (sizeof(char) * ( max_col * max_lines )) + 128 );
*text = 0;
memset(text, ' ', (sizeof(char) * ( max_col * max_lines )) + 128);
fgets(line, 1024, file);
p = 0;
while ( !feof(file) ) {
memcpy(text + p, line, strlen(line));
p += max_col;
fgets(line, 1024, file);
}
fclose(file);
// show the file
fline = 0;
fcol = 0;
ncol = 80;
if(ncol > max_col)
ncol = max_col;
show = 1;
while(show) {
p = 0;
while( (fline + p) < max_lines && p < 24) {
move(p, 0);
pnt = ( (fline + p) * max_col ) + fcol;
addnstr(text + pnt, ncol);
p++;
}
move(0, 0);
ca = getch();
switch(ca) {
case 'i':
case 'I':
fline = 0;
fcol = 0;
break;
case 'a':
case 'A':
if ( fline > 0 )
fline--;
break;
case 'z':
case 'Z':
if ( fline < (max_lines - 24) )
fline++;
break;
case ',':
if ( fcol > 0 )
fcol--;
break;
case '.':
if ( fcol < (max_col - 80) )
fcol++;
break;
case 'f':
case 'F':
show = 0;
break;
}
}
// Return to the application
free(text);
free(narq);
return;
}