[Include] "stringh.inc": Implementing useful functions from C's "string.h"
#1

About "stringh.inc":

Hey... guess this is my first release here. Anyway, some of you (depending where you've come from) might know me as "Lunghi".

This include is released under the Mozilla Public License v2.0 (MPL v2.0) and is a collection of macros seeking to build upon SA:MP's solid foundations in terms of support for string manipulation and add functionality for slightly more obscure functions, taken from the C standard. The aim from the start was to emulate the most useful, and at the time missing functions from "string.h".

Functions included (click hyperlinks for function documentation):
  1. strcpy
  2. strncpy
  3. strncat
  4. strcmp
  5. strncmp
  6. strcasecmp
  7. strncasecmp
Some of the definitions might seem useless, but the idea behind those particular definitions is to give PAWN a more C-like experience, for those who feel more comfortable with it, without mandating said preference for everybody else.

In the source code, it is explained how to use each of these functions, with additional information about them alongside.

I am not sure how often any of these will be useful, but hopefully they make life easier for someone either coming over from C or C++, or someone who needs to use one of the more advanced features not available yet in PAWN.

I will be updating and maintining this as I see fit and on that note (18/MAY/2013 - when the project was first uploaded), as I did this so late at night, there may be mistakes. If anyone notices any (there probably aren't any though), then just leave a message behind and I'll fix it ASAP.

Documentation may come later (if necessary), as with an extended post and a better explanation in the body of this post about the functions.

Download (and source): http://pastebin.com/LjQGUsQN (Version: v0.5)
Credits: None (removed previous credited function(s) from the include)
Licence: Mozilla Public License v2.0 (MPL v2.0)
Reply
#2

How is using any of these functions better than using the already given to us string functions? (strcat, strcmp, etc)
Reply
#3

i am not known to any of such function , can u please Document them?, uses and features
EDiT: ^ in the thread too, though u already provided it in pastebin.
and yes
Quote:
Originally Posted by Rajat_Pawar
Посмотреть сообщение
How is using any of these functions better than using the already given to us string functions? (strcat, strcmp, etc)
if they are same as already provided samp function
Reply
#4

Quote:
Originally Posted by Rajat_Pawar
Посмотреть сообщение
How is using any of these functions better than using the already given to us string functions? (strcat, strcmp, etc)
Have you ever seen strncat in SA:MP before? I don't think so. Most of them are unique, a couple of them are just to help people who prefer to program as close to C or C++ as possible.
Reply
#5

Your macros
pawn Код:
#define strncpy(%0,%1,%2,%3) \
                        strmid(%1,%1,0,((%2)-1)) \
                        strcat((%0[0] = '\0', %0),%1,%3)

#define strncat(%0,%1,%2,%3) \
                        strmid(%1,%1,0,((%2)-1)) \
                        strcat(%0,%1,%2)

#define strcasecmp(%0,%1,%2,%3) \
                        strtolower(%0) \
                        strtolower(%1) \
                        strcmp(%0,%1,%2,%3)

#define strncasecmp(%0,%1,%2,%3) \
                        strtolower(%0) \
                        strtolower(%0) \
                        strcmp(%0,%1,%2,%3)
won't work, since you have to put semicolons before you call the last functions.

Try
pawn Код:
#define strncpy(%0,%1,%2,%3)    strmid(%1,%1,0,((%2)-1));strcat((%0[0]='\0',%0),%1,%3)
#define strncat(%0,%1,%2,%3)    strmid(%1,%1,0,((%2)-1));strcat(%0,%1,%2)
#define strcasecmp(%0,%1,%2,%3) strtolower(%0);strtolower(%1);strcmp(%0,%1,%2,%3)
#define strncasecmp(%0,%1,%2,%3)    strtolower(%0);strtolower(%0);strcmp(%0,%1,%2,%3)
Reply
#6

pawn Код:
/*
        Name: strncmp
        Usage: strncmp(string1, string2, ignorecase, num chars from start)
*/

#define strncmp(%0,%1,%2,%3) \
                        strcmp(%0,%1,%2,%3)
lolwut?
Reply
#7

Quote:
Originally Posted by Emmet_
Посмотреть сообщение
pawn Код:
/*
        Name: strncmp
        Usage: strncmp(string1, string2, ignorecase, num chars from start)
*/

#define strncmp(%0,%1,%2,%3) \
                        strcmp(%0,%1,%2,%3)
lolwut?
http://www.cplusplus.com/reference/cstring/strcmp/
http://www.cplusplus.com/reference/cstring/strncmp/
Reply
#8

I gathered that, I made it at about 2/3AM last night, I'm almost done changing it now and re-defining some of the things in there that are a bit wrong.
Reply
#9

Do you even know what I'm talking about or what? He basically made a macro called 'strncmp' which is just a renamed version of strcmp.
Reply
#10

Quote:
Originally Posted by Emmet_
Посмотреть сообщение
Do you even know what I'm talking about or what? He basically made a macro called 'strncmp' which is just a renamed version of strcmp.
READ: "a couple of them are just to help people who prefer to program as close to C or C++ as possible."
Obviously you've never used C/C++ before. Besides, as I said, that was in error, I made this at 3AM last night. Just finalising the changes now.
Reply
#11

Quote:
Originally Posted by Aeonosphere
Посмотреть сообщение
READ: "a couple of them are just to help people who prefer to program as close to C or C++ as possible."
Obviously you've never used C/C++ before. Besides, as I said, that was in error, I made this at 3AM last night. Just finalising the changes now.
Shit, my bad. I didn't read the post lol

By the way...

pawn Код:
#define chrtolower(%1) \
        (((%1) > 0x40 && (%1) <= 0x5A) ? ((%1) | 0x20) : (%1))
That macro is useless because there's a function included with one of the SA-MP includes by default: tolower (works the same way).
Reply
#12

UPDATED. Check the first post. All the stuff from being tired is fixed. As far as I know in the PAWN pre-processor, you can declare new variables. If that doesn't work, or there's any other bugs, tell me and I'll fix 'em.
Reply
#13

You cannot simply do something like for example
pawn Код:
#define strncpy(%0,%1,%2,%3) \
                        new cchar[] \
                        ;strmid(char,%1,0,((%2)-1)) \
                        ;strdel(char,%2,strlen(char)) \
                        ;strcat((%0[0] = '\0', %0),char,%3)
since macros are basicly text replacements and, if you try to use such macro twice in your script
pawn Код:
public someCallback()
{
    new some_array[4], some_array_2[4];
    strncpy(some_array, "foo", 2, sizeof some_array); // not the same "sizeof" we know from C/C++
    strncpy(some_array_2, "bar", 2, sizeof some_array_2);
}
becomes
pawn Код:
public someCallback()
{
    new some_array[4], some_array_2[4];
    new cchar[];
    strmid(char, "foo",0,(( 2)-1));
    strdel(char, 2,strlen(char));
    strcat((some_array[0] = '\0', some_array),char, sizeof some_array);
    new cchar[];
    strmid(char, "bar",0,(( 2)-1));
    strdel(char, 2,strlen(char));
    strcat((some_array_2[0] = '\0', some_array_2),char, sizeof some_array_2);
}
will give you compiling errors, like that you've not declared the size of a "cchar" array and redeclaration of a "cchar" array.
Reply
#14

Quote:
Originally Posted by BigETI
Посмотреть сообщение
<snip>
I see. I think I sorted this problem out. This one is better off as a public function. Updated (v0.5) in the first post.
Reply
#15

Atleast nice effort, but you are allowed to use my "just from scratch recreated" include http://pastebin.com/DMid5gRQ freely, which includes much more functions (macros and stocks), also they almost behave like the ones used for C/C++.
  • __strcat_ as strcat
  • strncat
  • strchr
  • strrchr
  • __strcmp_ as strcmp
  • strncmp
  • strcasecmp
  • strncasecmp
  • strcpy
  • strncpy
  • strpbrk
  • strspn
  • strcspn
  • strstr
  • strcasestr
Reply
#16

Quote:
Originally Posted by ******
Посмотреть сообщение
Both those includes have versions of strcpy which look something like the standard strcpy macro used on the forums (leading me to believe you are lying when you say there are no credits), but both broken in some way.
I would really like to see the one who made a PAWN version of strcpy before us.
Reply
#17

I just showed an improved version of the main topic's include. I had really no idea who originally made this strcpy macro.
That's why I used to say "both", since I had no idea it exists now for some years. Nice found ******!
Reply
#18

What's wrong with NULL terminating a string and use strcat to make it look like strcpy. I've actually tested this macro...

Edit:
pawn Код:
new buffer[128] = "";
__strcat_(buffer, "Test __strcat_");
print(buffer);
strncat(buffer, "Test strncat", 12);
print(buffer);
printf("First 's' at %d", strchr(buffer, 's'));
printf("Last 's' at %d", strrchr(buffer, 's'));
printf("__strcmp_ result: %d", __strcmp_("foo", "Foo"));
printf("strncmp result: %d", strncmp("foo", "Foo", 2));
printf("strcasecmp result: %d", strcmp("foo", "Foo"));
printf("strncasecmp result: %d", strncasecmp("foo", "Foo", 2));
strcpy(buffer, "Test strcpy");
print(buffer);
strncpy(buffer, "Test strncpy", 12);
print(buffer);
print(strpbrk(buffer, {'s', 't', 'c'}));
printf("strspn %d", strspn(buffer, {'s', 't', 'c'}));
printf("strspn %d", strcspn(buffer, {'s', 't', 'c'}));
printf("strstr %d", strstr(buffer, "Test"));
printf("strcasestr %d", strstr(buffer, "Test"));
Код:
[22:45:40] Test __strcat_
[22:45:40] Test strncat
[22:45:40] First 's' at 3
[22:45:40] Last 's' at 6
[22:45:40] __strcmp_ result: 1
[22:45:40] strncmp result: 1
[22:45:40] strcasecmp result: 1
[22:45:40] strncasecmp result: 0
[22:45:40] Test strcpy
[22:45:40] Test strncpy
[22:45:40] ssttcssttrsspn 
[22:45:40] strspn 9
[22:45:40] strspn 3
[22:45:40] strstr 0
[22:45:40] strcasestr 0
Reply
#19

pawn Код:
#define strcpy(%0,%1)           strcat((%0[0]='\0',%0),%1,_)
It's the same as you would use "sizeof"


or...
pawn Код:
#define strcpy(%0,%1)           strcat((%0[0]='\0',%0),%1,strlen(%1))
?
Reply
#20

Well, then as you said
pawn Код:
#define strcpy(%0,%1)           strcat((%0[0]='\0',%0),%1)
should allow to make this parameter optional for the user.
In the first place I thought to restrict the optional arguments at the strcpy macro for the user, so it becomes more likely the one used in C/C++. But as you said before it uses to have conflicts with enums and etc. Thanks for the info.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)