Code Monkey home page Code Monkey logo

tmpfile.zig's Introduction

tmpfile.zig

why

So far as I found there is no good lib in zig for creating temp files, so I write one for myself. This util file provides methods to create temp dir or temp file in system temp folder less tedious and manageable.

see code for example

var tmp_file = try ThisModule.tmpFile(.{});
defer tmp_file.deinit(); // file will be deleted when deinit
try tmp_file.f.writeAll("hello, world!");
try tmp_file.f.seekTo(0);
var buf: [4096]u8 = undefined;
var read_count = try tmp_file.f.readAll(&buf);
try testing.expectEqual(read_count, "hello, world!".len);
try testing.expectEqualSlices(u8, buf[0..read_count], "hello, world!");

or complexer

var tmp_dir = try ThisModule.tmpDirOwned(.{});
defer {
    tmp_dir.deinit();
    tmp_dir.allocator.destroy(tmp_dir);
}

var tmp_file = try ThisModule.tmpFile(.{ .tmp_dir = tmp_dir });
defer tmp_file.deinit();
try tmp_file.f.writeAll("hello, world!");
try tmp_file.f.seekTo(0);
var buf: [4096]u8 = undefined;
var read_count = try tmp_file.f.readAll(&buf);
try testing.expectEqual(read_count, "hello, world!".len);
try testing.expectEqualSlices(u8, buf[0..read_count], "hello, world!");

var tmp_file2 = try ThisModule.tmpFile(.{ .tmp_dir = tmp_dir });
defer tmp_file2.deinit();
try tmp_file2.f.writeAll("hello, world!2");
try tmp_file2.f.seekTo(0);
read_count = try tmp_file2.f.readAll(&buf);
try testing.expectEqual(read_count, "hello, world!2".len);
try testing.expectEqualSlices(u8, buf[0..read_count], "hello, world!2");

useage

use zig packager

zig fetch --save https://github.com/liyu1981/tmpfile.zig/archive/refs/heads/main.tar.gz

(or lock on any commit as)

zig fetch --save zig fetch https://github.com/liyu1981/zcmd.zig/archive/<commit hash>.tar.gz

this lib is also provided for build.zig, use like

// in build.zig
const tmpfile = @import("tmpfile").tmpfile;

license

MIT

tmpfile.zig's People

Contributors

dgv avatar liyu1981 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.