Converting C (Having Pointers) Code To Java

I have to convert the following function from C to Java...But I am not able to as Java Does not support pointers. Can any one help

[code]
void find_substituted_text (char* phrase, char* substitute, char* original,
char** phrase_start, char** phrase_end)
{
DEBUG_OUT(5, ("find_substitute_text('%s', '%s', '%s', %p, %p)
",
phrase, substitute, original,
phrase_start, phrase_end));

if (strcmp(phrase, substitute) == 0) {
char* end;
char* pos = find_phrase(original, &end);
if (pos && ((!*phrase_start) || (pos < *phrase_start))) {
DEBUG_OUT(3, ("Overriding %s resolution w/ %s (coref line %d)
",
substitute, original, line));
*phrase_start = pos;
*phrase_end = end;
}
}
}
[/code]

[code]
int merge_text (char* target_text, char* output_text, int issue_warning)
{
DEBUG_OUT(4, ("merge_text('%s', '%s', %d)
",
target_text, output_text, issue_warning));
int ok = FALSE;

// Find the corresponding text in the original file
// TODO: skip the search if the entity_id is not needed (eg, verb entry)
char* end = NULL;
char* place_in_text = find_phrase(target_text, &end);

#if TRUE
// Do special-case corrections for transliterated phrase
// Ex: "pof" for "'s" or "s'"; ";" for "--";
find_substituted_text(target_text, ";", "--", &place_in_text, &end);
find_substituted_text(target_text, "pof", "'s", &place_in_text, &end);
find_substituted_text(target_text, "pof", "s'", &place_in_text, &end);
find_substituted_text(target_text, "pof", "'", &place_in_text, &end);
#endif

if (!place_in_text) {
if (issue_warning) {
error_out("Warning: unable to find '%s' in original text
",
target_text);
}
}
else if ((jump_size(end) - num_words(target_text)) > max_word_skip) {
if (issue_warning) {
error_out("Warning: next occurrence of '%s' is beyond %d-word threshword
",
target_text, max_word_skip);
}
}
else {
ok = TRUE;

// Print the text preceding the entity in the original text
// and update the current position.
print_original_text(place_in_text);

// Print the transformed text
printf("%s", output_text);

// Skip past the corresponding text in the original file
// NOTE: only advance past the target if it is the same as
// the output text.
if (strcmp(target_text, output_text) != 0) {
// Be careful since the target might be larger than original
// due to extraneous spaces.
end = max(original_text_pos, end - strlen(target_text));
ASSERT(end >= original_text_pos);
}
skip_entity_text(place_in_text, end);
}

return (ok);
}
[/code]

[code]
char* find_phrase (char* text, char** end)
{
DEBUG_OUT(4, ("find_phrase('%s', %p)
", text, end));
int text_len = strlen(text);
assert(text_len > 0);
assert(end);
char* text_pos = NULL;

// Do a brute force case-insensitive scan for the text
// TODO: use a more efficient search technique (eg, boyer/moore)
int found = FALSE;
char* start = original_text_pos;
do {
found = TRUE;
#if FALSE
start = find_word_start(start);
#endif
int tag_offset = 0;
int text_offset = 0;
for (int i = 0; (i + text_offset) < text_len; i++) {
// Skip over all but the last of a run of whitespace characters
// in order to treat whitespace as one character to be matched.
skip_excess_spacing(&start[i], tag_offset);
skip_excess_spacing(&text[i], text_offset);

// See if the current characters match
if (!equiv_chars(start[i + tag_offset], text[i + text_offset])) {
found = FALSE;
break;
}
}
if (found) {
text_pos = start;
*end = start + (text_len - text_offset) + tag_offset;
break;
}
#if FALSE
start = find_word_end(start);
#endif
start++; // just try next character
} while (*start);

return (text_pos);
}
[/code]

All the required header files had been included TRUE is 1 and FALSE is 0. It is working on c but I have to convert it to java.

Comments

  • : I have to convert the following function from C to Java...But I am not able to as Java Does not support pointers. Can any one help
    :
    : [code]
    : void find_substituted_text (char* phrase, char* substitute, char* original,
    : char** phrase_start, char** phrase_end)
    : {
    : DEBUG_OUT(5, ("find_substitute_text('%s', '%s', '%s', %p, %p)
    ",
    : phrase, substitute, original,
    : phrase_start, phrase_end));
    :
    : if (strcmp(phrase, substitute) == 0) {
    : char* end;
    : char* pos = find_phrase(original, &end);
    : if (pos && ((!*phrase_start) || (pos < *phrase_start))) {
    : DEBUG_OUT(3, ("Overriding %s resolution w/ %s (coref line %d)
    ",
    : substitute, original, line));
    : *phrase_start = pos;
    : *phrase_end = end;
    : }
    : }
    : }
    : [/code]
    :
    : [code]
    : int merge_text (char* target_text, char* output_text, int issue_warning)
    : {
    : DEBUG_OUT(4, ("merge_text('%s', '%s', %d)
    ",
    : target_text, output_text, issue_warning));
    : int ok = FALSE;
    :
    : // Find the corresponding text in the original file
    : // TODO: skip the search if the entity_id is not needed (eg, verb entry)
    : char* end = NULL;
    : char* place_in_text = find_phrase(target_text, &end);
    :
    : #if TRUE
    : // Do special-case corrections for transliterated phrase
    : // Ex: "pof" for "'s" or "s'"; ";" for "--";
    : find_substituted_text(target_text, ";", "--", &place_in_text, &end);
    : find_substituted_text(target_text, "pof", "'s", &place_in_text, &end);
    : find_substituted_text(target_text, "pof", "s'", &place_in_text, &end);
    : find_substituted_text(target_text, "pof", "'", &place_in_text, &end);
    : #endif
    :
    : if (!place_in_text) {
    : if (issue_warning) {
    : error_out("Warning: unable to find '%s' in original text
    ",
    : target_text);
    : }
    : }
    : else if ((jump_size(end) - num_words(target_text)) > max_word_skip) {
    : if (issue_warning) {
    : error_out("Warning: next occurrence of '%s' is beyond %d-word threshword
    ",
    : target_text, max_word_skip);
    : }
    : }
    : else {
    : ok = TRUE;
    :
    : // Print the text preceding the entity in the original text
    : // and update the current position.
    : print_original_text(place_in_text);
    :
    : // Print the transformed text
    : printf("%s", output_text);
    :
    : // Skip past the corresponding text in the original file
    : // NOTE: only advance past the target if it is the same as
    : // the output text.
    : if (strcmp(target_text, output_text) != 0) {
    : // Be careful since the target might be larger than original
    : // due to extraneous spaces.
    : end = max(original_text_pos, end - strlen(target_text));
    : ASSERT(end >= original_text_pos);
    : }
    : skip_entity_text(place_in_text, end);
    : }
    :
    : return (ok);
    : }
    : [/code]
    :
    : [code]
    : char* find_phrase (char* text, char** end)
    : {
    : DEBUG_OUT(4, ("find_phrase('%s', %p)
    ", text, end));
    : int text_len = strlen(text);
    : assert(text_len > 0);
    : assert(end);
    : char* text_pos = NULL;
    :
    : // Do a brute force case-insensitive scan for the text
    : // TODO: use a more efficient search technique (eg, boyer/moore)
    : int found = FALSE;
    : char* start = original_text_pos;
    : do {
    : found = TRUE;
    : #if FALSE
    : start = find_word_start(start);
    : #endif
    : int tag_offset = 0;
    : int text_offset = 0;
    : for (int i = 0; (i + text_offset) < text_len; i++) {
    : // Skip over all but the last of a run of whitespace characters
    : // in order to treat whitespace as one character to be matched.
    : skip_excess_spacing(&start[i], tag_offset);
    : skip_excess_spacing(&text[i], text_offset);
    :
    : // See if the current characters match
    : if (!equiv_chars(start[i + tag_offset], text[i + text_offset])) {
    : found = FALSE;
    : break;
    : }
    : }
    : if (found) {
    : text_pos = start;
    : *end = start + (text_len - text_offset) + tag_offset;
    : break;
    : }
    : #if FALSE
    : start = find_word_end(start);
    : #endif
    : start++; // just try next character
    : } while (*start);
    :
    : return (text_pos);
    : }
    : [/code]
    :
    : All the required header files had been included TRUE is 1 and FALSE is 0. It is working on c but I have to convert it to java.
    :
    I don't know C, but to convert this code to Java, you need to change the pointers into integer indexes to characters in the string. The pointers representing strings should be changed to String objects. Since objects are passed by reference and strings are objects in Java, the code will run in similar ways.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion