Author Topic: C Lessons  (Read 10480 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
C Lessons
« on: August 17, 2012, 04:41:09 AM »
« Last Edit: August 17, 2012, 12:51:46 PM by Charles Pegge »

Peter

  • Guest
Re: C Lessons
« Reply #1 on: August 17, 2012, 12:47:24 PM »
c-lesson  Arithmetic pointer

Do you know this easy expression?

int (* (* (a)) []) ()

a is a function with return value of type pointer to array of pointers to functions with return value of type int

Very easy to do with C. But is too heavy for OxygenBasic.

Charles Pegge

  • Guest
Re: C Lessons
« Reply #2 on: August 17, 2012, 01:04:31 PM »

A deprecated style of coding, I hope!

One should never have to encounter such expressions. OOP eliminates the need for pointy- pointy code :)

Peter

  • Guest
Re: C Lessons
« Reply #3 on: August 17, 2012, 01:30:54 PM »
Hi Charles,

You hope in vain!  :D

Charles Pegge

  • Guest
Re: C Lessons
« Reply #4 on: August 17, 2012, 01:34:41 PM »
A pleasant surprise: GCC supports inner functions

Code: C
  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5. int f()
  6. {
  7.   return 2;
  8. };
  9.  
  10.  
  11. int main()
  12. {
  13.   printf("%i\n",f()); //result 2
  14.   int f()
  15.   {
  16.     return 3;
  17.   };
  18.   printf("%i\n",f()); //result 3
  19. }
  20.  

Charles

efgee

  • Guest
Re: C Lessons
« Reply #5 on: August 17, 2012, 01:42:52 PM »
Wow, this was new to me...
Working to much with tcc I guess  :o

Thanks for the info.

Charles Pegge

  • Guest
Re: C Lessons
« Reply #6 on: August 17, 2012, 01:50:32 PM »
And of course, scope:

Code: C
  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5.  
  6. int main()
  7. {
  8.   int a=2;
  9.   {
  10.     int a=3;
  11.     printf("%i\n",a); //result 3
  12.   };
  13.   printf("%i\n",a); //result 2
  14. }
  15.  

Charles

Charles Pegge

  • Guest
Re: C Lessons
« Reply #7 on: August 17, 2012, 03:53:01 PM »
OOP model

Code: C
  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4.  
  5.  
  6. //VIRTUAL TABLE
  7.  
  8. typedef struct
  9. {
  10.   int (*m1)(void*this);
  11.   int (*m2)(void*this);
  12.   int c;
  13. } vfoo;
  14.  
  15. //OBJECT
  16.  
  17. typedef struct
  18. {
  19.   vfoo *vtp;
  20.   int  a;
  21. } cfoo;
  22.  
  23. //METHODS
  24.  
  25. int m1(void*this)
  26. {
  27.   cfoo*c=this;
  28.   c->a=4;
  29.   c->vtp->m2(this);
  30.   return 1;
  31. };
  32.  
  33. int m2(void*this)
  34. {
  35.   cfoo*c=this;
  36.   printf("ok");
  37.   return 2;
  38. };
  39.  
  40. //CREATE
  41.  
  42. int main()
  43. {
  44.   vfoo tfoo={&m1,&m2,1}; //virtual part. (once for all objects)
  45.   cfoo foo={&tfoo,0};    //object part. (each object)
  46.   foo.vtp->m1(&foo);
  47.   printf("%i",foo.a);
  48. }
  49. //result: ok4
  50.  

Charles

Charles Pegge

  • Guest
Re: C Lessons
« Reply #8 on: August 22, 2012, 03:01:08 AM »
BSTRING FUNCTIONS

bstrings are dynamic binary string pointers. The length is a 4 byte int value located immediately before the first character, and the string is terminated with at least 2 null characters which are not included in the length value.

the bstring points to the first character of the string.

Code: C
  1.  
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. typedef char * bstring;
  8.  
  9.  
  10. const int empty[4]={0,0,0,0};
  11.  
  12.  
  13. bstring bstringbuf(int n)
  14. {
  15.   if (n==0) return (char*) &empty+4;
  16.   int*t=malloc(n+8);
  17.   *t=n;
  18.   *(t+n-4)=0;
  19.   return (char*) t+4;
  20. }
  21.  
  22. bstring bstringnuls(int n)
  23. {
  24.   if (n==0) return (char*) &empty+4;
  25.   int m=(n+12)>>2;
  26.   int*t=calloc(m,sizeof(long));
  27.   *t=n;
  28.   return (char*) t+4;
  29. }
  30.  
  31. void bstringfree(bstring*s)
  32. {
  33.   if (s==NULL) return;
  34.   if (*s==NULL) return;
  35.   free(*s-sizeof(long));
  36.   *s=(bstring) &empty+4;
  37. }
  38.  
  39. long bstringlen(bstring s)
  40. {
  41.   if (s==NULL) return 0;
  42.   return *((long*)(s-sizeof(long)));
  43. }
  44.  
  45. void bstringcat(bstring *s,bstring t)
  46. {
  47.   if (t==NULL) return;
  48.   int ls,lt,le;
  49.   ls=bstringlen(*s);
  50.   lt=bstringlen(t);
  51.   if (lt==0) return;
  52.   le=ls+lt;
  53.   if (le>0)
  54.   {
  55.     bstring b=bstringnuls(le);
  56.     strcpy(b,*s);
  57.     strcpy(b+ls,t);
  58.     bstringfree(s);
  59.     *s=b;
  60.   }
  61. }
  62.  
  63. void bstringcatl(bstring *s,char*t)
  64. {
  65.   if (t==NULL) return;
  66.   int ls,lt,le;
  67.   ls=bstringlen(*s);
  68.   lt=strlen(t);
  69.   if (lt==0) return;
  70.   le=ls+lt;
  71.   if (le>0)
  72.   {
  73.     bstring b=bstringnuls(le);
  74.     if (*s != NULL) strcpy(b,*s);
  75.     strcpy(b+ls,t);
  76.     bstringfree(s);
  77.     *s=b;
  78.   }
  79. }
  80.  
  81. void bstringsetlen(bstring *s,int n)
  82. {
  83.   if (n==0) return;
  84.   int ls,lt,le;
  85.   ls=bstringlen(*s);
  86.   lt=n-ls;
  87.   if (lt==0) return;
  88.   le=ls+lt;
  89.   if (le>0)
  90.   {
  91.     bstring b=bstringnuls(le);
  92.     if (*s != NULL)
  93.     {
  94.       strncpy(b,*s,le);
  95.     }
  96.     bstringfree(s);
  97.     *s=b;
  98.   }
  99. }
  100.  
  101. void bstringleft(bstring*t,bstring s,int n)
  102. {
  103.   if (n<=0) n=0;
  104.   int le=bstringlen(s);
  105.   if (le>n) le=n;
  106.   bstring b=bstringnuls(le);
  107.   if (s != NULL) strncpy(b,s,le);
  108.   bstringfree(t);
  109.   *t=b;
  110. }
  111.  
  112. void bstringmid(bstring*t,bstring s,int p,int n)
  113. {
  114.   p--;
  115.   int le=bstringlen(s);
  116.   if (n<0) n=0;
  117.   if (p<0) p=le+p+1;   //offset from right
  118.   if (n>=le-p) n=le-p; //limit with offset
  119.   if (n<0) n=0;
  120.   bstring b=bstringnuls(n);
  121.   if (s != NULL) strncpy(b,s+p,n);
  122.   bstringfree(t);
  123.   *t=b;
  124. }
  125.  
  126. void bstringmidp(bstring*t,bstring s,int p)
  127. {
  128.   p--;
  129.   int lt=bstringlen(*t);
  130.   int ls=bstringlen(s);
  131.   if (p<0) p=lt+p+1;   //offset from right
  132.   if (ls>=lt-p) ls=lt-p; //limit with offset
  133.   if (ls<=0) return;
  134.   if (s != NULL) strncpy(*t+p,s,ls);
  135. }
  136.  
  137. void bstringmidpl(bstring*t,char* s,int p)
  138. {
  139.   p--;
  140.   int lt=bstringlen(*t);
  141.   int ls=strlen(s);
  142.   if (p<0) p=lt+p+1;   //offset from right
  143.   if (ls>=lt-p) ls=lt-p; //limit with offset
  144.   if (ls<=0) return;
  145.   if (s != NULL) strncpy(*t+p,s,ls);
  146. }
  147.  
  148.  
  149. int main()
  150. {
  151.   //TESTS
  152.   //
  153.   bstring       p=NULL,q=NULL;
  154.   bstringcatl   (&p,"Hello ");
  155.   bstringcatl   (&q,"World ");
  156.   bstringcat    (&p,q);
  157.   bstringcatl   (&p," ok");
  158.   bstringsetlen (&p,15);
  159.   bstringmid    (&p,p,-9,11);
  160.   bstringmidpl  (&p,"X",2);
  161.   printf        ("%s\n",p);
  162.   printf        ("%i\n",bstringlen(p));
  163.   bstringfree   (&p);
  164. }
  165.  

Charles

JRS

  • Guest
Re: C Lessons
« Reply #9 on: August 22, 2012, 01:02:30 PM »
Charles,

I tried to compile the BStr example with gcc under Ubuntu 64. I had to change ALL the long references to int. A long in 64 bit is 8 bytes I think.

jrs@laptop:~/O2C$ gcc bstr.c -o bstr
jrs@laptop:~/O2C$ ./bstr
WXrld  ok
9
jrs@laptop:~/O2C$

Code: [Select]
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <string.h> 
     
    typedef char * bstring; 
     
     
    const int empty[4]={0,0,0,0}; 
     
     
    bstring bstringbuf(int n) 
    { 
      if (n==0) return (char*) &empty+4; 
      int*t=malloc(n+8); 
      *t=n; 
      *(t+n-4)=0; 
      return (char*) t+4; 
    } 
     
    bstring bstringnuls(int n) 
    { 
      if (n==0) return (char*) &empty+4; 
      int m=(n+12)>>2; 
      int*t=calloc(m,sizeof(int)); 
      *t=n; 
      return (char*) t+4; 
    } 
     
    void bstringfree(bstring*s) 
    { 
      if (s==NULL) return; 
      if (*s==NULL) return; 
      free(*s-sizeof(int)); 
      *s=(bstring) &empty+4;   
    } 
     
    int bstringlen(bstring s) 
    { 
      if (s==NULL) return 0; 
      return *((int*)(s-sizeof(int))); 
    } 
     
    void bstringcat(bstring *s,bstring t) 
    { 
      if (t==NULL) return; 
      int ls,lt,le; 
      ls=bstringlen(*s); 
      lt=bstringlen(t); 
      if (lt==0) return; 
      le=ls+lt; 
      if (le>0) 
      { 
        bstring b=bstringnuls(le); 
        strcpy(b,*s); 
        strcpy(b+ls,t); 
        bstringfree(s); 
        *s=b; 
      } 
    } 
     
    void bstringcatl(bstring *s,char*t) 
    { 
      if (t==NULL) return; 
      int ls,lt,le; 
      ls=bstringlen(*s); 
      lt=strlen(t); 
      if (lt==0) return; 
      le=ls+lt; 
      if (le>0) 
      { 
        bstring b=bstringnuls(le); 
        if (*s != NULL) strcpy(b,*s); 
        strcpy(b+ls,t); 
        bstringfree(s); 
        *s=b; 
      } 
    } 
     
    void bstringsetlen(bstring *s,int n) 
    { 
      if (n==0) return; 
      int ls,lt,le; 
      ls=bstringlen(*s); 
      lt=n-ls; 
      if (lt==0) return; 
      le=ls+lt; 
      if (le>0) 
      { 
        bstring b=bstringnuls(le); 
        if (*s != NULL) 
        { 
          strncpy(b,*s,le); 
        } 
        bstringfree(s); 
        *s=b; 
      } 
    } 
     
    void bstringleft(bstring*t,bstring s,int n) 
    { 
      if (n<=0) n=0; 
      int le=bstringlen(s); 
      if (le>n) le=n; 
      bstring b=bstringnuls(le); 
      if (s != NULL) strncpy(b,s,le); 
      bstringfree(t); 
      *t=b; 
    } 
     
    void bstringmid(bstring*t,bstring s,int p,int n) 
    { 
      p--; 
      int le=bstringlen(s); 
      if (n<0) n=0; 
      if (p<0) p=le+p+1;   //offset from right 
      if (n>=le-p) n=le-p; //limit with offset 
      if (n<0) n=0; 
      bstring b=bstringnuls(n); 
      if (s != NULL) strncpy(b,s+p,n); 
      bstringfree(t); 
      *t=b; 
    } 
     
    void bstringmidp(bstring*t,bstring s,int p) 
    { 
      p--; 
      int lt=bstringlen(*t); 
      int ls=bstringlen(s); 
      if (p<0) p=lt+p+1;   //offset from right 
      if (ls>=lt-p) ls=lt-p; //limit with offset 
      if (ls<=0) return; 
      if (s != NULL) strncpy(*t+p,s,ls); 
    } 
     
    void bstringmidpl(bstring*t,char* s,int p) 
    { 
      p--; 
      int lt=bstringlen(*t); 
      int ls=strlen(s); 
      if (p<0) p=lt+p+1;   //offset from right 
      if (ls>=lt-p) ls=lt-p; //limit with offset 
      if (ls<=0) return; 
      if (s != NULL) strncpy(*t+p,s,ls); 
    } 
     
     
    int main() 
    { 
      //TESTS 
      // 
      bstring       p=NULL,q=NULL; 
      bstringcatl   (&p,"Hello "); 
      bstringcatl   (&q,"World "); 
      bstringcat    (&p,q); 
      bstringcatl   (&p," ok"); 
      bstringsetlen (&p,15); 
      bstringmid    (&p,p,-9,11); 
      bstringmidpl  (&p,"X",2); 
      printf        ("%s\n",p); 
      printf        ("%i\n",bstringlen(p)); 
      bstringfree   (&p); 
    } 

Charles Pegge

  • Guest
Re: C Lessons
« Reply #10 on: August 22, 2012, 06:55:43 PM »
Thanks John.

I did some investigation and I think the most reliable way to specify a fixed length integer is: __int32  / __int64.

Charles
« Last Edit: August 23, 2012, 01:53:06 PM by Charles Pegge »

JRS

  • Guest
Re: C Lessons
« Reply #11 on: August 22, 2012, 07:38:34 PM »
I had to use the <stdint.h> include and change all the __int32 to u_int32_t for your new version to compile with gcc on Ubuntu 64.

jrs@laptop:~/O2C$ gcc bstr2.c -o bstr2
bstr2.c: In function ‘bstringbuf’:
bstr2.c:13:7: error: unknown type name ‘__int32’
bstr2.c: In function ‘bstringnuls’:
bstr2.c:23:7: error: unknown type name ‘__int32’
bstr2.c: At top level:
bstr2.c:36:5: error: unknown type name ‘__int32’
bstr2.c: In function ‘bstringlen’:
bstr2.c:39:17: error: ‘__int32’ undeclared (first use in this function)
bstr2.c:39:17: note: each undeclared identifier is reported only once for each function it appears in
bstr2.c:39:25: error: expected expression before ‘)’ token
jrs@laptop:~/O2C$

Code: [Select]
   #include <stdio.h>  
    #include <stdlib.h>  
    #include <string.h>  
    #include <stdint.h>
      
    typedef char * bstring;  
      
    const int empty[4]={0,0,0,0};  
      
      
    bstring bstringbuf(int n)  
    {  
      if (n==0) return (char*) &empty+4;  
      u_int32_t*t=malloc(n+8);  
      *t=n;  
      *(t+n-4)=0;  
      return (char*) t+4;  
    }  
      
    bstring bstringnuls(int n)  
    {  
      if (n==0) return (char*) &empty+4;  
      int m=(n+12)>>2;  
      u_int32_t*t=calloc(m,4);  
      *t=n;  
      return (char*) t+4;  
    }  
      
    void bstringfree(bstring*s)  
    {  
      if (s==NULL) return;  
      if (*s==NULL) return;  
      free(*s-4);  
      *s=(bstring) &empty+4;  
    }  
      
    u_int32_t bstringlen(bstring s)  
    {  
      if (s==NULL) return 0;  
      return *((u_int32_t*)(s-4));  
    }  
      
    void bstringcat(bstring *s,bstring t)  
    {  
      if (t==NULL) return;  
      int ls,lt,le;  
      ls=bstringlen(*s);  
      lt=bstringlen(t);  
      if (lt==0) return;  
      le=ls+lt;  
      if (le>0)  
      {  
        bstring b=bstringnuls(le);  
        strcpy(b,*s);  
        strcpy(b+ls,t);  
        bstringfree(s);  
        *s=b;  
      }  
    }  
      
    void bstringcatl(bstring *s,char*t)  
    {  
      if (t==NULL) return;  
      int ls,lt,le;  
      ls=bstringlen(*s);  
      lt=strlen(t);  
      if (lt==0) return;  
      le=ls+lt;  
      if (le>0)  
      {  
        bstring b=bstringnuls(le);  
        if (*s != NULL) strcpy(b,*s);  
        strcpy(b+ls,t);  
        bstringfree(s);  
        *s=b;  
      }  
    }  
      
    void bstringsetlen(bstring *s,int n)  
    {  
      if (n==0) return;  
      int ls,lt,le;  
      ls=bstringlen(*s);  
      lt=n-ls;  
      if (lt==0) return;  
      le=ls+lt;  
      if (le>0)  
      {  
        bstring b=bstringnuls(le);  
        if (*s != NULL)  
        {  
          strncpy(b,*s,le);  
        }  
        bstringfree(s);  
        *s=b;  
      }  
    }  
      
    void bstringleft(bstring*t,bstring s,int n)  
    {  
      if (n<=0) n=0;  
      int le=bstringlen(s);  
      if (le>n) le=n;  
      bstring b=bstringnuls(le);  
      if (s != NULL) strncpy(b,s,le);  
      bstringfree(t);  
      *t=b;  
    }  
      
      
    void bstringmid(bstring*t,bstring s,int p,int n)  
    {  
      p--;  
      int le=bstringlen(s);  
      if (n<0) n=0;  
      if (p<0) p=le+p+1;   //offset from right  
      if (n>=le-p) n=le-p; //limit with offset  
      if (n<0) n=0;  
      bstring b=bstringnuls(n);  
      if (s != NULL) strncpy(b,s+p,n);  
      bstringfree(t);  
      *t=b;  
    }  
      
      
    void bstringmidp(bstring*t,bstring s,int p)  
    {  
      p--;  
      int lt=bstringlen(*t);  
      int ls=bstringlen(s);  
      if (p<0) p=lt+p+1;   //offset from right  
      if (ls>=lt-p) ls=lt-p; //limit with offset  
      if (ls<=0) return;  
      if (s != NULL) strncpy(*t+p,s,ls);  
    }  
      
      
    void bstringmidpl(bstring*t,char* s,int p)  
    {  
      p--;  
      int lt=bstringlen(*t);  
      int ls=strlen(s);  
      if (p<0) p=lt+p+1;   //offset from right  
      if (ls>=lt-p) ls=lt-p; //limit with offset  
      if (ls<=0) return;  
      if (s != NULL) strncpy(*t+p,s,ls);  
    }  
      
    int main()  
    {  
      //TESTS  
      //  
      bstring       p=NULL,q=NULL;  
      bstringcatl   (&p,"Hello ");  
      bstringcatl   (&q,"World ");  
      bstringcat    (&p,q);  
      bstringcatl   (&p," ok");  
      bstringsetlen (&p,15);  
      bstringmid    (&p,p,-9,11);  
      bstringmidpl  (&p,"X",2);  
      printf        ("%s\n",p);  
      printf        ("%i\n",bstringlen(p));  
      bstringfree   (&p);  
    }  

jrs@laptop:~/O2C$ ./bstr2
WXrld  ok
9
jrs@laptop:~/O2C$
« Last Edit: August 22, 2012, 09:24:51 PM by JRS »

Charles Pegge

  • Guest
Re: C Lessons
« Reply #12 on: August 23, 2012, 01:06:38 AM »
This is nuts! :)

MinGW GCC stdint does not have such a type.
Does your stdint.h define int32_t ?

JRS

  • Guest
Re: C Lessons
« Reply #13 on: August 23, 2012, 01:15:22 AM »
I don't know where I got u_int32_t when it should have been int32_t.

Code: [Select]
/* Copyright (C) 1997,1998,1999,2000,2001,2006 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.  */

/*
 * ISO C99: 7.18 Integer types <stdint.h>
 */

#ifndef _STDINT_H
#define _STDINT_H 1

#include <features.h>
#include <bits/wchar.h>
#include <bits/wordsize.h>

/* Exact integral types.  */

/* Signed.  */

/* There is some amount of overlap with <sys/types.h> as known by inet code */
#ifndef __int8_t_defined
# define __int8_t_defined
typedef signed char int8_t;
typedef short int int16_t;
typedef int int32_t;
# if __WORDSIZE == 64
typedef long int int64_t;
# else
__extension__
typedef long long int int64_t;
# endif
#endif

/* Unsigned.  */
typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;
#ifndef __uint32_t_defined
typedef unsigned int uint32_t;
# define __uint32_t_defined
#endif
#if __WORDSIZE == 64
typedef unsigned long int uint64_t;
#else
__extension__
typedef unsigned long long int uint64_t;
#endif


/* Small types.  */

/* Signed.  */
typedef signed char int_least8_t;
typedef short int int_least16_t;
typedef int int_least32_t;
#if __WORDSIZE == 64
typedef long int int_least64_t;
#else
__extension__
typedef long long int int_least64_t;
#endif

/* Unsigned.  */
typedef unsigned char uint_least8_t;
typedef unsigned short int uint_least16_t;
typedef unsigned int uint_least32_t;
#if __WORDSIZE == 64
typedef unsigned long int uint_least64_t;
#else
__extension__
typedef unsigned long long int uint_least64_t;
#endif


/* Fast types.  */

/* Signed.  */
typedef signed char int_fast8_t;
#if __WORDSIZE == 64
typedef long int int_fast16_t;
typedef long int int_fast32_t;
typedef long int int_fast64_t;
#else
typedef int int_fast16_t;
typedef int int_fast32_t;
__extension__
typedef long long int int_fast64_t;
#endif

/* Unsigned.  */
typedef unsigned char uint_fast8_t;
#if __WORDSIZE == 64
typedef unsigned long int uint_fast16_t;
typedef unsigned long int uint_fast32_t;
typedef unsigned long int uint_fast64_t;
#else
typedef unsigned int uint_fast16_t;
typedef unsigned int uint_fast32_t;
__extension__
typedef unsigned long long int uint_fast64_t;
#endif


/* Types for `void *' pointers.  */
#if __WORDSIZE == 64
# ifndef __intptr_t_defined
typedef long int intptr_t;
#  define __intptr_t_defined
# endif
typedef unsigned long int uintptr_t;
#else
# ifndef __intptr_t_defined
typedef int intptr_t;
#  define __intptr_t_defined
# endif
typedef unsigned int uintptr_t;
#endif


/* Largest integral types.  */
#if __WORDSIZE == 64
typedef long int intmax_t;
typedef unsigned long int uintmax_t;
#else
__extension__
typedef long long int intmax_t;
__extension__
typedef unsigned long long int uintmax_t;
#endif


/* The ISO C99 standard specifies that in C++ implementations these
   macros should only be defined if explicitly requested.  */
#if !defined __cplusplus || defined __STDC_LIMIT_MACROS

# if __WORDSIZE == 64
#  define __INT64_C(c) c ## L
#  define __UINT64_C(c) c ## UL
# else
#  define __INT64_C(c) c ## LL
#  define __UINT64_C(c) c ## ULL
# endif

/* Limits of integral types.  */

/* Minimum of signed integral types.  */
# define INT8_MIN (-128)
# define INT16_MIN (-32767-1)
# define INT32_MIN (-2147483647-1)
# define INT64_MIN (-__INT64_C(9223372036854775807)-1)
/* Maximum of signed integral types.  */
# define INT8_MAX (127)
# define INT16_MAX (32767)
# define INT32_MAX (2147483647)
# define INT64_MAX (__INT64_C(9223372036854775807))

/* Maximum of unsigned integral types.  */
# define UINT8_MAX (255)
# define UINT16_MAX (65535)
# define UINT32_MAX (4294967295U)
# define UINT64_MAX (__UINT64_C(18446744073709551615))


/* Minimum of signed integral types having a minimum size.  */
# define INT_LEAST8_MIN (-128)
# define INT_LEAST16_MIN (-32767-1)
# define INT_LEAST32_MIN (-2147483647-1)
# define INT_LEAST64_MIN (-__INT64_C(9223372036854775807)-1)
/* Maximum of signed integral types having a minimum size.  */
# define INT_LEAST8_MAX (127)
# define INT_LEAST16_MAX (32767)
# define INT_LEAST32_MAX (2147483647)
# define INT_LEAST64_MAX (__INT64_C(9223372036854775807))

/* Maximum of unsigned integral types having a minimum size.  */
# define UINT_LEAST8_MAX (255)
# define UINT_LEAST16_MAX (65535)
# define UINT_LEAST32_MAX (4294967295U)
# define UINT_LEAST64_MAX (__UINT64_C(18446744073709551615))


/* Minimum of fast signed integral types having a minimum size.  */
# define INT_FAST8_MIN (-128)
# if __WORDSIZE == 64
#  define INT_FAST16_MIN (-9223372036854775807L-1)
#  define INT_FAST32_MIN (-9223372036854775807L-1)
# else
#  define INT_FAST16_MIN (-2147483647-1)
#  define INT_FAST32_MIN (-2147483647-1)
# endif
# define INT_FAST64_MIN (-__INT64_C(9223372036854775807)-1)
/* Maximum of fast signed integral types having a minimum size.  */
# define INT_FAST8_MAX (127)
# if __WORDSIZE == 64
#  define INT_FAST16_MAX (9223372036854775807L)
#  define INT_FAST32_MAX (9223372036854775807L)
# else
#  define INT_FAST16_MAX (2147483647)
#  define INT_FAST32_MAX (2147483647)
# endif
# define INT_FAST64_MAX (__INT64_C(9223372036854775807))

/* Maximum of fast unsigned integral types having a minimum size.  */
# define UINT_FAST8_MAX (255)
# if __WORDSIZE == 64
#  define UINT_FAST16_MAX (18446744073709551615UL)
#  define UINT_FAST32_MAX (18446744073709551615UL)
# else
#  define UINT_FAST16_MAX (4294967295U)
#  define UINT_FAST32_MAX (4294967295U)
# endif
# define UINT_FAST64_MAX (__UINT64_C(18446744073709551615))


/* Values to test for integral types holding `void *' pointer.  */
# if __WORDSIZE == 64
#  define INTPTR_MIN (-9223372036854775807L-1)
#  define INTPTR_MAX (9223372036854775807L)
#  define UINTPTR_MAX (18446744073709551615UL)
# else
#  define INTPTR_MIN (-2147483647-1)
#  define INTPTR_MAX (2147483647)
#  define UINTPTR_MAX (4294967295U)
# endif


/* Minimum for largest signed integral type.  */
# define INTMAX_MIN (-__INT64_C(9223372036854775807)-1)
/* Maximum for largest signed integral type.  */
# define INTMAX_MAX (__INT64_C(9223372036854775807))

/* Maximum for largest unsigned integral type.  */
# define UINTMAX_MAX (__UINT64_C(18446744073709551615))


/* Limits of other integer types.  */

/* Limits of `ptrdiff_t' type.  */
# if __WORDSIZE == 64
#  define PTRDIFF_MIN (-9223372036854775807L-1)
#  define PTRDIFF_MAX (9223372036854775807L)
# else
#  define PTRDIFF_MIN (-2147483647-1)
#  define PTRDIFF_MAX (2147483647)
# endif

/* Limits of `sig_atomic_t'.  */
# define SIG_ATOMIC_MIN (-2147483647-1)
# define SIG_ATOMIC_MAX (2147483647)

/* Limit of `size_t' type.  */
# if __WORDSIZE == 64
#  define SIZE_MAX (18446744073709551615UL)
# else
#  define SIZE_MAX (4294967295U)
# endif

/* Limits of `wchar_t'.  */
# ifndef WCHAR_MIN
/* These constants might also be defined in <wchar.h>.  */
#  define WCHAR_MIN __WCHAR_MIN
#  define WCHAR_MAX __WCHAR_MAX
# endif

/* Limits of `wint_t'.  */
# define WINT_MIN (0u)
# define WINT_MAX (4294967295u)

#endif /* C++ && limit macros */


/* The ISO C99 standard specifies that in C++ implementations these
   should only be defined if explicitly requested.  */
#if !defined __cplusplus || defined __STDC_CONSTANT_MACROS

/* Signed.  */
# define INT8_C(c) c
# define INT16_C(c) c
# define INT32_C(c) c
# if __WORDSIZE == 64
#  define INT64_C(c) c ## L
# else
#  define INT64_C(c) c ## LL
# endif

/* Unsigned.  */
# define UINT8_C(c) c
# define UINT16_C(c) c
# define UINT32_C(c) c ## U
# if __WORDSIZE == 64
#  define UINT64_C(c) c ## UL
# else
#  define UINT64_C(c) c ## ULL
# endif

/* Maximal type.  */
# if __WORDSIZE == 64
#  define INTMAX_C(c) c ## L
#  define UINTMAX_C(c) c ## UL
# else
#  define INTMAX_C(c) c ## LL
#  define UINTMAX_C(c) c ## ULL
# endif

#endif /* C++ && constant macros */

#endif /* stdint.h */
« Last Edit: August 23, 2012, 01:26:54 AM by JRS »

Charles Pegge

  • Guest
Re: C Lessons
« Reply #14 on: August 23, 2012, 01:57:47 PM »
Parsing words and symbols

Code: C
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5.  
  6. //BWORD
  7. //parsing
  8.  
  9. char SkipSpace(char*s,int*i)
  10. {
  11.   char c;
  12.   while (1)
  13.   {
  14.     c=s[*i];
  15.     if (c==0) break;
  16.     if (c>32) break;
  17.     (*i)++;
  18.   }
  19.   return c;
  20. }
  21.  
  22.  
  23. void EndLine(char*s,int*i)
  24. {
  25.   char c;
  26.   while (1)
  27.   {
  28.     c=s[*i];
  29.     if ((c==0)||(c==10)||(c==13)) break;
  30.     (*i)++;
  31.   }
  32. }
  33.  
  34.  
  35. void SkipLine(char*s,int*i)
  36. {
  37.   char c;
  38.   while (1)
  39.   {
  40.     c=s[*i];
  41.     if (c==0) break;
  42.     if (c==10)
  43.     {
  44.       (*i)++;
  45.       break;
  46.     }
  47.     if (c==13)
  48.     {
  49.       *i++;
  50.       if (s[*i]==10)
  51.       {
  52.         (*i)++;
  53.         break;
  54.       }
  55.     }
  56.     (*i)++;
  57.   }
  58. }
  59.  
  60.  
  61. char SkipLSpace(char*s,int*i)
  62. {
  63.   char c;
  64.   while (1)
  65.   {
  66.     c=s[*i];
  67.     if (c==0)  break;
  68.     if (c==10) break;
  69.     if (c==13) break;
  70.     if (c>32)  break;
  71.     (*i)++;
  72.   }
  73.   return c;
  74. }
  75.  
  76.  
  77. char SkipWord(char*s,int*i)
  78. {
  79.   char c,d;
  80.   c=s[*i];
  81.   //if (strchr(".,;:!£$%^&*()+-*/\\|={}[]<>?@~'",c)!=NULL)
  82.   //{
  83.   //  (*i)++;
  84.   //  return c;
  85.   //}
  86.   //
  87.   //QUOTES
  88.   if ((c==34) || (c==96))
  89.   {
  90.     (*i)++;
  91.     while (1)
  92.     {
  93.       d=s[*i];
  94.       if ((d==c) || (c==0)) break;
  95.       (*i)++;
  96.     }
  97.     (*i)++;
  98.     return c;
  99.   }
  100.   if (c==35) goto morechars;
  101.   if (c<48)  goto singles;
  102.   if (c<58)  goto morechars;
  103.   if (c<65)  goto singles;
  104.   if (c<91)  goto morechars;
  105.   if (c==95) goto morechars;
  106.   if (c<97)  goto singles;
  107.   if (c<123) goto morechars;
  108.   if (c<128) goto singles;
  109.   //
  110.   singles:
  111.   //
  112.   (*i)++;
  113.   return c;
  114.   //
  115.   morechars:
  116.   //
  117.   while (1)
  118.   {
  119.     (*i)++;
  120.     c=s[*i];
  121.     if (c==35) continue;
  122.     if (c<48)  break;
  123.     if (c<58)  continue;
  124.     if (c<65)  break;
  125.     if (c<91)  continue;
  126.     if (c==95) continue;
  127.     if (c<97)  break;
  128.     if (c<123) continue;
  129.     if (c<128) break;
  130.   }
  131.   return c;
  132. }
  133.  
  134.  
  135. typedef struct
  136. {
  137.   char ascw,ascn;
  138.   int  swd,nwd,lenw;
  139. } bword;
  140.  
  141.  
  142. void wword(bword*w,char*s,int*i)
  143. {
  144.   w->ascw=SkipSpace(s,i);
  145.   int b=*i;
  146.   w->swd=b;
  147.   SkipWord(s,i);
  148.   w->lenw=*i-b;
  149.   w->ascn=SkipLSpace(s,i);
  150.   w->nwd=*i;
  151. }
  152.  
  153.  
  154. void ShowWord(bword*w,char*s)
  155. {
  156.   char wr[128];
  157.   strncpy(wr,s+w->swd,w->lenw);
  158.   wr[w->lenw]=(char) 0;
  159.   printf("%s ",wr);
  160. }
  161.  
  162.  
  163. /*
  164. #include <stdint.h>
  165. int8_t ii;
  166. int32_t jj;
  167. uint64_t kk;
  168. */
  169.  
  170. int main()
  171. {
  172.   //TESTS
  173.   //
  174.   char s[ ]="\
  175.  Hello Brave New World!!\\\
  176.  ";
  177.   int  i=0,j;
  178.   bword w;
  179.   for (j=1;j<8;j++)
  180.   {
  181.     wword(&w,s,&i);
  182.     ShowWord(&w,s);
  183.     if (s[ i]==0) break;
  184.   }
  185.   printf("\n%i",j);
  186.   printf("\n%i",w.ascw);
  187.   printf("\n%i",w.ascn);
  188. }
  189.  

Charles

« Last Edit: August 23, 2012, 02:12:41 PM by Charles Pegge »