Re: String concatenation function, request for comments.



"michael.casey@xxxxxxxxx" wrote:
>
>> Better to write a piece of code of the general form:
>>
>> while (condition) {
>> /* concatenate thingb onto thinga */
>> }
>
> This method seems far too redundant especially when frequently used
> in practice. I see little advantage to using it apose to the current
> implementation of my function, which could be described as a variadic
> implementation of strlcat().
>
> The largest problem with the current version is the usage of an 'end
> of list' marker, which could easily be omitted. If it where omitted,
> the bug would be simple to trace however. I would like to hear about
> any concerns you have about its current implementation and use.
>
>> [...] I know of no second version, my implementation is unchanged, is
>> portable, and has no known reentrancy or threading problems. [...]
>
> The second version I was refering to was that of my function:
>
> Example:
> char vbuf[VAST_BUF];
> ret = vast(vbuf, VAST_BUF, "Put it", " together.", EOL);

Well, here you are starting at the right end, the interface to your
function. Get that right and you can then implement it in various
ways. My objection to your above interface is that there is no way
to control or prevent overflow, which is one of the points of
strlcat (its return shows what is actually needed). The other
direction is to have the routine always saw off the required space
via malloc, so it never fails barring memory exhaustion. Then your
prototype interface would be:

char *bigcat(const char *s1, ...);

returning NULL on failure, and requiring a final NULL in the list
of concatanees. The only interfacing foulup possible is omitting
the final NULL. An initial NULL could simply return an empty
string, so you can confidently pass the non-null results onward.
You are also confident that the returned item can be passed to
free. There are also no restrictions on the input strings
origins. The usage could then be:

if (!(s = bigcat(s1, s2, s3, s4, NULL))) theoperationfailed();
else {
/* further processing and use of s */
free(s);
}
s = NULL;

In fact, you can safely use this to inject things in front of a
bigcat string within the "further processing" above by:

s = bigcat("prefix string", s, NULL)

except that that is liable to a memory leak when the prefix
operation fails, similar to having realloc fail. The cure is the
same, use a temp.

Just some random thoughts.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson


.



Relevant Pages

  • Re: Retrieve name of VB6 calling app via reflection from C# COM interf
    ... string, or do you get a string that makes no sense? ... However, since you have the middle interface, why not require the caller to ... VB6 application. ... this fails when I use the VB6 client. ...
    (microsoft.public.dotnet.general)
  • Re: [OT] My First C# (warning - long post)
    ... or string,. ... public string IBreturn ... interface block with the new return code. ... It is unwieldy to keep referencing substrings of an 8K string ...
    (comp.lang.cobol)
  • Re: Explicit Linking of DLLs in VB.net
    ... yes this technique will only work on managed assemblys (exe or dll) ... "YourObject" is a form and is limited to the form methods. ... Wel implement propertys, methods, events in your interface and start ... Public Shared Function LoadMeByName(ByVal vstrAssemblyName As String, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Operator overloading [was Re: 7.0 wishlist?]
    ... I doubt Harry was proposing that the translation be naïve. ... public interface Addable{ ... BigDecimal--would be powerful enough, even if operator overloading ... String and return a String, ...
    (comp.lang.java.programmer)
  • Re: C# app with COM Interface for array of COM structs
    ... USDALib.PhysicalLocation_tI1000Gates.GetROI(int partID, string ... public interface I1000Gates ... riid, out IntPtr ppvObject) ... public interface IClassFactory ...
    (microsoft.public.dotnet.languages.csharp)