filekind

package
v0.0.0-...-1dd1f65 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2023 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package filekind contains types for dealing with the various 'kinds' of file present in a subject.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrBadKind = errors.New("unknown kind")

ErrBadKind occurs if we try to convert a kind from a string that doesn't match any known kind string.

Functions

This section is empty.

Types

type Kind

type Kind uint8

Kind is the bitflag enumeration of file kinds.

const (
	// Other states that the kind of this file doesn't fit in any of the above categorisations.
	Other Kind = 1 << iota
	// Litmus states that this file is a litmus test.
	Litmus
	// Bin states that this file is a binary.
	Bin
	// Log states that this file is a compile log.
	Log
	// Trace states that this file is a fuzzer trace.
	Trace
	// CSrc states that this file is C source code (.c).
	CSrc
	// CHeader states that this file is a C header (.h).
	CHeader

	// C is shorthand for CSrc|CHeader.
	C = CSrc | CHeader

	// Any is a suggestive alias for both Loc and Kind saturation.
	Any = math.MaxUint8
)

func GuessFromFile

func GuessFromFile(fpath string) Kind

GuessFromFile tries to guess the kind of the file whose filepath is path. It does so only from the file extension.

Example

ExampleGuessFromFile is a runnable example for GuessFromFile.

package main

import (
	"fmt"

	"github.com/c4-project/c4t/internal/model/filekind"
)

func main() {
	fmt.Println(filekind.GuessFromFile("foo.litmus.c") == filekind.CSrc)
	fmt.Println(filekind.GuessFromFile("stdio.h") == filekind.CHeader)
	fmt.Println(filekind.GuessFromFile("foo.litmus") == filekind.Litmus)
	fmt.Println(filekind.GuessFromFile("foo.litmus.c") == filekind.Litmus)

}
Output:

true
true
true
false

func KindFromString

func KindFromString(str string) (Kind, error)

KindFromString tries to map str to a filekind.

func KindFromStrings

func KindFromStrings(strs ...string) (Kind, error)

KindFromStrings returns the union of the kinds corresponding to each string in strs.

Example

ExampleKindFromStrings is a runnable example for KindFromStrings.

package main

import (
	"fmt"

	"github.com/c4-project/c4t/internal/model/filekind"
)

func main() {
	k, err := filekind.KindFromStrings("other", "c")
	if err != nil {
		fmt.Println("ERROR:", err)
	} else {
		fmt.Println(k.String())
	}

}
Output:

other|c

func (Kind) ArchivePerm

func (k Kind) ArchivePerm() int64

ArchivePerm gets the idealised Unix permission set for archiving a file of this kind.

func (Kind) FilterFiles

func (k Kind) FilterFiles(fpaths []string) []string

FilterFiles filters fpaths to paths whose guess matches this kind.

Example

ExampleKind_FilterFiles is a runnable example for FilterFiles.

package main

import (
	"fmt"

	"github.com/c4-project/c4t/internal/model/filekind"
)

func main() {
	for _, f := range filekind.C.FilterFiles([]string{
		"barbaz.trace",
		"foo.c",
		"foo.h",
		"baz.sh",
		"a.out",
		"bar.c.litmus",
		"bar.c",
	}) {
		fmt.Println(f)
	}

}
Output:

foo.c
foo.h
bar.c

func (Kind) MarshalJSON

func (k Kind) MarshalJSON() ([]byte, error)

MarshalJSON marshals a filekind to JSON using its string-list form.

func (Kind) Matches

func (k Kind) Matches(pat Kind) bool

Matches checks whether this kind is included in pat.

func (Kind) String

func (k Kind) String() string

String produces a stringified representation of a filekind.

func (Kind) Strings

func (k Kind) Strings() []string

Strings produces a stringified representation of each flag enabled in this filekind.

Example

ExampleKind_Strings is a runnable example for Strings.

package main

import (
	"fmt"

	"github.com/c4-project/c4t/internal/model/filekind"
)

func main() {
	for _, s := range (filekind.C | filekind.Litmus | filekind.Trace).Strings() {
		fmt.Println(s)
	}

}
Output:

c
litmus
trace

func (*Kind) UnmarshalJSON

func (k *Kind) UnmarshalJSON(bytes []byte) error

UnmarshalJSON unmarshals an op from JSON using its string-list form.

type Loc

type Loc uint8

Loc is the bitflag enumeration of file locations.

const (
	// InOrig marks that a mapping is part of the original source of a subject.
	InOrig Loc = 1 << iota
	// InFuzz marks that a mapping is part of a fuzz.
	InFuzz
	// InCompile marks that a mapping is part of a compile.
	InCompile
	// InRecipe marks that a mapping is part of a recipe.
	InRecipe
)

func (Loc) Matches

func (l Loc) Matches(pat Loc) bool

Matches checks whether this location is included in pat.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL