/************************************************************* * * This is an implementation of a predicate in C++. * Multiple values are returned in a pipelined fashion. * This function, given a string, returns each character and * its position at a time. On each success, LDL_SUCCESS is * returned. LDL_FAIL is returned upon a failure. A local * state variable, State, maintains the state of the * computation between invocations. * * Note: This function is re-entrant, as all LDL++ externals * ought to be. * **************************************************************/ #include #include #include #include // Make sure that the name is not mangled extern "C" { LdlStatus all_letters(LdlObject str, LdlObject ch, LdlObject pos); } // State variables typedef struct { int position; // Current position int length; // Length of string } State; LdlStatus all_letters(LdlObject str, LdlObject ch, LdlObject pos) { State* state; LdlStatus status; char ret_string[2]; char* c_string; if (ldl_entry_p()) { // This is the first time around // Create the state structure state = (State *)ldl_create_state(sizeof(State)); // Initialize the position state->position = 0; if (c_string = ldl_get_string(str)) state->length = strlen(c_string); else state->length = 0; } // Recover the state from the LDL++ system state = (State *)ldl_get_state(); if ((c_string = ldl_get_string(str)) && (state->position < state->length)) { LdlObject temp_string; LdlObject temp_int; ret_string[0] = c_string[state->position]; ret_string[1] = '\0'; temp_string = ldl_create_string(ret_string); temp_int = ldl_create_int(state->position); ldl_assign_argument(ch, temp_string); ldl_assign_argument(pos, temp_int); state->position++; ldl_delete_object(temp_string); ldl_delete_object(temp_int); status = LDL_SUCCESS; } else status = LDL_FAIL; return(status); }