/// @addtogroup sefun
/// @{
/// @file strings
/// @brief string and parser related efuns
/// @author Gwenhwyvar
/// @version 0.1.0
/// @date 2016-01-27

// --------------------------------------------------------------------------
/// @brief atoi 
/// convert number string to int
/// @Param arg
/// @Returns 
// --------------------------------------------------------------------------
public int atoi(string arg)
{
    int ret;

    if(!arg || (sscanf(arg, "%d", ret) != 1))
        return 0;
    return ret;
}
// --------------------------------------------------------------------------
/// @brief itoa 
/// convert int to number string
/// @Param arg
/// @Returns 
// --------------------------------------------------------------------------
public string itoa(int arg)
{
    return sprintf("%d", arg);;
}
// --------------------------------------------------------------------------
/// @brief add_article 
/// add's article for given text
///
/// if there is an article present it will be replaced:
/// add_article("the sword", 0) -> "a sword"
/// @Param text
/// @Param flag - 0 (default): unbestimmt article
///               1          : definite article
/// @Returns string
// --------------------------------------------------------------------------
public string add_article(string text, int flag = 0)
{
    string ret = remove_article(text);

    if(!strlen(ret))
        return text;

    // definite article?
    if(flag)
        ret = "the " + ret;
    else
    {
        // otherwise depends on first letter
        switch(text[0])
        {
            case 'A': case 'E': case 'I': case 'O': case 'U':
            case 'a': case 'e': case 'i': case 'o': case 'u':
                ret = "an " + ret;
            default:
                ret = "a " + ret;
        }
    }
    return ret;
}
// --------------------------------------------------------------------------
/// @brief remove_article 
/// removes's article for given text
/// @Param arg
/// @Returns string
// --------------------------------------------------------------------------
public string remove_article(string text)
{
    string ret = lower_case(text);;

    if(!strlen(ret))
        return text;

    if(ret[0..3] == "the ")
        ret = text[4..];
    else if(ret[0..2] == "an ")
        ret = text[3..];
    else if(ret[0..1] == "a ")
        ret = text[2..]
    else
        ret = text;
    return ret;
}
// --------------------------------------------------------------------------
/// @brief iwrap 
/// return text wraped to width with every line except the first indented
/// @Param text
/// @Param width
/// @Param indent
/// @Returns 
// --------------------------------------------------------------------------
public string i_wrap(string text, int width = DFLT_SCR_WIDTH, int indent = DFLT_SCR_INDENT)
{
    return sprintf("%s%-=*s\n", text[0..(indent-1)], width, text[indent..]);
}
// --------------------------------------------------------------------------
/// @brief more 
///
/// prints a collection of lines, each not longer than screen width, given as
/// either an array of strings or as single string with '\n' embeded, in
/// chunks of maximal height lines, where the last line is reserved for a
/// continuation prompt
/// @Param arg    - string|string* lines to be printed
/// @Param height - int screen height
/// @Param input  - internal use only
/// @Returns 
// --------------------------------------------------------------------------
public void more(mixed arg, int height = DFLT_SCR_HEIGHT, string input = "")
{
    int sz;

    // quit?
    if(input == "q")
        return;

    // prepare/check argument
    if(stringp(arg))
        arg = explode(arg, "\n");
    if(!pointerp(arg))
        error(TYPE_ERROR("sefun::more", "1", typeof(arg), "string|string*"));

    // write at most sz lines
    sz = sizeof(arg);
    for(int i = 0; i < sz;)
    {
        write(arg[i++] + "\n");

        // maximum screen height reached (one line reserved for prompt)
        if(i == height - 1)
        {
            // show prompt
            write("'q' to quit, any other key to continue...");

            // cut of what is already written
            arg = arg[i..];
            // and ask once for a single character
            TP()->modal_push_char((: more, arg, height :), INPUT_AUTO_POP);
            return;
        }
    }
}
///  @}
