0

I have been trying to make a function in C++ for a gameserver DLL which aligns given text to center before returning the new string to Lua for processing. I have spent quite a lot of time looking at examples on various sites but have only been able to find 'cout', which prints it in the console application, which I do not want it to do.

I'm new to C++, and I'm really confused on how to approach this. If someone would be able to provide an example and explain how it works, I'll be able to learn how to do this for the future.

Basically, it does this:

  1. Forward our string from Lua to C++.
  2. C++ centers the string that we just forwarded.
  3. Return the completed string back to Lua.

Here's a sample of what I've been trying to do:

int CScriptBind_GameRules::CentreTextForConsole(IFunctionHandler *pH, const char *input)
{
    if (input)
    {
        int l=strlen(input);
        int pos=(int)((113-l)/2);
        for(int i=0;i<pos;i++)
            std::cout<<" ";
        std::cout<<input;
        return pH->EndFunction(input); 
    }
    else
    {
        CryLog("[System] Error in CScriptBind_GameRules::CentreTextForConsole: Failed to align");
        return pH->EndFunction();
    }
    return pH->EndFunction();
}

Which builds but it prints the text to the console, not forwarding back the completed string.

AStopher
  • 4,207
  • 11
  • 50
  • 75

3 Answers3

3

I'm going to assume you already know how to pass a string from Lua to C++ and return the result from C++ to Lua, so the only part we need to deal with is producing the centered string.

That, however, is pretty easy:

std::string center(std::string input, int width = 113) { 
    return std::string((width - input.length()) / 2, ' ') + input;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

Here's another way that will ensure the text is centered within a given width and is padded left and right with spaces.

std::string center(const std::string s, const int w) {
    std::stringstream ss, spaces;
    int pad = w - s.size();                  // count excess room to pad
    for(int i=0; i<pad/2; ++i)
        spaces << " ";
    ss << spaces.str() << s << spaces.str(); // format with padding
    if(pad>0 && pad%2!=0)                    // if pad odd #, add 1 more space
        ss << " ";
    return ss.str();
}

This could be written more elgantly or concisely.

synaptik
  • 8,971
  • 16
  • 71
  • 98
1
std::string center (const std::string& s, unsigned width)
{
    assert (width > 0);
    if (int padding = width - s.size (), pad = padding >> 1; pad > 0)
        return std::string (padding, ' ').insert (pad, s);
    return s;
}
  • 3
    Hi and welcome to SO! Could you please post an explanation for your answer? – party-ring Aug 21 '19 at 15:07
  • 1
    Welcome to Stack Overflow! Thank you for this code snippet, which might provide some limited short-term help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its long-term value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Toby Speight Aug 21 '19 at 16:19