File: argcargv.i

package info (click to toggle)
swig 4.3.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 46,232 kB
  • sloc: cpp: 54,631; ansic: 29,122; java: 17,530; python: 12,505; cs: 10,369; ruby: 7,232; yacc: 6,477; makefile: 5,965; javascript: 5,520; sh: 5,415; perl: 4,187; php: 3,693; ml: 2,187; lisp: 2,056; tcl: 1,991; xml: 115
file content (66 lines) | stat: -rw-r--r-- 1,709 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/* ------------------------------------------------------------
 * SWIG library containing argc and argv multi-argument typemaps
 * ------------------------------------------------------------ */

%{
SWIGINTERN int SWIG_AsVal_string (Napi::Value valRef, Napi::String *str)
{
  if (!valRef.IsString()) {
    return SWIG_TypeError;
  }
  if(str != SWIG_NULLPTR) {
      *str = valRef.ToString();
  }
  return SWIG_OK;
}
%}

%typemap(in) (int ARGC, char **ARGV) {
  $1_ltype i, len;
  size_t arraysize;
  Napi::Array array;
  if (!$input.IsArray()) {
    SWIG_exception_fail(SWIG_ERROR, "not array");
  }
  array = $input.As<Napi::Array>();
  len = array.Length();
  arraysize = (len+1)*sizeof($*2_ltype);
  $1 = len;
  $2 = ($2_ltype) malloc(arraysize);
  if ($2 == SWIG_NULLPTR) {
    SWIG_exception_fail(SWIG_ERROR, "memory allocation of array failed");
  }
  memset($2, 0, arraysize);
  for (i = 0; i < len; i++) {
    int res, slen;
    $*2_ltype pstr;
    Napi::String napi_str;
    Napi::Value jsvalue = array.Get(i);
    res = SWIG_AsVal_string(jsvalue, &napi_str);
    if (!SWIG_IsOK(res)) {
      SWIG_exception_fail(SWIG_ERROR, "failed to convert to string");
    }
    std::string str = napi_str.Utf8Value();
    slen = str.size();
    pstr = ($*2_ltype) malloc(slen + 1);
    if (pstr == SWIG_NULLPTR) {
      SWIG_exception_fail(SWIG_ERROR, "memory allocation of a string failed");
    }
    if (slen) {
      memcpy(pstr, str.c_str(), slen);
    }
    pstr[slen] = 0;
    $2[i] = pstr;
  }
  $2[i] = SWIG_NULLPTR;
}

%typemap(freearg) (int ARGC, char **ARGV) {
  if ($2 != SWIG_NULLPTR) {
    $1_ltype i;
    for (i = 0; i < $1; i++) {
      free((void *)$2[i]);
    }
    free((void *)$2);
  }
}