Thread: cpp Makefiles

cpp Makefiles

From
Lonnie Cumberland
Date:
Hello All,

I have now been able to compile the complex.c functions and test them out with
no problems.

I made a copy of the Makefile, Makefile.global, and Makefile.port (Linux) from
the tutorial directory and made a simple modification for the paths. Now the
'c" files compile up just fine.

The problem is that this set up will not work if I make some C++ files. I think
that there is something wrong in one of the above makefiles for the g++
compiler and the needed options, but do not know where to find it.

I also need to link in some other libraries like "libfile.a" that I have for
some of the functions that will be placed in my pgSQL interface but do not know
where to add these library files so that the generated "file.so" can use them.

does someone have a simple Makefile that will allow me to compile up C++
functions and either link in the libfile.a files or could please tell me how to
make libfile.so out of them?

Cheers,
Lonnie 

__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail. 
http://personal.mail.yahoo.com/


Re: cpp Makefiles

From
Peter Eisentraut
Date:
Lonnie Cumberland writes:

> does someone have a simple Makefile that will allow me to compile up C++
> functions and either link in the libfile.a files or could please tell me how to
> make libfile.so out of them?

There is no simple Makefile to do that, that's the problem.  If you need
help with your particular setup you should show in detail what you already
have and precisely what problem occurred.  If you're using the makefiles
from the PostgreSQL tree, I suggest you update to 7.1 and look into
src/interfaces/libpq++ for an example.  Also make sure your functions are
using C linkage (extern "C"), otherwise PostgreSQL won't find them.  And
avoid static constructors.  In general, C++ shared libraries are a complex
area that is hard to get right.

-- 
Peter Eisentraut   peter_e@gmx.net   http://funkturm.homeip.net/~peter



Re: cpp Makefiles

From
Lonnie Cumberland
Date:
Hello Peter,

I am in the process of downloading the new 7.1 PostgreSQL and will update my
system from the 7.03 version.

Actually there is not a problem in compiling the examples in the
interfaces/libpq++ directory and those work just fine.

I will try to explain the problem and the steps that I have taken to get that
problem generated in the hopes that it will help illuminate the error.

I have a simple test function:

-----------------------------------------------------------------------------

 
#include "postgres.h"           // for variable length type
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>             // for unix crypt function
text *
concat_text(text *arg1, text *arg2);
text *
concat_text(text *arg1, text *arg2)
{   int32 new_text_size = VARSIZE(arg1) + VARSIZE(arg2) - VARHDRSZ;   text *new_text = (text *) malloc(new_text_size);
memset((void *) new_text, 0, new_text_size);   VARSIZE(new_text) = new_text_size;   strncpy(VARDATA(new_text),
VARDATA(arg1),VARSIZE(arg1)-VARHDRSZ);   strncat(VARDATA(new_text), VARDATA(arg2), VARSIZE(arg2)-VARHDRSZ);   return
(new_text);
}                                                                                     
-------------------------------------------------------------------------

which I have saved as two different files for testing:

files: "funcs.c" and "funcs.cc"

load into the pgsql command interpreter the registration of this function:

"psql -f funcs.sql -d trdata"

the "funcs.sql" is simply:

------------------------------------------------------------------------------
DROP FUNCTION concat_text(text, text);
CREATE FUNCTION concat_text(text, text) RETURNS text
AS '/treazurac/funcs.so' LANGUAGE 'c';
------------------------------------------------------------------------------ 

Now then, the two different outcomes are:

Outcome A:
1. I do a "make" with just the "funcs.c" program in the directory and the
"funcs.so" is created just fine.

I get the following messages:
-------------------------------------------------------------------------
[root@Treazurac /test]# make
gcc -I./include -I./backend   -O2 -Wall -Wmissing-prototypes
-Wmissing-declarations -I/usr/include/pgsql -I/usr/include -fpic   -c -o
funcs.o funcs.c
gcc -shared -o funcs.so funcs.o
rm funcs.o
----------------------------------------------------------------------------  

2. Enter the "psql trdata"

3. do a "select concat_text('a','b');

and the result is a string "ab" just as expected. 

Outcome B:
1. I do a "make" with just the "funcs.cc".

This time the g++ compiler is called and I get:
-------------------------------------------------------------------------
g++    -c -o funcs.o funcs.cc
funcs.cc:23: postgres.h: No such file or directory
make: *** [funcs.o] Error 1                         
--------------------------------------------------------------------------

I have tried adjusting the INCLUDES and LIB paths but is seems that there is
some other things going on in the Makefile, Makefile.global, or Makefile.port
because I cannot simply enter the same "gcc" line to get a usable "funcs.so".

The reason that I need this to work is because I will be adding some c++
functions to the "funcs.cc" file and also need to be able to link in those
libraries as well.

Cheers,
Lonnie


__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail. 
http://personal.mail.yahoo.com/


g++ not working for postgresql extension languages?

From
Lonnie Cumberland
Date:
Hi All again,

One more thing that I have tried.

If I now do a similar thing as in my previous message with the "funcs.c" file,
but this time with the "funcs.cc" file then I can get:

-------------------------------------------------------------------------
[root@Treazurac /test]# g++ -I./include -I./backend -O2 -Wall
-Wmissing-prototypes -Wmissing-declarations -I/usr/include/pgsql -I/usr/include
-fpic -c -o funcs.o funcs.cc

[root@Treazurac /test]# g++ -shared -o funcs.so funcs.o

[root@Treazurac /test]# ls

Makefile  Makefile.global  Makefile.port  funcs.cc  funcs.o  funcs.so* 
funcs.sql

[root@Treazurac /test]# pico -w funcs.sql

[root@Treazurac /test]# psql -f funcs.sql -d trdata
DROP
CREATE

[root@Treazurac /test]# psql trdata
Welcome to psql, the PostgreSQL interactive terminal.
Type:  \copyright for distribution terms      \h for help with SQL commands      \? for help on internal slash commands
    \g or terminate with semicolon to execute query      \q to quit
 
trdata=# select concat_text('a','b');
ERROR:  Can't find function concat_text in file /test/funcs.so
trdata=#         
--------------------------------------------------------------------------

And although I should not be running as "root", this is a test machine and it
is ok for the time being.

If I do the same thing as above, but using the "funcs.c" file (which is the
exact same file just renamed)
---------------------------------------------------------------------------
funcs.c and funcs.cc
-------------------------
#include "postgres.h"           // for variable length type
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>             // for unix crypt function
text *
concat_text(text *arg1, text *arg2);
text *
concat_text(text *arg1, text *arg2)
{   int32 new_text_size = VARSIZE(arg1) + VARSIZE(arg2) - VARHDRSZ;   text *new_text = (text *) malloc(new_text_size);
memset((void *) new_text, 0, new_text_size);   VARSIZE(new_text) = new_text_size;   strncpy(VARDATA(new_text),
VARDATA(arg1),VARSIZE(arg1)-VARHDRSZ);   strncat(VARDATA(new_text), VARDATA(arg2), VARSIZE(arg2)-VARHDRSZ);   return
(new_text);
}                                                            
--------------------------------------------------------------------------

with funcs.sql
------------------
DROP FUNCTION concat_text(text, text);
CREATE FUNCTION concat_text(text, text) RETURNS text    AS '/test/funcs.so' LANGUAGE 'c'; 
----------------------------------------------------------------------------

With the "funcs.c" file in place I get:
-----------------------------------------------------------------------------
[root@Treazurac /test]# gcc -I./include -I./backend -O2 -Wall
-Wmissing-prototypes -Wmissing-declarations -I/usr/include/pgsql -I/usr/include
-fpic -c -o funcs.o funcs.c

[root@Treazurac /test]# gcc -shared -o funcs.so funcs.o

[root@Treazurac /test]# psql -f funcs.sql -d trdata
DROP
CREATE

[root@Treazurac /test]# psql trdata
Welcome to psql, the PostgreSQL interactive terminal.
Type:  \copyright for distribution terms      \h for help with SQL commands      \? for help on internal slash commands
    \g or terminate with semicolon to execute query      \q to quit
 
trdata=# select concat_text('a','b');concat_text
-------------ab
(1 row)
trdata=#            
-----------------------------------------------------------------------------

so now I do not understand why the "gcc" version works and the "g++" version
does not?

Just a side note in that I can easliy compile c++ the examples in the
interfaces/libpq++ directory without any problems so that this is very strange
to me. 
I really need the "g++" version to work correctly as well?

Any ideas anyone?

Cheers
Lonnie



__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail. 
http://personal.mail.yahoo.com/


Re: cpp Makefiles

From
Lonnie Cumberland
Date:
Hello Peter,

I am in the process of downloading the new 7.1 PostgreSQL and will update my
system from the 7.03 version.

Actually there is not a problem in compiling the examples in the
interfaces/libpq++ directory and those work just fine.

I will try to explain the problem and the steps that I have taken to get that
problem generated in the hopes that it will help illuminate the error.

I have a simple test function:

-----------------------------------------------------------------------------

 
#include "postgres.h"           // for variable length type
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>             // for unix crypt function
text *
concat_text(text *arg1, text *arg2);
text *
concat_text(text *arg1, text *arg2)
{   int32 new_text_size = VARSIZE(arg1) + VARSIZE(arg2) - VARHDRSZ;   text *new_text = (text *) malloc(new_text_size);
memset((void *) new_text, 0, new_text_size);   VARSIZE(new_text) = new_text_size;   strncpy(VARDATA(new_text),
VARDATA(arg1),VARSIZE(arg1)-VARHDRSZ);   strncat(VARDATA(new_text), VARDATA(arg2), VARSIZE(arg2)-VARHDRSZ);   return
(new_text);
}                                                                                     
-------------------------------------------------------------------------

which I have saved as two different files for testing:

files: "funcs.c" and "funcs.cc"

load into the pgsql command interpreter the registration of this function:

"psql -f funcs.sql -d trdata"

the "funcs.sql" is simply:

------------------------------------------------------------------------------
DROP FUNCTION concat_text(text, text);
CREATE FUNCTION concat_text(text, text) RETURNS text
AS '/treazurac/funcs.so' LANGUAGE 'c';
------------------------------------------------------------------------------ 

Now then, the two different outcomes are:

Outcome A:
1. I do a "make" with just the "funcs.c" program in the directory and the
"funcs.so" is created just fine.

I get the following messages:
-------------------------------------------------------------------------
[root@Treazurac /test]# make
gcc -I./include -I./backend   -O2 -Wall -Wmissing-prototypes
-Wmissing-declarations -I/usr/include/pgsql -I/usr/include -fpic   -c -o
funcs.o funcs.c
gcc -shared -o funcs.so funcs.o
rm funcs.o
----------------------------------------------------------------------------  

2. Enter the "psql trdata"

3. do a "select concat_text('a','b');

and the result is a string "ab" just as expected. 

Outcome B:
1. I do a "make" with just the "funcs.cc".

This time the g++ compiler is called and I get:
-------------------------------------------------------------------------
g++    -c -o funcs.o funcs.cc
funcs.cc:23: postgres.h: No such file or directory
make: *** [funcs.o] Error 1                         
--------------------------------------------------------------------------

I have tried adjusting the INCLUDES and LIB paths but is seems that there is
some other things going on in the Makefile, Makefile.global, or Makefile.port
because I cannot simply enter the same "gcc" line to get a usable "funcs.so".

The reason that I need this to work is because I will be adding some c++
functions to the "funcs.cc" file and also need to be able to link in those
libraries as well.

Cheers,
Lonnie


__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail. 
http://personal.mail.yahoo.com/


Re: [SQL] g++ not working for postgresql extension languages?

From
Tom Lane
Date:
Lonnie Cumberland <lonnie_cumberland@yahoo.com> writes:
> so now I do not understand why the "gcc" version works and the "g++" version
> does not?

You need to say extern "C" to persuade a C++ compiler to compile a
function as something that can be called from plain C.  They are not
the same language.  See your C++ documentation for more info.
        regards, tom lane


Re: g++ not working for postgresql extension languages?

From
Lonnie Cumberland
Date:
Thanks All,

It is now solved by using the "extern "C" {....} directive.

Thanks for all of the help..

Cheers :)

Lonnie
--- Lonnie Cumberland <lonnie_cumberland@yahoo.com> wrote:
> Hi All again,
> 
> One more thing that I have tried.
> 
> If I now do a similar thing as in my previous message with the "funcs.c"
> file,
> but this time with the "funcs.cc" file then I can get:
> 
> -------------------------------------------------------------------------
> [root@Treazurac /test]# g++ -I./include -I./backend -O2 -Wall
> -Wmissing-prototypes -Wmissing-declarations -I/usr/include/pgsql
> -I/usr/include
> -fpic -c -o funcs.o funcs.cc
> 
> [root@Treazurac /test]# g++ -shared -o funcs.so funcs.o
> 
> [root@Treazurac /test]# ls
> 
> Makefile  Makefile.global  Makefile.port  funcs.cc  funcs.o  funcs.so* 
> funcs.sql
> 
> [root@Treazurac /test]# pico -w funcs.sql
> 
> [root@Treazurac /test]# psql -f funcs.sql -d trdata
> DROP
> CREATE
> 
> [root@Treazurac /test]# psql trdata
> Welcome to psql, the PostgreSQL interactive terminal.
>  
> Type:  \copyright for distribution terms
>        \h for help with SQL commands
>        \? for help on internal slash commands
>        \g or terminate with semicolon to execute query
>        \q to quit
>  
> trdata=# select concat_text('a','b');
> ERROR:  Can't find function concat_text in file /test/funcs.so
> trdata=#         
> --------------------------------------------------------------------------
> 
> And although I should not be running as "root", this is a test machine and it
> is ok for the time being.
> 
> If I do the same thing as above, but using the "funcs.c" file (which is the
> exact same file just renamed)
> ---------------------------------------------------------------------------
> funcs.c and funcs.cc
> -------------------------
> #include "postgres.h"           // for variable length type
>  
> #include <stdio.h>
> #include <string.h>
> #include <stdlib.h>
> #include <time.h>
>  
> #include <unistd.h>             // for unix crypt function
>  
> text *
> concat_text(text *arg1, text *arg2);
>  
> text *
> concat_text(text *arg1, text *arg2)
> {
>     int32 new_text_size = VARSIZE(arg1) + VARSIZE(arg2) - VARHDRSZ;
>     text *new_text = (text *) malloc(new_text_size);
>  
>     memset((void *) new_text, 0, new_text_size);
>     VARSIZE(new_text) = new_text_size;
>     strncpy(VARDATA(new_text), VARDATA(arg1), VARSIZE(arg1)-VARHDRSZ);
>     strncat(VARDATA(new_text), VARDATA(arg2), VARSIZE(arg2)-VARHDRSZ);
>     return (new_text);
> }                                                            
> --------------------------------------------------------------------------
> 
> with funcs.sql
> ------------------
> DROP FUNCTION concat_text(text, text);
> CREATE FUNCTION concat_text(text, text) RETURNS text
>      AS '/test/funcs.so' LANGUAGE 'c'; 
> ----------------------------------------------------------------------------
> 
> With the "funcs.c" file in place I get:
> -----------------------------------------------------------------------------
> [root@Treazurac /test]# gcc -I./include -I./backend -O2 -Wall
> -Wmissing-prototypes -Wmissing-declarations -I/usr/include/pgsql
> -I/usr/include
> -fpic -c -o funcs.o funcs.c
> 
> [root@Treazurac /test]# gcc -shared -o funcs.so funcs.o
> 
> [root@Treazurac /test]# psql -f funcs.sql -d trdata
> DROP
> CREATE
> 
> [root@Treazurac /test]# psql trdata
> Welcome to psql, the PostgreSQL interactive terminal.
>  
> Type:  \copyright for distribution terms
>        \h for help with SQL commands
>        \? for help on internal slash commands
>        \g or terminate with semicolon to execute query
>        \q to quit
>  
> trdata=# select concat_text('a','b');
>  concat_text
> -------------
>  ab
> (1 row)
>  
> trdata=#            
> -----------------------------------------------------------------------------
> 
> so now I do not understand why the "gcc" version works and the "g++" version
> does not?
> 
> Just a side note in that I can easliy compile c++ the examples in the
> interfaces/libpq++ directory without any problems so that this is very
> strange
> to me. 
>  
> I really need the "g++" version to work correctly as well?
> 
> Any ideas anyone?
> 
> Cheers
> Lonnie
> 
> 
> 
> __________________________________________________
> Do You Yahoo!?
> Get email at your own domain with Yahoo! Mail. 
> http://personal.mail.yahoo.com/
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 3: if posting/reading through Usenet, please send an appropriate
> subscribe-nomail command to majordomo@postgresql.org so that your
> message can get through to the mailing list cleanly


__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail. 
http://personal.mail.yahoo.com/


Re: [SQL] g++ not working for postgresql extension languages?

From
Stephen Jackson
Date:
Lonnie,                
C++ uses "name mangling" to permit overloading of functions. Therefore
your function concat_text() ends up with a very different name. You can
probably get round this as follows:

#ifdef __cplusplus
extern "C" {
#endif
text *
concat_text(text *arg1, text *arg2);
#ifdef __cplusplus
}
#endif

This tells the C++ compiler that concat_text is a "C" name - so don't do
name mangling. This is a common technique for making C++ code callable
from C, although I haven't used it in the specific context you are
using.

Hope this helps,

Stephen Jackson
Home: Stephen.Jackson@panting-deer.org.uk  www.panting-deer.org.uk
Work: Stephen.Jackson@looksystems.co.uk    www.looksystems.co.uk

Lonnie Cumberland wrote:
> 
> Hi All again,
> 
> One more thing that I have tried.
> 
> If I now do a similar thing as in my previous message with the "funcs.c" file,
> but this time with the "funcs.cc" file then I can get:
> 

[snip]

> 
> trdata=# select concat_text('a','b');
> ERROR:  Can't find function concat_text in file /test/funcs.so
> trdata=#
> --------------------------------------------------------------------------
> 

[snip]

> 
> text *
> concat_text(text *arg1, text *arg2);
> 

[snip]

> so now I do not understand why the "gcc" version works and the "g++" version
> does not?
> 
> Just a side note in that I can easliy compile c++ the examples in the
> interfaces/libpq++ directory without any problems so that this is very strange
> to me.
> 
> I really need the "g++" version to work correctly as well?
> 
> Any ideas anyone?
> 
> Cheers
> Lonnie
>


Re: Re: [SQL] g++ not working for postgresql extension languages?

From
Lonnie Cumberland
Date:
Hello All,

I have gotten the EXTERN "C" statement to work with the simple test c++
functions, but I cannot get it to work with the my actual crypto4.0 c++ sources
which I am able to compile all together into a single funcs.so file.

I am getting no errors but when I go into the "psql trdata" and try the same
test then I get this error:

[root@Treazurac /test]# psql trdata
Welcome to psql, the PostgreSQL interactive terminal.

Type:  \copyright for distribution terms      \h for help with SQL commands      \? for help on internal slash commands
    \g or terminate with semicolon to execute query      \q to quit
 

trdata=# select concat_text('a','d');
ERROR:  Load of file /test/trfuncs.so failed: /test/trfuncs.so: undefined
symbol: MemoryContextAlloc__FP17MemoryContextDataUi

-----------------------------------------------------------------------------

I do not know what is going on here because I did a simple test without my
cyrpto4.0 c++ files and using 3 different copies of concat_text(),
concat1_text() and concat2_text() which compiled and even ran fine.  

now when I put in my other sources which compile correctly outside of
Postgresql, I get the above error.

I have zipped up the test files and will send them to someone if you would like
to take a quick look. Just let me know as I am at a loss as to why PostgreSQL
is having som much trouble with this C++ interface.

Cheers
Lonnie

--- Stephen Jackson <Stephen.Jackson@panting-deer.org.uk> wrote:
> Lonnie,
>                  
> C++ uses "name mangling" to permit overloading of functions. Therefore
> your function concat_text() ends up with a very different name. You can
> probably get round this as follows:
> 
> #ifdef __cplusplus
> extern "C" {
> #endif
> text *
> concat_text(text *arg1, text *arg2);
> #ifdef __cplusplus
> }
> #endif
> 
> This tells the C++ compiler that concat_text is a "C" name - so don't do
> name mangling. This is a common technique for making C++ code callable
> from C, although I haven't used it in the specific context you are
> using.
> 
> Hope this helps,
> 
> Stephen Jackson
> Home: Stephen.Jackson@panting-deer.org.uk  www.panting-deer.org.uk
> Work: Stephen.Jackson@looksystems.co.uk    www.looksystems.co.uk
> 
> Lonnie Cumberland wrote:
> > 
> > Hi All again,
> > 
> > One more thing that I have tried.
> > 
> > If I now do a similar thing as in my previous message with the "funcs.c"
> file,
> > but this time with the "funcs.cc" file then I can get:
> > 
> 
> [snip]
> 
> > 
> > trdata=# select concat_text('a','b');
> > ERROR:  Can't find function concat_text in file /test/funcs.so
> > trdata=#
> > --------------------------------------------------------------------------
> > 
> 
> [snip]
> 
> > 
> > text *
> > concat_text(text *arg1, text *arg2);
> > 
> 
> [snip]
> 
> > so now I do not understand why the "gcc" version works and the "g++"
> version
> > does not?
> > 
> > Just a side note in that I can easliy compile c++ the examples in the
> > interfaces/libpq++ directory without any problems so that this is very
> strange
> > to me.
> > 
> > I really need the "g++" version to work correctly as well?
> > 
> > Any ideas anyone?
> > 
> > Cheers
> > Lonnie
> >
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster


__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail. 
http://personal.mail.yahoo.com/


Re: Re: [SQL] g++ not working for postgresql extension languages?

From
Peter Eisentraut
Date:
Lonnie Cumberland writes:

> trdata=# select concat_text('a','d');
> ERROR:  Load of file /test/trfuncs.so failed: /test/trfuncs.so: undefined
> symbol: MemoryContextAlloc__FP17MemoryContextDataUi

Name mangling at its finest.  You need to put extern "C" around all
#include's of PostgreSQL header files as well.

-- 
Peter Eisentraut   peter_e@gmx.net   http://funkturm.homeip.net/~peter



Now for the VARDATA, VARSIZE, and VARHDRSZ stuff

From
Lonnie Cumberland
Date:
Hello All,

That fixed the problem I think as I am no longer getting the loading errors and
the functions appear to be being called.

I now have a simple question about the use of these VARDATA() VARSIZE() and
VARHDRSZ() wrappers

I tried to make the modifications using the manual for reference, but there
seems to be little information related to these VAR???() wrappers and my simple
routine went off into never-never land.

I have a simple function that used to work with individual pointers.

My original simple routine:
------------------------------------------------------------------------------
void public_to_enc(char *public_userid, char *enc_userid )
{   char ch[3];   int i,j;   char buff[5];   int sp=0;   // Clear out the public_userid place   strcpy(enc_userid,"");
while(*public_userid!='\0')   {       ch[0]=*public_userid;       ch[1]=*(public_userid+1);       i=atoi(ch);
j=i;      sp++;
 
       //      printf("48 sect --- i=%d\n",i);       if((i>=1)&&(i<=10))       {               i+=47;       }
else      //      printf("65 sect --- i=%d\n",i);       if((i>=11)&&(i<=36))       {               i+=54;       }
else      //      printf("97 sect --- i=%d\n",i);       if((i>=37)&&(i<=62))       {               i+=60;        }
 else        printf("public_enc: ERROR !!!!\n\n");       sprintf(buff,"%c",i);       // Add the value to our string
 strcat(enc_userid,buff);       public_userid+=2;       }
 
}                                                                              

------------------------------------------------------------------------------

and my new modified routine of the above:
------------------------------------------------------------------------------
text *
encode(text *enc_userid)
{   int32 new_text_size = VARSIZE(enc_userid) * 2 - VARHDRSZ;   text *new_text = (text *) palloc(new_text_size);   //
Clearout the public_userid place   memset((void *) new_text, 0, new_text_size);   VARSIZE(new_text) = new_text_size;
//range values   //          (0 - 9)  ---- dec 48 - 57 ascii   //          (A - Z)  ---- dec 65 - 90 ascii   //
(a - z)  ---- dec 97 - 122 ascii   char ch;   int i,j;   char buff[5];   int sp=0;   while(VARDATA(enc_userid)!='\0')
{      ch=VARDATA(enc_userid);       i=ch;       j=i;       sp++;       //      printf("48 sect --- i=%d\n",i);
if((i>=48)&&(i<=57))      {               i-=47;       }       //      printf("65 sect --- i=%d\n",i);       else
if((i>=65)&&(i<=90))      {               i-=54;       }       else       //      printf("97 sect --- i=%d\n",i);
if((i>=97)&&(i<=122))      {               i-=60;       }       else       printf("public encoder: ERROR !!!!\n\n");
  sprintf(buff,"%02d",i);       // Add the value to our string       strcat(VARDATA(new_text),VARDATA(buff));
enc_userid++;      }   // Add the value to our string   strcat(VARDATA(new_text),VARDATA("\0"));   return (new_text);
 
}                                              
-------------------------------------------------------------------------------

The problem is that I do not think that I have converted the second version
over correctly and would like to know if someone could take a look to see what
might be wrong.

Also, I just wanted to say that I really appreciate all of the help that you
you have given me to get started using this interface with PostgreSQL.

Best Regards,
Lonnie



--- Peter Eisentraut <peter_e@gmx.net> wrote:
> Lonnie Cumberland writes:
> 
> > trdata=# select concat_text('a','d');
> > ERROR:  Load of file /test/trfuncs.so failed: /test/trfuncs.so: undefined
> > symbol: MemoryContextAlloc__FP17MemoryContextDataUi
> 
> Name mangling at its finest.  You need to put extern "C" around all
> #include's of PostgreSQL header files as well.
> 
> -- 
> Peter Eisentraut   peter_e@gmx.net   http://funkturm.homeip.net/~peter
> 


__________________________________________________
Do You Yahoo!?
Get email at your own domain with Yahoo! Mail. 
http://personal.mail.yahoo.com/


Re: Now for the VARDATA, VARSIZE, and VARHDRSZ stuff

From
Peter Eisentraut
Date:
Lonnie Cumberland writes:

> I now have a simple question about the use of these VARDATA() VARSIZE() and
> VARHDRSZ() wrappers

VARDATA is a pointer (to char?) to where the actual data of the specific
value starts.  VARSIZE is the length of the specific value, plus the
header.  VARHRDZ is the size of the header, currently always 4.  For
example, if you have a text string 'abc' then the memory representation is

int    4    <-- text* points here
char    'a'    <-- VARDATA points here
char    'b'
char    'c'

VARHDRZ == 4, VARSIZE = 7

Note that the data is not null-terminated.

The best things might be not to mess with this but call textin() and
textout() to convert your data to C strings.

-- 
Peter Eisentraut   peter_e@gmx.net   http://funkturm.homeip.net/~peter



Re: Now for the VARDATA, VARSIZE, and VARHDRSZ stuff

From
Brendan Guenther
Date:

Peter Eisentraut wrote:

> The best things might be not to mess with this but call textin() and
> textout() to convert your data to C strings.

I can't seem to find documentation for textin() and textout() can somebody point
me in the right direction for this or provide an example.  I'm having similar
problems that Lonnie is having with VARDATA and I think this might help.  I'm
trying to link in a C function that makes use of OpenSSL's libcrypto functions.

Basically I just need to hash and verify passwords in the database, so as an
alternate question, if I'm missing something and there is a better way to do
password storage let me know.

Thanks,
--
Brendan Guenther
guenthe8@msu.edu



Re: Re: Now for the VARDATA, VARSIZE, and VARHDRSZ stuff

From
Lonnie Cumberland
Date:
Hello Brendan,

I too was having this problem, but now think that I am getting a very good
handle on these interfaces.

Being that you are trying to hash the standard unix crypt function to look over
the password file, I think that this solution might help you.

I have a simple an efficient C++ solution to this problem:

My "unixcrypt" functions now looks like this                                        
---------------------------------------------------------------
text *unix_encrypt(text *inarg, text *inpass)
{       char temp[25];       int temp_len;       // Take the "text" variables directly to the "strings"       string
instr(VARDATA(inarg),0,VARSIZE(inarg)-VARHDRSZ);      string passPhrase(VARDATA(inpass),0,VARSIZE(inpass)-VARHDRSZ);
  strcpy(temp,crypt(instr.c_str(),passPhrase.c_str()));       temp_len=strlen(temp);       string
outstr(temp,0,temp_len);              // Set up the outgoing DB "text" variable       int32      new_text_size =
outstr.size()+4;      text       *new_text = (text *) palloc(new_text_size);       memset((void *) new_text, 0,
new_text_size);      VARATT_SIZEP(new_text) = new_text_size;       // Move the variable over from the string
strncpy(VARDATA(new_text),outstr.c_str(),outstr.size());      return new_text;
 
}
----------------------------------------------------------------------------

Be sure to include the header:

#include <unistd.h>             // for unix crypt function  

---------------------------------------------------------------------------

hope that this helps,

Lonnie

--- Brendan Guenther <guenthe8@msu.edu> wrote:
> 
> 
> Peter Eisentraut wrote:
> 
> > The best things might be not to mess with this but call textin() and
> > textout() to convert your data to C strings.
> 
> I can't seem to find documentation for textin() and textout() can somebody
> point
> me in the right direction for this or provide an example.  I'm having similar
> problems that Lonnie is having with VARDATA and I think this might help.  I'm
> trying to link in a C function that makes use of OpenSSL's libcrypto
> functions.
> 
> Basically I just need to hash and verify passwords in the database, so as an
> alternate question, if I'm missing something and there is a better way to do
> password storage let me know.
> 
> Thanks,
> --
> Brendan Guenther
> guenthe8@msu.edu
> 
> 
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
> 
> http://www.postgresql.org/search.mpl


__________________________________________________
Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/


Re: Re: Now for the VARDATA, VARSIZE, and VARHDRSZ stuff

From
Tom Lane
Date:
>> The best things might be not to mess with this but call textin() and
>> textout() to convert your data to C strings.

textin() and textout() are notationally painful to call these days.
Personally I think it's just as easy to do your own manipulation of
text objects.  There are examples at

http://www.postgresql.org/users-lounge/docs/7.1/postgres/xfunc-c.html

--- see the copytext and concat_text() samples in particular.
        regards, tom lane