2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,5 +1,12 @@
|
|||
Truncate a file to a specific length. This should be implemented as a routine that takes two parameters: the filename and the required file length (in bytes).
|
||||
;Task:
|
||||
Truncate a file to a specific length. This should be implemented as a routine that takes two parameters: the filename and the required file length (in bytes).
|
||||
|
||||
Truncation can be achieved using system or library calls intended for such a task, if such methods exist, or by creating a temporary file of a reduced size and renaming it, after first deleting the original file, if no other method is available. The file may contain non human readable binary data in an unspecified format, so the routine should be "binary safe", leaving the contents of the untruncated part of the file unchanged.
|
||||
|
||||
If the specified filename does not exist, or the provided length is not less than the current file length, then the routine should raise an appropriate error condition. On some systems, the provided file truncation facilities might not change the file or may extend the file, if the specified length is greater than the current length of the file. This task permits the use of such facilities. However, such behaviour should be noted, or optionally a warning message relating to an non change or increase in file size may be implemented.
|
||||
Truncation can be achieved using system or library calls intended for such a task, if such methods exist, or by creating a temporary file of a reduced size and renaming it, after first deleting the original file, if no other method is available. The file may contain non human readable binary data in an unspecified format, so the routine should be "binary safe", leaving the contents of the untruncated part of the file unchanged.
|
||||
|
||||
If the specified filename does not exist, or the provided length is not less than the current file length, then the routine should raise an appropriate error condition.
|
||||
|
||||
On some systems, the provided file truncation facilities might not change the file or may extend the file, if the specified length is greater than the current length of the file.
|
||||
|
||||
This task permits the use of such facilities. However, such behaviour should be noted, or optionally a warning message relating to an non change or increase in file size may be implemented.
|
||||
<br><br>
|
||||
|
|
|
|||
29
Task/Truncate-a-file/Elena/truncate-a-file.elena
Normal file
29
Task/Truncate-a-file/Elena/truncate-a-file.elena
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#import system.
|
||||
#import system'io.
|
||||
#import extensions.
|
||||
|
||||
#class(extension:file_path)fileOp
|
||||
{
|
||||
#method set &length:length
|
||||
[
|
||||
#var(type:stream)stream := FileStream openForEdit &path:self.
|
||||
|
||||
stream set &length:length.
|
||||
|
||||
stream close.
|
||||
]
|
||||
}
|
||||
|
||||
#symbol program =
|
||||
[
|
||||
('program'arguments length != 3)?
|
||||
[ console << "Please provide the path to the file and a new length". #throw AbortException new. ].
|
||||
|
||||
#var fileName := 'program'arguments@1.
|
||||
#var length := ('program'arguments@2) toInt.
|
||||
|
||||
(fileName file_path is &available)
|
||||
! [ console writeLine:"File ":fileName:" does not exist". #throw AbortException new. ].
|
||||
|
||||
fileName file_path set &length:length.
|
||||
].
|
||||
46
Task/Truncate-a-file/Fortran/truncate-a-file.f
Normal file
46
Task/Truncate-a-file/Fortran/truncate-a-file.f
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
SUBROUTINE CROAK(GASP) !Something bad has happened.
|
||||
CHARACTER*(*) GASP !As noted.
|
||||
WRITE (6,*) "Oh dear. ",GASP !So, gasp away.
|
||||
STOP "++ungood." !Farewell, cruel world.
|
||||
END !No return from this.
|
||||
|
||||
SUBROUTINE FILEHACK(FNAME,NB)
|
||||
CHARACTER*(*) FNAME !Name for the file.
|
||||
INTEGER NB !Number of bytes to survive.
|
||||
INTEGER L !A counter for te length of the file.
|
||||
INTEGER F,T !Mnemonics for file unit numbers.
|
||||
PARAMETER (F=66,T=67) !These should do.
|
||||
LOGICAL EXIST !Same as the mnemonic so left/right can be forgotten.
|
||||
CHARACTER*1 B !The worker!
|
||||
IF (FNAME.EQ."") CALL CROAK("Blank file name!")
|
||||
IF (NB.LE.0) CALL CROAK("Chop must be positive!")
|
||||
INQUIRE(FILE = FNAME, EXIST = EXIST) !This mishap is frequent, so attend to it.
|
||||
IF (.NOT.EXIST) CALL CROAK("Can't find a file called "//FNAME) !Tough love.
|
||||
OPEN (F,FILE=FNAME,STATUS="OLD",ACTION="READWRITE", !Grab the source file.
|
||||
1 FORM="UNFORMATTED",RECL=1,ACCESS="DIRECT") !Oh dear.
|
||||
OPEN (T,STATUS="SCRATCH",FORM="UNFORMATTED",RECL=1) !Request a temporary file.
|
||||
|
||||
Copy the desired "records" to the temporary file.
|
||||
10 DO L = 1,NB !Only up to a point.
|
||||
READ (F,REC = L,ERR = 20) B !One whole byte!
|
||||
WRITE (T) B !And, write it too!
|
||||
END DO !Again.
|
||||
20 IF (L.LE.NB) CALL CROAK("Short file!") !Should end the loop with L = NB + 1.
|
||||
Convert from input to output...
|
||||
REWIND T !Not CLOSE! That would discard the file!
|
||||
CLOSE(F) !The source file still exists.
|
||||
OPEN (F,FILE=FNAME,FORM="FORMATTED", !But,
|
||||
1 ACTION="WRITE",STATUS="REPLACE") !This dooms it!
|
||||
Copy from the temporary file.
|
||||
DO L = 1,NB !A certain number only.
|
||||
READ (T) B !One at at timne.
|
||||
WRITE (F,"(A1,$)") B !The $, obviously, means no end-of-record appendage.
|
||||
END DO !And again.
|
||||
Completed.
|
||||
30 CLOSE(T) !Abandon the temporary file.
|
||||
CLOSE(F) !Finished with the source file.
|
||||
END !Done.
|
||||
|
||||
PROGRAM CHOPPER
|
||||
CALL FILEHACK("foobar.txt",12)
|
||||
END
|
||||
14
Task/Truncate-a-file/Lua/truncate-a-file.lua
Normal file
14
Task/Truncate-a-file/Lua/truncate-a-file.lua
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
function truncate (filename, length)
|
||||
local inFile = io.open(filename, 'r')
|
||||
if not inFile then
|
||||
error("Specified filename does not exist")
|
||||
end
|
||||
local wholeFile = inFile:read("*all")
|
||||
inFile:close()
|
||||
if length >= wholeFile:len() then
|
||||
error("Provided length is not less than current file length")
|
||||
end
|
||||
local outFile = io.open(filename, 'w')
|
||||
outFile:write(wholeFile:sub(1, length))
|
||||
outFile:close()
|
||||
end
|
||||
3
Task/Truncate-a-file/PARI-GP/truncate-a-file.pari
Normal file
3
Task/Truncate-a-file/PARI-GP/truncate-a-file.pari
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
install("truncate", "isL", "trunc")
|
||||
|
||||
trunc("/tmp/test.file", 20)
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
use NativeCall;
|
||||
|
||||
sub truncate(Str, Int --> int) is native {*}
|
||||
sub truncate(Str, int32 --> int32) is native {*}
|
||||
|
||||
sub MAIN (Str $file, Int $to) {
|
||||
given $file.IO {
|
||||
|
|
|
|||
|
|
@ -1,20 +1,20 @@
|
|||
/*REXX program truncates a file to a specified (smaller) number of bytes.*/
|
||||
parse arg siz FID /*get required arguments from the C.L. */
|
||||
FID=strip(FID) /*elide leading/trailing blanks from FID*/
|
||||
/*REXX program truncates a file to a specified (and smaller) number of bytes. */
|
||||
parse arg siz FID /*obtain required arguments from the CL*/
|
||||
FID=strip(FID) /*elide FID leading/trailing blanks. */
|
||||
if siz=='' then call ser "No truncation size was specified (1st argument)."
|
||||
if FID=='' then call ser "No fileID was specified (2nd argument)."
|
||||
if \datatype(siz,'W') then call ser "trunc size isn't an integer: " siz
|
||||
if siz<1 then call ser "trunc size isn't a positive integer: " siz
|
||||
_=charin(FID,1,siz+1) /*position file and read a wee bit more*/
|
||||
#=length(_) /*get the length of the part just read.*/
|
||||
if #==0 then call ser "the specified file doesn't exist: " FID
|
||||
if \datatype(siz,'W') then call ser "trunc size isn't an integer: " siz
|
||||
if siz<1 then call ser "trunc size isn't a positive integer: " siz
|
||||
_=charin(FID,1,siz+1) /*position file and read a wee bit more*/
|
||||
#=length(_) /*get the length of the part just read.*/
|
||||
if #==0 then call ser "the specified file doesn't exist: " FID
|
||||
if #<siz then call ser "the file is smaller than trunc size: " #
|
||||
call lineout FID /*close the file used, just to be safe.*/
|
||||
'ERASE' FID /*invoke a command to delete the file */
|
||||
call lineout FID /*close the file, maybe for REXX's use.*/
|
||||
call charout FID, left(_,siz), 1 /*write a truncated version of the file*/
|
||||
call lineout FID /*close the file used, just to be safe.*/
|
||||
say 'file ' FID " truncated to " siz 'bytes.' /*display some info*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
ser: say '***error!***' arg(1); exit 13 /*display an error message.*/
|
||||
call lineout FID /*close the file used, just to be safe.*/
|
||||
'ERASE' FID /*invoke a command to delete the file */
|
||||
call lineout FID /*close the file, maybe for REXX's use.*/
|
||||
call charout FID, left(_,siz), 1 /*write a truncated version of the file*/
|
||||
call lineout FID /*close the file used, just to be safe.*/
|
||||
say 'file ' FID " truncated to " siz 'bytes.' /*display some information to terminal.*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
ser: say '***error***' arg(1); exit 13 /*display an error message and exit. */
|
||||
|
|
|
|||
|
|
@ -1,26 +1,27 @@
|
|||
/*REXX program truncates a file to a specified (smaller) number of bytes.*/
|
||||
parse arg siz FID /*get required arguments from the C.L. */
|
||||
FID=strip(FID) /*elide leading/trailing blanks from FID*/
|
||||
/*REXX program truncates a file to a specified (and smaller) number of bytes. */
|
||||
parse arg siz FID /*obtain required arguments from the CL*/
|
||||
FID=strip(FID) /*elide FID leading/trailing blanks. */
|
||||
if siz=='' then call ser "No truncation size was specified (1st argument)."
|
||||
if FID=='' then call ser "No fileID was specified (2nd argument)."
|
||||
if \datatype(siz,'W') then call ser "trunc size isn't an integer: " siz
|
||||
if siz<1 then call ser "trunc size isn't a positive integer: " siz
|
||||
chunk=4000 /*read the file with this "buffer" size*/
|
||||
call charin fid,1,0 /*position the file pointer to byte # 1*/
|
||||
_= /* [↓] read while the length data<siz.*/
|
||||
do while length(_)<=siz /* [↓] have we reached End-Of-File ? */
|
||||
buffer=charin(FID,,chunk); if length(buffer)==0 then leave
|
||||
_=_ || buffer /*append the chunk to the _ input data*/
|
||||
end /*while ··· */
|
||||
#=length(_) /*get the length of the part just read.*/
|
||||
if #==0 then call ser "the specified file doesn't exist: " FID
|
||||
if \datatype(siz,'W') then call ser "trunc size isn't an integer: " siz
|
||||
if siz<1 then call ser "trunc size isn't a positive integer: " siz
|
||||
chunk=4000 /*read the file with this "buffer" size*/
|
||||
call charin fid,1,0 /*position the file pointer to byte # 1*/
|
||||
_= /* [↓] read while the length data<siz.*/
|
||||
do while length(_)<=siz /* [↓] have we reached End-Of-File ? */
|
||||
buffer=charin(FID,,chunk)
|
||||
if length(buffer)==0 then leave /*Nothing read? Then we're done reading*/
|
||||
_=_ || buffer /*append the chunk to the _ input data*/
|
||||
end /*while*/
|
||||
#=length(_) /*get the length of the part just read.*/
|
||||
if #==0 then call ser "the specified file doesn't exist: " FID
|
||||
if #<siz then call ser "the file is smaller than trunc size: " #
|
||||
call lineout FID /*close the file used, just to be safe.*/
|
||||
'ERASE' FID /*invoke a command to delete the file */
|
||||
call lineout FID /*close the file, maybe for REXX's use.*/
|
||||
call charout FID, left(_,siz), 1 /*write a truncated version of the file*/
|
||||
call lineout FID /*close the file used, just to be safe.*/
|
||||
say 'file ' FID " truncated to " siz 'bytes.' /*display some info*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
ser: say '***error!***' arg(1); exit 13 /*display an error message.*/
|
||||
call lineout FID /*close the file used, just to be safe.*/
|
||||
'ERASE' FID /*invoke a command to delete the file */
|
||||
call lineout FID /*close the file, maybe for REXX's use.*/
|
||||
call charout FID, left(_,siz), 1 /*write a truncated version of the file*/
|
||||
call lineout FID /*close the file used, just to be safe.*/
|
||||
say 'file ' FID " truncated to " siz 'bytes.' /*display some information to terminal.*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
ser: say '***error***' arg(1); exit 13 /*display an error message and exit. */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue