/**
 * Counts a lines in manifest file. Skips commented out and blank lines.
 *
 * @param buf
 * @return
 */
static int count_mf_lines(jchar* buf) {
    int numberOfLines = 0;

    if ((!buf) || (!*buf)) {
        return -1;
    }

    while (*buf) {
        /* skip commented out lines and lines that start with space */
        if ((COMMENTED_OUT(buf)) || (MF_SPACE(buf))) {
            /* run to the end of line */
            while (!NEW_LINE(buf)) {
                buf++;
            }
            /* skip all the new line characters an the end of line */
            while (NEW_LINE(buf)) {
                buf++;
            }
        } else {
            numberOfLines++;
            while (!NEW_LINE(buf)) {
                buf++;
            }
            while (NEW_LINE(buf)) {
                buf++;
            }
        }
    } /* end of while */
    return numberOfLines;
} /* end of count_mf_lines */
/**
 * Reads a line from a jad file.
 *
 * @param jadbuf pointer to the jad jchar_buffer; this pointer will move to
 *               the next line
 * @param result pointer to pcsl_string where the new line from jad file
 *               will be stored, the caller is responsible for freeing this
 *               string
 * @return error code
 */
static MIDPError readJadLine(jchar** jadbuf, pcsl_string * result) {

    jchar* lineStart = NULL;
    jchar* p = NULL;
    int count = 0;

    /* will use p to avoid all the *(*jadbuf) stuff and make it more readable */
    p = (*jadbuf);

    if (!(*p)) {
        /* end of jchar_buffer */
        *result = PCSL_STRING_NULL;
        return END_OF_JAD;
    }

    /* skip commented out and blank lines */
    while (COMMENTED_OUT(p) || NEW_LINE(p)) {
        while (!NEW_LINE(p)) {
            /* skip commented out line */
            p++;
        }
        while (NEW_LINE(p)) {
            /* skip new line */
            p++;
        }
    }

    lineStart = p;

    for (;*p ; ) {
        count++;
        p++;
        if (NEW_LINE(p)) {
            *p = 0x00; /* cut the line */
            if (*(p+1)) { /* if not end of jchar_buffer */
                p++; /* point to the next line beginning */
                break;
            } /* end of if */
        } /* end of if */
    } /* end of for */

    /* move jadbuf to point to the next line */
    (*jadbuf) = p;

    if (PCSL_STRING_OK !=
        pcsl_string_convert_from_utf16(lineStart, count, result)) {
        return OUT_OF_MEMORY;
    }

    return ALL_OK;
} /* end of readJadLine */
/**
 * Reads a line from a manifest file. Compacts continuation lines.<BR>
 * (Allocates memory for this line.)
 *
 * @param mfbuf  pointer to the manifest jchar_buffer; this pointer will move to
 *               the next line
 * @param result pointer to pcsl_string where the new line from manifest
 *               will be stored
 * @return error code
 */
static MIDPError readMfLine(jchar** mfbuf, pcsl_string * result) {
    jchar* lineStart = NULL;
    int is_broken_line = 0;
    jchar* p = NULL;
    int count = 0;
    int i = 0;

    *result = PCSL_STRING_NULL;

    /* will use p to avoid all the *(*mfbuf) stuff and make it more readable */
    p = (*mfbuf);

    if (!(*p)) {
        /* end of jchar_buffer */
        return END_OF_MF;
    }

    /* skip commented out and blank lines */
    while (COMMENTED_OUT(p) || NEW_LINE(p)) {

        while (!NEW_LINE(p)) {
            p++; /* skip commented out line */
        }
        while (NEW_LINE(p)) {
            p++; /* skip new line */
        }

        /* now pointing to the next line */
        if (MF_SPACE(p)) { /* if starting with space */
            while (!NEW_LINE(p)) {
                p++; /* skip the line */
            }
        } /* end of if */

    } /* end of while */

    lineStart = p;

    for (;*p ;) {
        count++;
        p++;
        if (NEW_LINE(p) && !MF_BROKEN_LINE(p)) {
            *p = 0x00; /* cut the line */
            if (*(p+1)) { /* if not end of the buffer */
                p++; /* point to the next line beginning */
                break;
            }
        } else if (MF_BROKEN_LINE(p)) {
            while (!MF_SPACE(p)) { /* look for the space */
                count++;
                p++;
            }
            /* once space found, point to the next character and go ahead */
            count++;
            p++;
            is_broken_line = 1;
            continue;
        } /* end of else */

    } /* end of for */

    /* move mfbuf to point to the next line */
    (*mfbuf) = p;

    pcsl_string_predict_size(result, count);
    if (is_broken_line) {
        i = 0;
        while (*(lineStart+i)) {

            /* here once we have a new line it will be followed by space */
            if (NEW_LINE_1(lineStart+i)) {
                i+=3;
                count-=3;
            } else if (NEW_LINE(lineStart+i)) {
                i+=2;
                count-=2;
            }

            if (PCSL_STRING_OK !=
                pcsl_string_append_char(result, lineStart[i])) {
                pcsl_string_free(result);
                return OUT_OF_MEMORY;
            }
            i++;
        }
    } else {
        if (PCSL_STRING_OK !=
            pcsl_string_convert_from_utf16(lineStart, count, result)) {
            return OUT_OF_MEMORY;
        }
    }

    return ALL_OK;
} /* end of readMfLine */