September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
45
Task/Truncate-a-file/AWK/truncate-a-file.awk
Normal file
45
Task/Truncate-a-file/AWK/truncate-a-file.awk
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
# syntax: GAWK -f TRUNCATE_A_FILE.AWK
|
||||
BEGIN {
|
||||
main("NOTHERE",100)
|
||||
main("FILENAME.TMP",-1)
|
||||
main("FILENAME.TMP",500)
|
||||
exit(0)
|
||||
}
|
||||
function main(filename,size, ret) {
|
||||
ret = truncate_file(filename,size)
|
||||
if (ret != "") {
|
||||
printf("error: FILENAME=%s, %s\n",filename,ret)
|
||||
}
|
||||
}
|
||||
function truncate_file(filename,size, cmd,fnr,msg,old_BINMODE,old_RS,rec) {
|
||||
cmd = sprintf("ls --full-time -o %s",filename)
|
||||
if (size < 0) {
|
||||
return("size cannot be negative")
|
||||
}
|
||||
old_BINMODE = BINMODE
|
||||
old_RS = RS
|
||||
BINMODE = 3
|
||||
RS = "[^\x00-\xFF]"
|
||||
while (getline rec <filename > 0) {
|
||||
fnr++
|
||||
}
|
||||
close(filename)
|
||||
if (fnr == 0) {
|
||||
msg = "file not found"
|
||||
}
|
||||
if (fnr > 1) {
|
||||
msg = "choose a different RecordSeparator"
|
||||
}
|
||||
if (msg == "") { # no errors
|
||||
system(cmd) # optional: show filesize before truncation
|
||||
if (length(rec) > size) {
|
||||
rec = substr(rec,1,size)
|
||||
}
|
||||
printf("%s",rec) >filename
|
||||
close(filename)
|
||||
system(cmd) # optional: show filesize after truncation
|
||||
}
|
||||
BINMODE = old_BINMODE
|
||||
RS = old_RS
|
||||
return(msg)
|
||||
}
|
||||
20
Task/Truncate-a-file/C++/truncate-a-file.cpp
Normal file
20
Task/Truncate-a-file/C++/truncate-a-file.cpp
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
#include <string>
|
||||
#include <fstream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void truncateFile(string filename, int max_size) {
|
||||
std::ifstream input( filename, std::ios::binary );
|
||||
char buffer;
|
||||
string outfile = filename + ".trunc";
|
||||
ofstream appendFile(outfile, ios_base::out);
|
||||
for(int i=0; i<max_size; i++) {
|
||||
input.read( &buffer, sizeof(buffer) );
|
||||
appendFile.write(&buffer,1);
|
||||
}
|
||||
appendFile.close(); }
|
||||
|
||||
int main () {
|
||||
truncateFile("test.txt", 5);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,29 +1,28 @@
|
|||
#import system.
|
||||
#import system'io.
|
||||
#import extensions.
|
||||
import system'io.
|
||||
import extensions.
|
||||
|
||||
#class(extension:file_path)fileOp
|
||||
file_info extension fileOp
|
||||
{
|
||||
#method set &length:length
|
||||
set length:length
|
||||
[
|
||||
#var(type:stream)stream := FileStream openForEdit &path:self.
|
||||
stream stream := FileStream openForEdit:self.
|
||||
|
||||
stream set &length:length.
|
||||
stream set length:length.
|
||||
|
||||
stream close.
|
||||
]
|
||||
}
|
||||
|
||||
#symbol program =
|
||||
program =
|
||||
[
|
||||
('program'arguments length != 3)?
|
||||
[ console << "Please provide the path to the file and a new length". #throw AbortException new. ].
|
||||
if ('program'arguments length != 3)
|
||||
[ console printLine:"Please provide the path to the file and a new length". AbortException new; raise ].
|
||||
|
||||
#var fileName := 'program'arguments@1.
|
||||
#var length := ('program'arguments@2) toInt.
|
||||
file_info file := File new('program'arguments[1]).
|
||||
var length := 'program'arguments[2] toInt.
|
||||
|
||||
(fileName file_path is &available)
|
||||
! [ console writeLine:"File ":fileName:" does not exist". #throw AbortException new. ].
|
||||
ifnot (file isAvailable)
|
||||
[ console printLine("File ",file," does not exist"). AbortException new; raise ].
|
||||
|
||||
fileName file_path set &length:length.
|
||||
file set length:length.
|
||||
].
|
||||
|
|
|
|||
25
Task/Truncate-a-file/Kotlin/truncate-a-file.kotlin
Normal file
25
Task/Truncate-a-file/Kotlin/truncate-a-file.kotlin
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
// version 1.1.2
|
||||
|
||||
import java.io.FileOutputStream
|
||||
import java.nio.channels.FileChannel
|
||||
|
||||
fun truncateFile(fileName: String, newSize: Long) {
|
||||
var fc: FileChannel? = null
|
||||
try {
|
||||
fc = FileOutputStream(fileName, true).channel
|
||||
if (newSize >= fc.size())
|
||||
println("Requested file size isn't less than existing size")
|
||||
else
|
||||
fc.truncate(newSize)
|
||||
}
|
||||
catch (ex: Exception) {
|
||||
println(ex.message)
|
||||
}
|
||||
finally {
|
||||
fc!!.close()
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
truncateFile("test.txt", 10)
|
||||
}
|
||||
3
Task/Truncate-a-file/PowerShell/truncate-a-file.psh
Normal file
3
Task/Truncate-a-file/PowerShell/truncate-a-file.psh
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Function Truncate-File(fname) {
|
||||
$null | Set-Content -Path "$fname"
|
||||
}
|
||||
|
|
@ -5,5 +5,4 @@ def truncate_file(name, length):
|
|||
return False
|
||||
with open(name, 'ab') as f:
|
||||
f.truncate(length)
|
||||
f.close()
|
||||
return True
|
||||
return True
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue