tgetr.c revision 3f54fd611f536639ec30dd53c48e5ec1897cc7d9
4b0a4c7dea0f67a233dcc42ce9bb18d36de109aeChristian Maeder/***********************************************************************
4b0a4c7dea0f67a233dcc42ce9bb18d36de109aeChristian Maeder* *
11d6ec73ee5550e00cb56b221bdbeb709142e779Christian Maeder* This software is part of the ast package *
97018cf5fa25b494adffd7e9b4e87320dae6bf47Christian Maeder* Copyright (c) 1999-2011 AT&T Intellectual Property *
f3cd81f98592d1dbf301f48af31677a6a0cc666aChristian Maeder* and is licensed under the *
eca29a7be76eb73944ec19b06eda3d6a9e6e543dChristian Maeder* Eclipse Public License, Version 1.0 *
4b0a4c7dea0f67a233dcc42ce9bb18d36de109aeChristian Maeder* by AT&T Intellectual Property *
4b0a4c7dea0f67a233dcc42ce9bb18d36de109aeChristian Maeder* *
4b0a4c7dea0f67a233dcc42ce9bb18d36de109aeChristian Maeder* A copy of the License is available at *
f3a94a197960e548ecd6520bb768cb0d547457bbChristian Maeder* http://www.eclipse.org/org/documents/epl-v10.html *
f3cd81f98592d1dbf301f48af31677a6a0cc666aChristian Maeder* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
f3cd81f98592d1dbf301f48af31677a6a0cc666aChristian Maeder* *
e8ffec0fa3d3061061bdc16e44247b9cf96b050fChristian Maeder* Information and Software Systems Research *
f3cd81f98592d1dbf301f48af31677a6a0cc666aChristian Maeder* AT&T Research *
1c67beb3720d0b84d8d71ee2012166a09be81fbdChristian Maeder* Florham Park NJ *
1c67beb3720d0b84d8d71ee2012166a09be81fbdChristian Maeder* *
1c67beb3720d0b84d8d71ee2012166a09be81fbdChristian Maeder* Glenn Fowler <gsf@research.att.com> *
1c67beb3720d0b84d8d71ee2012166a09be81fbdChristian Maeder* *
23f8d286586ff38a9e73052b2c7c04c62c5c638fChristian Maeder***********************************************************************/
e8ffec0fa3d3061061bdc16e44247b9cf96b050fChristian Maeder#include "sftest.h"
4b0a4c7dea0f67a233dcc42ce9bb18d36de109aeChristian Maeder
1c67beb3720d0b84d8d71ee2012166a09be81fbdChristian Maedertmain()
1c67beb3720d0b84d8d71ee2012166a09be81fbdChristian Maeder{
1c67beb3720d0b84d8d71ee2012166a09be81fbdChristian Maeder Sfio_t *f;
1c67beb3720d0b84d8d71ee2012166a09be81fbdChristian Maeder int n, i;
62925f4a144f45b5ed1e7c841f891d13f51e553dChristian Maeder char buf[50], *s, *string = "111\n222\n333";
715ffaf874309df081d1e1cd8e05073fc1227729Christian Maeder
53301de22afd7190981b363b57c48df86fcb50f7Christian Maeder f = sfopen(NIL(Sfio_t*),string,"s");
cdaff0507c1b7240e2660dbb311f9c4646a6d14aChristian Maeder if(!(s = sfgetr(f,'\n',SF_STRING|SF_LOCKR)) || strcmp(s,"111") != 0)
f3cd81f98592d1dbf301f48af31677a6a0cc666aChristian Maeder terror("sfgetr failed1");
ff9a53595208f532c25ac5168f772f48fd80fdb5Christian Maeder
d17834302eaa101395b4b806cd73670fd864445fChristian Maeder if(sfgetr(f,'\n',0) != NIL(char*))
0f67ca7b0c738a28f6688ba6e96d44d7c14af611Christian Maeder terror("sfgetr should have failed because of locking");
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder sfread(f,s,1);
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder if(!(s = sfgetr(f,'\n',SF_STRING)) || strcmp(s,"222") != 0)
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder terror("sfgetr failed2");
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder
ff9a53595208f532c25ac5168f772f48fd80fdb5Christian Maeder if((s = sfgetr(f,'\n',0)) != NIL(char*))
836e72a3c413366ba9801726f3b249c7791cb9caChristian Maeder terror("sfgetr should have failed because of partial record");
ff9a53595208f532c25ac5168f772f48fd80fdb5Christian Maeder
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder if(!(s = sfgetr(f,0,SF_LASTR)) )
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder terror("sfgetr should have succeeded getting partial record");
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder /* test type == -1 and type == 1 modes */
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder sfseek(f,(Sfoff_t)0,0);
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder if(!(s = sfgetr(f,'\n',1)) || strcmp(s,"111") != 0)
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder terror("sfgetr failed in compatible mode");
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder if(!(s = sfgetr(f,'\n',SF_STRING|SF_LOCKR)) || strcmp(s,"222") != 0)
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder terror("sfgetr failed3");
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder if(sfgetr(f,'\n',1) )
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder terror("sfgetr should have failed due to locking");
53301de22afd7190981b363b57c48df86fcb50f7Christian Maeder sfread(f,s,0);
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder if(sfgetr(f,'\n',1) )
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder terror("sfgetr should have failed because record is incomplete");
ff9a53595208f532c25ac5168f772f48fd80fdb5Christian Maeder
fa45d098e1c9d468f128be9505eb7e5b2705b304Christian Maeder if(!(s = sfgetr(f,0,-1)) || strcmp(s,"333") != 0)
25612a7b3ce708909298d5426406592473880a20Christian Maeder terror("sfgetr failed in getting last partial record");
0b75edd800cef1a3b695f24df39d13f1ceace257Christian Maeder
0b75edd800cef1a3b695f24df39d13f1ceace257Christian Maeder if(!(f = sftmp(0)) )
c1db3d36c29a6324745a86dbcba18b8e4cd9f338Christian Maeder terror("Can't open temporary stream");
18b709ce961d68328da768318dcc70067f066d86Christian Maeder for(n = 0; n < 10; ++n) /* each record is 100 bytes */
18b709ce961d68328da768318dcc70067f066d86Christian Maeder { for(i = 0; i < 100; ++i)
18b709ce961d68328da768318dcc70067f066d86Christian Maeder sfputc(f, 'a');
18b709ce961d68328da768318dcc70067f066d86Christian Maeder sfputc(f,'\n');
18b709ce961d68328da768318dcc70067f066d86Christian Maeder }
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder sfseek(f,(Sfoff_t)0,0);
18b709ce961d68328da768318dcc70067f066d86Christian Maeder sfsetbuf(f, buf, 50);
18b709ce961d68328da768318dcc70067f066d86Christian Maeder if(!(s = sfgetr(f, '\n', 1)))
18b709ce961d68328da768318dcc70067f066d86Christian Maeder terror("Can't get a record");
b814fecd0a2dbdeae62402903783d08e4206b4d2Christian Maeder
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder n = 80;
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder sfmaxr(n, 1); /* set maximum record size */
18b709ce961d68328da768318dcc70067f066d86Christian Maeder if((i = sfmaxr(0, 0)) != n)
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder terror("maxr is %d, expected %d", i, n);
18b709ce961d68328da768318dcc70067f066d86Christian Maeder if((s = sfgetr(f, '\n', 1)) != 0)
9c5b1136299d9052e4e995614a3a36a051a2682fChristian Maeder terror("Shouldn't have gotten a record");
18b709ce961d68328da768318dcc70067f066d86Christian Maeder
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder n = 0;
36c6cc568751e4235502cfee00ba7b597dae78dcChristian Maeder sfmaxr(n, 1); /* no record size limit */
9c5b1136299d9052e4e995614a3a36a051a2682fChristian Maeder if((i = sfmaxr(0, 0)) != n)
9c5b1136299d9052e4e995614a3a36a051a2682fChristian Maeder terror("maxr is %d, expected %d", i, n);
ff9a53595208f532c25ac5168f772f48fd80fdb5Christian Maeder if(!(s = sfgetr(f, '\n', 1)))
76fa667489c5e0868ac68de9f0253ac10f73d0b5Christian Maeder terror("Can't get a record");
9c5b1136299d9052e4e995614a3a36a051a2682fChristian Maeder if(!(s = sfgetr(f, '\n', 1)))
aff01ee50b66032469c232e00c945d1fd4f57d1bChristian Maeder terror("Can't get a record");
797f811e57952d59e73b8cd03b667eef276db972Christian Maeder
11d6ec73ee5550e00cb56b221bdbeb709142e779Christian Maeder texit(0);
fe5dbb45b6a8abf34375b4bc5f2a81cda664c0e4Christian Maeder}
797f811e57952d59e73b8cd03b667eef276db972Christian Maeder