File: GB_op_name_and_defn.c

package info (click to toggle)
suitesparse-graphblas 7.4.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 67,112 kB
  • sloc: ansic: 1,072,243; cpp: 8,081; sh: 512; makefile: 506; asm: 369; python: 125; awk: 10
file content (107 lines) | stat: -rw-r--r-- 3,862 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
//------------------------------------------------------------------------------
// GB_op_name_and_defn: get the name and defn of a unary, binary, or selectop
//------------------------------------------------------------------------------

// SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2022, All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

//------------------------------------------------------------------------------

#include "GB.h"
#include <ctype.h>

GrB_Info GB_op_name_and_defn
(
    // output
    char *operator_name,        // op->name of the GrB operator struct
    char **operator_defn,       // op->defn of the GrB operator struct
    size_t *operator_defn_size, // op->defn_size of the GrB operator struct
    // input
    const char *input_name,     // user-provided name, may be NULL
    const char *input_defn,     // user-provided name, may be NULL
    const char *typecast_name,  // typecast name for function pointer
    size_t typecast_name_len    // length of typecast_name
)
{

    //--------------------------------------------------------------------------
    // check inputs
    //--------------------------------------------------------------------------

    ASSERT (operator_name != NULL) ;
    ASSERT (operator_defn != NULL) ;
    ASSERT (typecast_name != NULL) ;
    ASSERT (operator_defn_size != NULL) ;
    (*operator_defn) = NULL ;
    (*operator_defn_size) = 0 ;

    //--------------------------------------------------------------------------
    // get the name of the operator
    //--------------------------------------------------------------------------

    memset (operator_name, 0, GxB_MAX_NAME_LEN) ;
    if (input_name != NULL)
    {
        // copy the input_name into the working name
        char working [GxB_MAX_NAME_LEN] ;
        memset (working, 0, GxB_MAX_NAME_LEN) ;
        strncpy (working, input_name, GxB_MAX_NAME_LEN-1) ;
        // see if the typecast appears in the input_name
        char *p = NULL ;
        p = strstr (working, typecast_name) ;
        if (p != NULL)
        { 
            // skip past the typecast, the left parenthesis, and any whitespace
            p += typecast_name_len ;
            while (isspace (*p)) p++ ;
            if (*p == ')') p++ ;
            while (isspace (*p)) p++ ;
            // p now contains the final name, copy it to the output name
            strncpy (operator_name, p, GxB_MAX_NAME_LEN-1) ;
        }
        else
        { 
            // no typcast appears; copy the entire operator_name as-is
            memcpy (operator_name, working, GxB_MAX_NAME_LEN) ;
        }
    }
    else
    { 
        // no operator_name, so give it a generic name
        snprintf (operator_name, GxB_MAX_NAME_LEN-1, "user_op") ;
    }
    // ensure operator_name is null-terminated
    operator_name [GxB_MAX_NAME_LEN-1] = '\0' ;

    //--------------------------------------------------------------------------
    // get the definition of the operator, if present
    //--------------------------------------------------------------------------

    char *defn = NULL ;
    size_t defn_size = 0 ;
    if (input_defn != NULL)
    { 
        // determine the string length of the definition
        size_t len = strlen (input_defn) ;

        // allocate space for the definition
        defn = GB_MALLOC (len+1, char, &defn_size) ;
        if (defn == NULL)
        { 
            // out of memory
            return (GrB_OUT_OF_MEMORY) ;
        }

        // copy the defintion into the new operator
        memcpy (defn, input_defn, len+1) ;
    }

    //--------------------------------------------------------------------------
    // return result
    //--------------------------------------------------------------------------

    (*operator_defn) = defn ;
    (*operator_defn_size) = defn_size ;
    return (GrB_SUCCESS) ;
}