kernaux 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f3d010f1c8a8ce21a88ce43e74f74cefd3948499192dfb15c86a7bdbec8fb207
4
+ data.tar.gz: 22cc8aa38ee888bcd8812eb5bc0632111211833a74f4c2d456356d0c7420657b
5
+ SHA512:
6
+ metadata.gz: 99134a6e80f7a9891de94d80e510e8d022d9522c2c1630e8ac7a0c23a7ee1e38ee9783a86eee91c7128af019d9ebfb415a650857e862fe7a826057768c3bfffb
7
+ data.tar.gz: 42d27fc4592ae2e6c7c760b5ae5804e88cd3c8e7d67e6900da11f64bc1b73d9d77aa7c30a17aaacab8b678d26b9b11ebd3717c7967ac1601edf1654f9b53b9ef
data/.gitignore ADDED
@@ -0,0 +1,44 @@
1
+ *.gem
2
+ *.rbc
3
+ *.so
4
+
5
+ /.byebug_history
6
+ /.config/
7
+ /.rake_tasks~
8
+ /InstalledFiles/
9
+ /pkg/
10
+ /tmp/
11
+
12
+ # Used by dotenv library to load environment variables.
13
+ /.env
14
+
15
+ # RSpec configuration and generated files.
16
+ /.rspec
17
+ /coverage/
18
+ /spec/examples.txt
19
+ /spec/reports/
20
+ /test/tmp/
21
+ /test/version_tmp/
22
+
23
+ # Documentation cache and generated files.
24
+ /.yardoc/
25
+ /_yardoc/
26
+ /doc/
27
+ /rdoc/
28
+
29
+ # Environment normalization.
30
+ /.bundle/
31
+ /vendor/bundle/
32
+ /lib/bundler/man/
33
+
34
+ # For a library or gem, you might want to ignore these files since the code is
35
+ # intended to run in multiple environments; otherwise, check them in.
36
+ /Gemfile.lock
37
+ /.ruby-version
38
+ /.ruby-gemset
39
+
40
+ # Unless supporting rvm < 1.11.0 or doing something fancy, ignore this.
41
+ /.rvmrc
42
+
43
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
44
+ /.rubocop-https?--*
data/.rubocop.yml ADDED
@@ -0,0 +1,79 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rake
4
+ - rubocop-rspec
5
+
6
+ AllCops:
7
+ TargetRubyVersion: 3.0
8
+ DisplayCopNames: true
9
+ NewCops: enable
10
+
11
+ Layout/AccessModifierIndentation:
12
+ EnforcedStyle: outdent
13
+
14
+ Layout/LineLength:
15
+ Max: 80
16
+
17
+ Lint/AmbiguousOperatorPrecedence:
18
+ Enabled: false
19
+
20
+ Lint/ReturnInVoidContext:
21
+ Enabled: false
22
+
23
+ Metrics/BlockLength:
24
+ Exclude:
25
+ - '*.gemspec'
26
+ - 'Rakefile'
27
+ - 'spec/**/*_spec.rb'
28
+
29
+ RSpec/ContextWording:
30
+ Prefixes:
31
+ - 'and'
32
+ - 'for'
33
+ - 'using'
34
+ - 'when'
35
+ - 'with'
36
+ - 'without'
37
+
38
+ RSpec/FilePath:
39
+ CustomTransform:
40
+ KernAux: kernaux
41
+
42
+ RSpec/ExampleLength:
43
+ CountAsOne: ['array', 'hash', 'heredoc']
44
+
45
+ Security/Eval:
46
+ Exclude:
47
+ - 'spec/**/*_spec.rb'
48
+
49
+ Style/AndOr:
50
+ EnforcedStyle: conditionals
51
+
52
+ Style/Documentation:
53
+ Exclude:
54
+ - 'Rakefile'
55
+
56
+ Style/DoubleNegation:
57
+ Enabled: false
58
+
59
+ Style/GlobalVars:
60
+ Exclude:
61
+ - 'ext/*/extconf.rb'
62
+
63
+ Style/HashAsLastArrayItem:
64
+ Enabled: false
65
+
66
+ Style/PerlBackrefs:
67
+ Enabled: false
68
+
69
+ Style/TrailingCommaInArguments:
70
+ EnforcedStyleForMultiline: comma
71
+
72
+ Style/TrailingCommaInArrayLiteral:
73
+ EnforcedStyleForMultiline: comma
74
+
75
+ Style/TrailingCommaInHashLiteral:
76
+ EnforcedStyleForMultiline: comma
77
+
78
+ Style/VariableInterpolation:
79
+ Enabled: false
data/.simplecov ADDED
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ if ENV['SKIP_COVERAGE'].to_s.empty?
4
+ SimpleCov.start do
5
+ minimum_coverage 95
6
+
7
+ add_filter '/spec/'
8
+ end
9
+ end
data/.yardopts ADDED
@@ -0,0 +1,4 @@
1
+ --markup markdown
2
+ --readme README.md
3
+ --protected
4
+ --private
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in kernaux.gemspec
6
+ gemspec
7
+
8
+ gem 'simplecov', require: false
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ ::KernAux
2
+ =========
3
+
4
+ [![Build status](https://github.com/tailix/libkernaux/actions/workflows/ruby.yml/badge.svg)](https://github.com/tailix/libkernaux/actions/workflows/ruby.yml)
5
+ [![Build status (FreeBSD)](https://api.cirrus-ci.com/github/tailix/libkernaux.svg?task=Ruby%20(FreeBSD))](https://cirrus-ci.com/github/tailix/libkernaux)
6
+
7
+ Binding to [libkernaux](https://github.com/tailix/libkernaux) - auxiliary
8
+ library for kernel development.
9
+
10
+
11
+
12
+ Install
13
+ -------
14
+
15
+ Add the following to your `Gemfile`:
16
+
17
+ ```ruby
18
+ gem 'kernaux', '~> 0.5.0'
19
+ ```
20
+
21
+ Or add the following to your `*.gemspec`:
22
+
23
+ ```ruby
24
+ Gem::Specification.new do |spec|
25
+ # ...
26
+ spec.add_runtime_dependency 'kernaux', '~> 0.5.0'
27
+ # ...
28
+ end
29
+ ```
data/Rakefile ADDED
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+
5
+ module Bundler
6
+ class GemHelper
7
+ def tag_version(*)
8
+ yield if block_given?
9
+ end
10
+
11
+ def git_push(*); end
12
+
13
+ def perform_git_push(*); end
14
+ end
15
+ end
16
+
17
+ CLEAN << '.yardoc'
18
+ CLEAN << 'coverage'
19
+ CLEAN << 'doc'
20
+ CLEAN << 'spec/examples.txt'
21
+
22
+ desc 'Run default checks'
23
+ task default: %i[test lint yard:cov]
24
+
25
+ desc 'Run tests'
26
+ task test: :spec
27
+
28
+ desc 'Run code analysis tools'
29
+ task lint: :rubocop
30
+
31
+ desc 'Fix code style (rubocop --auto-correct)'
32
+ task fix: 'rubocop:auto_correct'
33
+
34
+ begin
35
+ require 'rspec/core/rake_task'
36
+ RSpec::Core::RakeTask.new
37
+ rescue LoadError
38
+ nil
39
+ end
40
+
41
+ begin
42
+ require 'rubocop/rake_task'
43
+ RuboCop::RakeTask.new
44
+ rescue LoadError
45
+ nil
46
+ end
47
+
48
+ begin
49
+ require 'yard'
50
+ YARD::Rake::YardocTask.new
51
+ rescue LoadError
52
+ nil
53
+ end
54
+
55
+ begin
56
+ require 'rake/extensiontask'
57
+ Rake::ExtensionTask.new 'default' do |ext|
58
+ ext.lib_dir = 'lib/kernaux'
59
+ end
60
+ rescue LoadError
61
+ nil
62
+ end
63
+
64
+ desc 'Open development console'
65
+ task :console do
66
+ sh 'bundle', 'exec', File.expand_path(File.join('bin', 'console'), __dir__)
67
+ end
68
+
69
+ namespace :yard do
70
+ desc 'Measure documentation coverage'
71
+ task :cov do
72
+ result = `bundle exec yard stats`.lines.last.strip.freeze
73
+ m = result.match(/\A(\d+(\.\d+)?)% documented\z/)
74
+ raise 'Invalid result' if m.nil?
75
+
76
+ coverage = m[1].to_f.round(2)
77
+ puts "Documentation coverage: #{coverage}%"
78
+ raise 'Not fully documented!' if coverage != 100
79
+ end
80
+ end
data/bin/console ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'kernaux'
6
+ require 'pry'
7
+
8
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/sh
2
+
3
+ set -eux
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,55 @@
1
+ #include "main.h"
2
+
3
+ static void assert_cb(const char *file, int line, const char *msg);
4
+
5
+ static VALUE rb_KernAux_assert_cb(VALUE self);
6
+ static VALUE rb_KernAux_assert_cb_EQ(VALUE self, VALUE assert_cb);
7
+ static
8
+ VALUE rb_KernAux_assert_do(VALUE self, VALUE file, VALUE line, VALUE msg);
9
+
10
+ void init_assert()
11
+ {
12
+ kernaux_assert_cb = assert_cb;
13
+
14
+ rb_define_singleton_method(rb_KernAux, "assert_cb",
15
+ rb_KernAux_assert_cb, 0);
16
+ rb_define_singleton_method(rb_KernAux, "assert_cb=",
17
+ rb_KernAux_assert_cb_EQ, 1);
18
+ rb_define_singleton_method(rb_KernAux, "assert_do",
19
+ rb_KernAux_assert_do, 3);
20
+ }
21
+
22
+ void assert_cb(const char *const file, const int line, const char *const msg)
23
+ {
24
+ const VALUE assert_cb_rb = rb_iv_get(rb_KernAux, "@assert_cb");
25
+ if (assert_cb_rb == Qnil) return;
26
+ const VALUE file_rb = rb_str_new2(file);
27
+ const VALUE line_rb = INT2FIX(line);
28
+ const VALUE msg_rb = rb_str_new2(msg);
29
+ rb_funcall(assert_cb_rb, rb_intern_call, 3, file_rb, line_rb, msg_rb);
30
+ }
31
+
32
+ VALUE rb_KernAux_assert_cb(const VALUE self)
33
+ {
34
+ return rb_iv_get(self, "@assert_cb");
35
+ }
36
+
37
+ VALUE rb_KernAux_assert_cb_EQ(const VALUE self, const VALUE assert_cb_rb)
38
+ {
39
+ return rb_iv_set(self, "@assert_cb", assert_cb_rb);
40
+ }
41
+
42
+ VALUE rb_KernAux_assert_do(
43
+ const VALUE self_rb __attribute__((unused)),
44
+ VALUE file_rb,
45
+ const VALUE line_rb,
46
+ VALUE msg_rb
47
+ ) {
48
+ const char *const file = StringValueCStr(file_rb);
49
+ const int line = FIX2INT(line_rb);
50
+ const char *const msg = StringValueCStr(msg_rb);
51
+
52
+ kernaux_assert_do(file, line, msg);
53
+
54
+ return Qnil;
55
+ }
@@ -0,0 +1,83 @@
1
+ #include "main.h"
2
+
3
+ #ifdef KERNAUX_VERSION_WITH_CMDLINE
4
+
5
+ #define ARG_COUNT_MAX 256
6
+ #define BUFFER_SIZE 4096
7
+
8
+ struct Data {
9
+ char error_msg[KERNAUX_CMDLINE_ERROR_MSG_SIZE_MAX];
10
+ char *argv[ARG_COUNT_MAX];
11
+ char buffer[BUFFER_SIZE];
12
+ };
13
+
14
+ static VALUE rb_ANON_Data_ALLOC(VALUE klass);
15
+ static VALUE rb_KernAux_cmdline(VALUE self, VALUE cmdline);
16
+
17
+ static const struct rb_data_type_struct info = {
18
+ .wrap_struct_name = "cmdline",
19
+ .function = {
20
+ .dmark = NULL,
21
+ .dfree = RUBY_DEFAULT_FREE,
22
+ .dsize = NULL,
23
+ .dcompact = NULL,
24
+ .reserved = { 0 },
25
+ },
26
+ .parent = NULL,
27
+ .data = NULL,
28
+ .flags = RUBY_TYPED_FREE_IMMEDIATELY,
29
+ };
30
+
31
+ static VALUE rb_KernAux_CmdlineError = Qnil;
32
+ static VALUE rb_ANON_Data = Qnil;
33
+
34
+ void init_cmdline()
35
+ {
36
+ rb_gc_register_mark_object(rb_KernAux_CmdlineError =
37
+ rb_define_class_under(rb_KernAux, "CmdlineError", rb_KernAux_Error));
38
+ rb_gc_register_mark_object(rb_ANON_Data =
39
+ rb_funcall(rb_cClass, rb_intern_new, 1, rb_cObject));
40
+
41
+ rb_define_alloc_func(rb_ANON_Data, rb_ANON_Data_ALLOC);
42
+ rb_define_singleton_method(rb_KernAux, "cmdline", rb_KernAux_cmdline, 1);
43
+ }
44
+
45
+ VALUE rb_ANON_Data_ALLOC(const VALUE klass)
46
+ {
47
+ struct Data *data;
48
+ return TypedData_Make_Struct(klass, struct Data, &info, data);
49
+ }
50
+
51
+ VALUE rb_KernAux_cmdline(const VALUE self_rb, VALUE cmdline_rb)
52
+ {
53
+ const char *const cmdline = StringValueCStr(cmdline_rb);
54
+ size_t argc;
55
+
56
+ const VALUE data_rb = rb_funcall(rb_ANON_Data, rb_intern_new, 0);
57
+ struct Data *data;
58
+ TypedData_Get_Struct(data_rb, struct Data, &info, data);
59
+ if (!data) rb_raise(rb_KernAux_CmdlineError, "internal error");
60
+
61
+ const bool result = kernaux_cmdline(
62
+ cmdline,
63
+ data->error_msg,
64
+ &argc,
65
+ data->argv,
66
+ data->buffer,
67
+ ARG_COUNT_MAX,
68
+ BUFFER_SIZE
69
+ );
70
+
71
+ if (!result) rb_raise(rb_KernAux_CmdlineError, "%s", data->error_msg);
72
+
73
+ VALUE result_rb = rb_ary_new2(argc);
74
+ for (size_t index = 0; index < argc; ++index) {
75
+ rb_ary_push(
76
+ result_rb,
77
+ rb_funcall(rb_str_new2(data->argv[index]), rb_intern_freeze, 0)
78
+ );
79
+ }
80
+ return rb_funcall(result_rb, rb_intern_freeze, 0);
81
+ }
82
+
83
+ #endif // KERNAUX_VERSION_WITH_CMDLINE
@@ -0,0 +1,45 @@
1
+ #include "dynarg.h"
2
+
3
+ struct DynArg DynArg_create()
4
+ {
5
+ struct DynArg dynarg;
6
+ DynArg_init(&dynarg);
7
+ return dynarg;
8
+ }
9
+
10
+ void DynArg_init(struct DynArg *const dynarg)
11
+ {
12
+ dynarg->use_dbl = false;
13
+ dynarg->dbl = 0.0;
14
+ dynarg->arg.str = "";
15
+ }
16
+
17
+ void DynArg_use_char(struct DynArg *const dynarg, const char chr)
18
+ {
19
+ dynarg->use_dbl = false;
20
+ dynarg->arg.chr = chr;
21
+ }
22
+
23
+ void DynArg_use_double(struct DynArg *const dynarg, const double dbl)
24
+ {
25
+ dynarg->use_dbl = true;
26
+ dynarg->dbl = dbl;
27
+ }
28
+
29
+ void DynArg_use_long_long(struct DynArg *const dynarg, const long long ll)
30
+ {
31
+ dynarg->use_dbl = false;
32
+ dynarg->arg.ll = ll;
33
+ }
34
+
35
+ void DynArg_use_str(struct DynArg *const dynarg, const char *const str)
36
+ {
37
+ dynarg->use_dbl = false;
38
+ dynarg->arg.str = str;
39
+ }
40
+
41
+ void DynArg_use_unsigned_long_long(struct DynArg *const dynarg, const unsigned long long ull)
42
+ {
43
+ dynarg->use_dbl = false;
44
+ dynarg->arg.ull = ull;
45
+ }
@@ -0,0 +1,35 @@
1
+ #ifndef INCLUDED_DYNARG
2
+ #define INCLUDED_DYNARG
3
+
4
+ #ifdef __cplusplus
5
+ extern "C" {
6
+ #endif
7
+
8
+ #include <stdbool.h>
9
+
10
+ struct DynArg {
11
+ bool use_dbl;
12
+ double dbl;
13
+ // TODO: check if this will work on different endianness.
14
+ union {
15
+ char chr;
16
+ long long ll;
17
+ const char *str;
18
+ unsigned long long ull;
19
+ } __attribute__((packed)) arg;
20
+ };
21
+
22
+ struct DynArg DynArg_create();
23
+ void DynArg_init(struct DynArg *dynarg);
24
+
25
+ void DynArg_use_char(struct DynArg *dynarg, char chr);
26
+ void DynArg_use_double(struct DynArg *dynarg, double dbl);
27
+ void DynArg_use_long_long(struct DynArg *dynarg, long long ll);
28
+ void DynArg_use_str(struct DynArg *dynarg, const char *str);
29
+ void DynArg_use_unsigned_long_long(struct DynArg *dynarg, unsigned long long ull);
30
+
31
+ #ifdef __cplusplus
32
+ }
33
+ #endif
34
+
35
+ #endif
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'mkmf'
4
+
5
+ $CFLAGS += ' -pedantic -Wall -Wextra'
6
+
7
+ raise 'libkernaux not found' unless have_library 'kernaux'
8
+
9
+ raise 'kernaux_assert_do not found' unless have_func 'kernaux_assert_do'
10
+ unless have_var 'kernaux_assert_cb', 'kernaux.h'
11
+ raise 'kernaux_assert_cb not found'
12
+ end
13
+
14
+ raise 'can\'t create Makefile' unless create_makefile 'kernaux/default'
@@ -0,0 +1,34 @@
1
+ #include "main.h"
2
+
3
+ ID rb_intern_call = Qnil;
4
+ ID rb_intern_freeze = Qnil;
5
+ ID rb_intern_LESS = Qnil;
6
+ ID rb_intern_new = Qnil;
7
+
8
+ VALUE rb_KernAux = Qnil;
9
+ VALUE rb_KernAux_Error = Qnil;
10
+
11
+ void Init_default()
12
+ {
13
+ rb_gc_register_mark_object(ID2SYM(rb_intern_call = rb_intern("call")));
14
+ rb_gc_register_mark_object(ID2SYM(rb_intern_freeze = rb_intern("freeze")));
15
+ rb_gc_register_mark_object(ID2SYM(rb_intern_LESS = rb_intern("<")));
16
+ rb_gc_register_mark_object(ID2SYM(rb_intern_new = rb_intern("new")));
17
+
18
+ rb_gc_register_mark_object(rb_KernAux = rb_define_module("KernAux"));
19
+ rb_gc_register_mark_object(rb_KernAux_Error =
20
+ rb_define_class_under(rb_KernAux, "Error", rb_eRuntimeError));
21
+
22
+ init_version();
23
+ init_assert();
24
+
25
+ #ifdef KERNAUX_VERSION_WITH_CMDLINE
26
+ init_cmdline();
27
+ #endif // KERNAUX_VERSION_WITH_CMDLINE
28
+ #ifdef KERNAUX_VERSION_WITH_NTOA
29
+ init_ntoa();
30
+ #endif // KERNAUX_VERSION_WITH_NTOA
31
+ #ifdef KERNAUX_VERSION_WITH_PRINTF
32
+ init_printf();
33
+ #endif // KERNAUX_VERSION_WITH_PRINTF
34
+ }
@@ -0,0 +1,28 @@
1
+ #ifndef __MAIN_H__
2
+ #define __MAIN_H__
3
+
4
+ #include <kernaux.h>
5
+ #include <ruby.h>
6
+
7
+ extern ID rb_intern_call;
8
+ extern ID rb_intern_freeze;
9
+ extern ID rb_intern_LESS;
10
+ extern ID rb_intern_new;
11
+
12
+ extern VALUE rb_KernAux;
13
+ extern VALUE rb_KernAux_Error;
14
+
15
+ void init_version();
16
+ void init_assert();
17
+
18
+ #ifdef KERNAUX_VERSION_WITH_CMDLINE
19
+ void init_cmdline();
20
+ #endif // KERNAUX_VERSION_WITH_CMDLINE
21
+ #ifdef KERNAUX_VERSION_WITH_NTOA
22
+ void init_ntoa();
23
+ #endif // KERNAUX_VERSION_WITH_NTOA
24
+ #ifdef KERNAUX_VERSION_WITH_PRINTF
25
+ void init_printf();
26
+ #endif // KERNAUX_VERSION_WITH_PRINTF
27
+
28
+ #endif