"Fossies" - the Fresh Open Source Software Archive

Member "RPerl-5.002000/lib/RPerl/Operation/Expression/Operator/Named/Keys.cpp" (29 Nov 2019, 9374 Bytes) of package /linux/misc/RPerl-5.002000.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Keys.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 5.000000_vs_5.002000.

    1 using std::cout;  using std::cerr;  using std::endl;  using std::to_string;
    2 
    3 #ifndef __CPP__INCLUDED__RPerl__Operation__Expression__Operator__Named__Keys_cpp
    4 #define __CPP__INCLUDED__RPerl__Operation__Expression__Operator__Named__Keys_cpp 0.002_000
    5 
    6 #include <RPerl/Operation/Expression/Operator/Named/Keys.h>  // -> ???
    7 
    8 // [[[ STRINGIFY ]]]
    9 // [[[ STRINGIFY ]]]
   10 // [[[ STRINGIFY ]]]
   11 
   12 # ifdef __PERL__TYPES
   13 
   14 // short-hand, type-independent; call actual keys routine, after determining input data type
   15 SV* keys(SV* input_hvref) {
   16     // NEED ADD CODE
   17     // will require extending or modifying *_CHECK*() functions to detect positive instead of negative only, then use to detect data type
   18     // can also be used for other type-independent operators such as stringify() and type-reporting
   19 }
   20 
   21 // convert from (Perl SV containing RV to (Perl HV of (Perl SV containing RV to (Perl AV of (Perl SVs containing IVs))))) to (Perl SV containing RV to (Perl AV of (Perl SVs containing PVs)))
   22 SV* integer_arrayref_hashref_keys(SV* input_avref_hvref) {
   23 //    fprintf(stderr, "in CPPOPS_PERLTYPES integer_arrayref_hashref_keys(), top of subroutine...\n");
   24 //    fprintf(stderr, "in CPPOPS_PERLTYPES integer_arrayref_hashref_keys(), received format_level = %"INTEGER", indent_level = %"INTEGER"\n", SvIV(format_level), SvIV(indent_level));
   25 
   26 //  integer_arrayref_hashref_CHECK(input_avref_hvref);
   27     integer_arrayref_hashref_CHECKTRACE(input_avref_hvref, "input_avref_hvref", "integer_arrayref_hashref_to_string()");
   28 
   29 /*
   30  * START HERE, CONVERT FROM STRINGIFY TO KEYS
   31  * START HERE, CONVERT FROM STRINGIFY TO KEYS
   32  * START HERE, CONVERT FROM STRINGIFY TO KEYS
   33 
   34     // declare local variables
   35     HV* input_avref_hv;
   36     integer input_avref_hv_num_keys;
   37     integer i;
   38     boolean i_is_0 = 1;
   39     HE* input_avref_hv_entry;
   40     SV* input_avref_hv_entry_key;
   41     string input_avref_hv_entry_key_string;
   42     SV* input_avref_hv_entry_value;
   43     SV* output_sv = newSVpv("", 0);
   44 
   45     // generate indent
   46     SV* indent = newSVpv("", 0);
   47     for (i = 0; i < SvIV(indent_level); i++) { sv_catpvn(indent, "    ", 4); }
   48 
   49     // compute length of (number of keys in) input hash
   50     input_avref_hv = (HV*)SvRV(input_avref_hvref);
   51     input_avref_hv_num_keys = hv_iterinit(input_avref_hv);
   52 //  fprintf(stderr, "in CPPOPS_PERLTYPES integer_arrayref_hashref_keys(), have input_avref_hv_num_keys = %"INTEGER"\n", input_avref_hv_num_keys);
   53 
   54     // pre-begin with optional indent, depending on format level
   55     if (SvIV(format_level) >= 1) { sv_catsv(output_sv, indent); }  // pretty
   56 
   57     // begin output string with left-curly-brace, as required for all RPerl hashes
   58     sv_setpvn(output_sv, "{", 1);
   59 
   60     // loop through all hash keys
   61     // incrementing iteration, iterator i not actually used in loop body
   62     for (i = 0;  i < input_avref_hv_num_keys;  ++i) {
   63 //      fprintf(stderr, "in CPPOPS_PERLTYPES integer_arrayref_hashref_keys(), top of loop i = %"INTEGER"\n", i);
   64 
   65         // does not utilize i in entry retrieval
   66         input_avref_hv_entry = hv_iternext(input_avref_hv);
   67 
   68         // DEV NOTE: hash entry type-checking already done as part of integer_arrayref_hashref_CHECKTRACE()
   69 //      hashentry_CHECK(input_avref_hv_entry);
   70 //      hashentry_CHECKTRACE(input_avref_hv_entry, "input_avref_hv_entry", "integer_arrayref_hashref_keys()");
   71 
   72         // retrieve input hash's entry value at key
   73         input_avref_hv_entry_key = hv_iterkeysv(input_avref_hv_entry);
   74         input_avref_hv_entry_value = hv_iterval(input_avref_hv, input_avref_hv_entry);
   75 
   76         // DEV NOTE: integer type-checking already done as part of integer_arrayref_hashref_CHECKTRACE()
   77 //      integer_CHECK(input_avref_hv_entry_value);
   78 //      integer_CHECKTRACE(input_avref_hv_entry_value, (char*)((string)"input_avref_hv_entry_value at key '" + (string)SvPV_nolen(input_avref_hv_entry_key) + "'").c_str(), "integer_arrayref_hashref_keys()");
   79 
   80         // append comma to output string for all elements except index 0
   81         if (i_is_0) { i_is_0 = 0; }
   82         else        { sv_catpvn(output_sv, ",", 1); }
   83 
   84         // append newline-indent-tab or space, depending on format level
   85         if      (SvIV(format_level) >= 1) { sv_catpvn(output_sv, "\n", 1);  sv_catsv(output_sv, indent);  sv_catpvn(output_sv, "    ", 4); }  // pretty & expand
   86         else if (SvIV(format_level) >= 0) { sv_catpvn(output_sv, " ", 1); }                                                                   // normal
   87 
   88         input_avref_hv_entry_key_string = escape_backslash_singlequote(string(SvPV_nolen(input_avref_hv_entry_key)));  // escape key string for error message
   89 
   90         // DEV NOTE: emulate Data::Dumper & follow PBP by using single quotes for key strings
   91 //        sv_catpvf(output_sv, "'%s'", SvPV_nolen(input_avref_hv_entry_key));  // alternative form
   92         sv_catpvf(output_sv, "'%s'", input_avref_hv_entry_key_string.c_str());
   93 
   94         // append spaces before and after fat arrow AKA fat comma, depending on format level
   95         if      (SvIV(format_level) >= 0) { sv_catpvn(output_sv, " => ", 4); }  // normal & pretty & expand
   96         else                               { sv_catpvn(output_sv, "=>", 2); }   // compact
   97 
   98         // append newline after fat arrow AKA fat comma, depending on format level
   99         if      (SvIV(format_level) >= 2) { sv_catpvn(output_sv, "\n", 1); }    // expand
  100 
  101         // call *_to_string_format() for data sub-structure
  102         sv_catsv(output_sv, integer_arrayref_to_string_format(input_avref_hv_entry_value, newSViv(SvIV(format_level) - 1), newSViv(SvIV(indent_level) + 1)));  // YES UNDERSCORES
  103     }
  104 
  105     // append newline-indent or space, depending on format level
  106     if      (SvIV(format_level) >= 1) { sv_catpvn(output_sv, "\n", 1);  sv_catsv(output_sv, indent); }  // pretty & expand
  107     else if (SvIV(format_level) >= 0) { sv_catpvn(output_sv, " ", 1); }                                 // normal
  108 
  109     // end output string with right-curly-brace, as required for all RPerl hashes
  110     sv_catpvn(output_sv, "}", 1);
  111 
  112 //  fprintf(stderr, "in CPPOPS_PERLTYPES integer_arrayref_hashref_keys(), after for() loop, have output_sv =\n%s\n", SvPV_nolen(output_sv));
  113 //  fprintf(stderr, "in CPPOPS_PERLTYPES integer_arrayref_hashref_keys(), bottom of subroutine\n");
  114 
  115     return(output_sv);
  116 */
  117 }
  118 
  119 # elif defined __CPP__TYPES
  120 
  121 template<typename HASH_VALUE_TYPE>
  122 void print_keys_values(std::unordered_map<string, HASH_VALUE_TYPE> const &input_hash)
  123 {
  124     for (auto iterator = input_hash.cbegin(); iterator != input_hash.cend(); ++iterator) {
  125         std::cout << "key: " << (*iterator).first << "\n" << "value: " << (*iterator).second << "\n\n";
  126     }
  127 }
  128 
  129 // convert from (C++ std::unordered_map of (C++ std::vector of integers)) to (C++ std::vector of strings)
  130 template<typename HASH_VALUE_TYPE>
  131 string_arrayref keys(std::unordered_map<string, HASH_VALUE_TYPE> const &input_unordered_map) {
  132 //    fprintf(stderr, "in CPPOPS_CPPTYPES keys(), top of subroutine\n");
  133 
  134     // declare local variables
  135     string_arrayref output_keys;
  136 
  137     // loop through all hash keys
  138     for (auto iterator = input_unordered_map.begin();  iterator != input_unordered_map.end();  ++iterator) {
  139         // append key string to output vector
  140         output_keys.push_back(iterator->first);
  141     }
  142 
  143 //    fprintf(stderr, "in CPPOPS_CPPTYPES keys(), after for() loop, have output_keys =\n%s\n", (char*)(string_arrayref_to_string(output_keys).c_str()));
  144 //    fprintf(stderr, "in CPPOPS_CPPTYPES keys(), bottom of subroutine\n");
  145 
  146     return(output_keys);
  147 }
  148 
  149 
  150 /* NEED DELETE, TYPE-SPECIFIC CODE REPLACED BY TYPE-INDEPENDENT TEMPLATE CODE
  151 
  152 // short-hand, type-dependent; call actual keys routine
  153 string_arrayref keys(integer_arrayref_hashref input_vector_unordered_map) {
  154     return integer_arrayref_hashref_keys(input_vector_unordered_map);
  155 }
  156 
  157 // convert from (C++ std::unordered_map of (C++ std::vector of integers)) to (C++ std::vector of strings)
  158 string_arrayref integer_arrayref_hashref_keys(integer_arrayref_hashref input_vector_unordered_map) {
  159     fprintf(stderr, "in CPPOPS_CPPTYPES integer_arrayref_hashref_keys(), top of subroutine\n");
  160     fprintf(stderr, "in CPPOPS_CPPTYPES integer_arrayref_hashref_keys(), received format_level = %"INTEGER", indent_level = %"INTEGER"\n", format_level, indent_level);
  161 
  162     // declare local variables
  163     string_arrayref output_keys;
  164     integer_arrayref_hashref_const_iterator i;
  165     string key_string;
  166 
  167     // loop through all hash keys
  168     for (i = input_vector_unordered_map.begin();  i != input_vector_unordered_map.end();  ++i) {
  169 
  170         // escape key string
  171         key_string = escape_backslash_singlequote(i->first);
  172 
  173         // DEV NOTE: emulate Data::Dumper & follow PBP by using single quotes for key strings
  174         key_string = (const string)"'" + key_string << (const string)"'";
  175 
  176         // append key string to output vector
  177         output_keys.push_back(key_string);
  178     }
  179 
  180     fprintf(stderr, "in CPPOPS_CPPTYPES integer_arrayref_hashref_keys(), after for() loop, have output_keys =\n%s\n", (char*)(integer_arrayref_hashref_to_string(output_keys).c_str()));
  181     fprintf(stderr, "in CPPOPS_CPPTYPES integer_arrayref_hashref_keys(), bottom of subroutine\n");
  182 
  183     return(output_keys);
  184 }
  185 */
  186 
  187 # else
  188 
  189 Purposefully_die_from_a_compile-time_error,_due_to_neither___PERL__TYPES_nor___CPP__TYPES_being_defined.__We_need_to_define_exactly_one!
  190 
  191 # endif
  192 
  193 #endif