Hi,
I need functions that would return null terminated strings for me, for
example:
char *HTMLi(const char *string)
This function receives a pointer to a null terminated string, and
should return <i>This was the string</i>.
To make it more complex, I could even return the result of the above
function to this:
char *HTMLb(const char *string)
As a result, this is what I expect to have <b><i>This was the
string</i></b>
How about allocating memory; I assume if I allocate memory in these
functions, and never free it, this is a so called memory leak.
Does somebody has a sample code for me having the same logic? Your
feedback is really appreciated!
Thanks, 12 1623 gc******@belgacom.net wrote on 01/06/05 : Hi,
I need functions that would return null terminated strings for me, for example:
char *HTMLi(const char *string)
This function receives a pointer to a null terminated string, and should return <i>This was the string</i>.
To make it more complex, I could even return the result of the above function to this:
char *HTMLb(const char *string)
As a result, this is what I expect to have <b><i>This was the string</i></b>
How about allocating memory; I assume if I allocate memory in these functions, and never free it, this is a so called memory leak.
Does somebody has a sample code for me having the same logic? Your feedback is really appreciated!
Thanks,
The obvious way : the function returns an allocated bloc that you have
to free after use.
Another solution could fit.
define a size:
size_t size = 128;
define a string
char *s = malloc(size + 1);
Maintain the size and allocated memory through the converter functions
:
s = HTMLi (s, &size, "Hello world");
printf ("%s", s);
fflush (stdout);
s = HTMLb (s, &size, s);
printf ("%s", s);
fflush (stdout);
Use memmove() to make the copies and realloc() on request (and update
size accordingly). On realloc() error, just exit(), or free() and
return NULL, but the application would have to check against NULL each
time... (can be done simply with some embedded goto...)
#define CHK\
do {if (s==NULL) goto err;} while (0)
s = HTMLi (s, &size, "Hello world");
CHK;
printf ("%s", s);
fflush (stdout);
s = HTMLb (s, &size, s);
CHK;
printf ("%s", s);
fflush (stdout);
err:
Once finished, just free the allocated bloc.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html
"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.
On 1 Jun 2005 12:31:42 -0700, gc******@belgacom.net wrote: Does somebody has a sample code for me having the same logic? Your feedback is really appreciated!
There are several ways to do it in C but I'd like to point out that C
is not exactly a language of choice for string operations (always been
a bit of a weak point of C) so, perhaps, you should consider another
programming language for your application if it contains lots of
string handling (Perl, for example).
Salut Emmanuel!
Thank you for your clear explanation!
I have only one question; if I need to do a realloc in a function,
won't this cost a lot in performance?
Regards
Dirk gc******@belgacom.net wrote: Salut Emmanuel!
Thank you for your clear explanation!
I have only one question; if I need to do a realloc in a function, won't this cost a lot in performance?
Please quote enough context so that everyone can see what
you are responding to -- the original message may for example
not have made it to some newsserver (yet).
As for performance issues: The C standard does not tell us
anything about relative performance.
A good rule of thumb for many, but not all systems and
implementations is to start out with a sensible buffer size which
makes realloc()ating a rare event; on realloc()ation, rather
increase the buffer size by a certain factor (1.5 or 2, for example)
than by a fixed amount in order to keep the number of times low.
BTW: It is possible that your implementation gives you a larger
chunk of memory than you requested, to the effect that at reallocation
only administrative information has to be updated.
If you implement a solution using realloc() which is too slow,
be sure to measure carefully to find out where the time loss
happens, i.e. whether this is really the fault of realloc().
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address. gc******@belgacom.net wrote: Salut Emmanuel!
Thank you for your clear explanation!
I have only one question; if I need to do a realloc in a function, won't this cost a lot in performance?
Regards Dirk
Hi Dirk,
The problem is, your destination string is larger then your source string.
So, you need to realloc/malloc if you want to maintain the prototype you
specified. However, you can take the approach to make the situation a
little better where you allocate the maximum chunk of memory to build the
full HTML upfront. Then, use the following function to modify it in-place.
I have modified the prototype a little and you don't need to define HTMLi
HTMLb indipendently. For example the following program...
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define BOLD "b"
#define ITALICS "i"
int HTMLx(char *str,int max_size, char *type)
{
int prefix_len;
int orig_len;
prefix_len = strlen(type) + 2;
orig_len = strlen(str);
if (max_size < (prefix_len+1)*2)
return -1; /* Failure... not enough space */
memmove(str+prefix_len,str,orig_len+1);
/* Build the prefix */
str[0]='<';
memcpy(str+1,type,strlen(type));
str[prefix_len - 1]='>';
/* Build the suffix */
str[prefix_len + orig_len] = '<';
str[prefix_len + orig_len + 1] = '/';
memcpy(str+prefix_len+orig_len+2,type,strlen(type) );
str[prefix_len + orig_len + strlen(type) + 2] = '>';
}
int main()
{
char *str = malloc(1000);
sprintf(str,"Hello Worldie");
HTMLx(str,1000,ITALICS);
printf("%s\n",str);
HTMLx(str,1000,BOLD);
printf("%s\n",str);
return 0;
}
Will give you output of :
<i>Hello Worldie</i>
<b><i>Hello Worldie</i></b>
Also, you can make it more robust where, when you run out of memory, it
reallocs another chunk. That will improve performance in terms of using
reduced number of reallocs.
Hope that helps.
- IG
<gc******@belgacom.net> wrote I have only one question; if I need to do a realloc in a function, won't this cost a lot in performance?
Yes.
calls to malloc() or realloc() are always expensive in C terms, though not
always in terms of other languages. Precise execution times do of course
vary from platform to platform.
A good method is to call malloc() for as much memory as you will ever
realistically need, keeping realloc() in reserve so that formally the
function is unbounded. Then call realloc() once at the end to shrink the
block to the required size. gc******@belgacom.net wrote: I need functions that would return null terminated strings for me, for example:
char *HTMLi(const char *string)
This function receives a pointer to a null terminated string, and should return <i>This was the string</i>.
I suggest you build your code around strlcpy and strlcat. Source,
documentation, and references are available at:
<http://cbfalconer.home.att.net/download/strlcpy.zip>
--
"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare gc******@belgacom.net wrote: Hi,
I need functions that would return null terminated strings for me, for example:
char *HTMLi(const char *string)
This function receives a pointer to a null terminated string, and should return <i>This was the string</i>.
To make it more complex, I could even return the result of the above function to this:
char *HTMLb(const char *string)
As a result, this is what I expect to have <b><i>This was the string</i></b>
How about allocating memory; I assume if I allocate memory in these functions, and never free it, this is a so called memory leak.
Does somebody has a sample code for me having the same logic? Your feedback is really appreciated!
Thanks,
Working directly with strings is not a good idea for your problem i think.
Maybe you shhould use a stack of strings. Were each element has two strings
(the opening tag and the closing tag or NULL). All you have to do then is
pushing your data onto the stack, print the stack or convert it to a flat
string and freeing the stack.
--
Michael Knaup
Emmanuel Delahaye wrote: in********@gmail.com wrote on 02/06/05 : int main() { char *str = malloc(1000); sprintf(str,"Hello Worldie"); HTMLx(str,1000,ITALICS); printf("%s\n",str); HTMLx(str,1000,BOLD); printf("%s\n",str); return 0; }
You forgot to free() the block after use and to check against NULL...
:-).
Yes, I just skipped the error checks. It was just a prototype of what could
be done.
Ideally, along with those checks, we should be checking error code of HTMLx
also to see if it worked.
--
-IG gc******@belgacom.net wrote: Hi,
I need functions that would return null terminated strings for me, for example:
char *HTMLi(const char *string)
As per your description:
char *HTMLi(const char *string)
{
return "<i>This was the string</i>";
} This function receives a pointer to a null terminated string, and should return <i>This was the string</i>.
To make it more complex, I could even return the result of the above function to this:
char *HTMLb(const char *string)
And just as easily
char *HTMLb(const char *string)
{
return "<b><i>This was the string</i></b>";
}
As a result, this is what I expect to have <b><i>This was the string</i></b>
How about allocating memory; I assume if I allocate memory in these functions, and never free it, this is a so called memory leak.
Does somebody has a sample code for me having the same logic? Your feedback is really appreciated!
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =---- gc******@belgacom.net wrote: Hi,
I need functions that would return null terminated strings for me, for example:
char *HTMLi(const char *string)
This function receives a pointer to a null terminated string, and should return <i>This was the string</i>.
To make it more complex, I could even return the result of the above function to this:
char *HTMLb(const char *string)
As a result, this is what I expect to have <b><i>This was the string</i></b>
How about allocating memory; I assume if I allocate memory in these functions, and never free it, this is a so called memory leak.
Does somebody has a sample code for me having the same logic? Your feedback is really appreciated!
Thanks,
This is yet another question with the same answer. Get FreeDOS Edlin
2.5 (available on ibiblio or alt.sources) and dyke out the
string-handling code.
Then, you could do something like:
STRING_T *HTMLi(STRING_T *this)
{
DSinsertcstr(this, 0, "<i>", NPOS);
DSappendcstr(this, "</i>", NPOS);
return this;
}
/* Gregory Pietsch */ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Carl Youngblood |
last post by:
I imagine this subject has probably been brought up numerous times.
Forgive me for bringing it up again. I was googling through old posts
on this newsgroup about it and found a good suggestion on...
|
by: Mike Tyka |
last post by:
Hello community,
i'm fairly new to using the STL but i've been experimenting a bit with it.
I tried to derive a new class say MyString from string like so:
class MyString: public string{...
|
by: Paul |
last post by:
hi, there,
for example,
char *mystr="##this is##a examp#le";
I want to replace all the "##" in mystr with "****". How can I do this?
I checked all the string functions in C, but did not...
|
by: Tim Conner |
last post by:
Hi,
Thanks to Peter, Chris and Steven who answered my previous answer about
regex to split a string. Actually, it was as easy as create a regex with the
pattern "/*-+()," and most of my string...
|
by: Duncan Winn |
last post by:
I am new to VC++7. I am using a method GetPrivateProfileString that
requires an LPTSTR.
I have defined this as a:
char * data_name;
I am then trying to convert this to an LPOLESTR and I...
| |
by: zoro |
last post by:
Hi,
I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party
string handling library that includes some very useful string
functions, in particular I'm interested in BEFORE (return...
|
by: karthi |
last post by:
hi,
I need user defined function that converts string to float in c.
since the library function atof and strtod occupies large space in
my processor memory I can't use it in my code.
regards,...
|
by: Hitesh |
last post by:
Hi,
In python doc -- 4.1.4 Deprecated string functions -- I read that "The
following list of functions are also defined as methods of string and
Unicode objects; see ``String Methods'' (section...
|
by: =?Utf-8?B?Sm9hY2hpbQ==?= |
last post by:
I have seen the following function to convert from a System::String^ to a
const wchar_t*. I would like to get a LPCTSTR and AFAIK LPCTSTR is equal to
const wchar_t*. Then it should all work right?...
|
by: Cartoper |
last post by:
There is one little static C library to manage the serial number and
unlock key for my application. Today it is compiled with Microsoft
VC6 and linked into both VC6 modules and VS2005 modules. It...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |