66 * Author: Paul Cercueil <paul.cercueil@analog.com> 
77 */ 
88
9- #include  "iio-private.h" 
10- #include  "sort.h" 
11- 
12- #include  <inttypes.h> 
139#include  <errno.h> 
10+ #include  <inttypes.h> 
1411#include  <stdio.h> 
1512#include  <string.h> 
1613
17- static  inline  unsigned int   attr_index (const  struct  iio_attr_list  * list ,
18- 				      const  struct  iio_attr  * attr )
14+ #include  "iio-private.h" 
15+ #include  "sort.h" 
16+ 
17+ static  inline  unsigned int   attr_index (const  struct  iio_attr_list  * list , const  struct  iio_attr  * attr )
1918{
2019	uintptr_t  diff  =  (uintptr_t )attr  -  (uintptr_t )list -> attrs ;
2120	return  (unsigned int  )diff  / sizeof (* attr );
@@ -28,7 +27,7 @@ ssize_t iio_attr_read_raw(const struct iio_attr *attr, char *dst, size_t len)
2827
2928	if  (attr -> type  ==  IIO_ATTR_TYPE_CONTEXT ) {
3029		idx  =  attr_index (& attr -> iio .ctx -> attrlist , attr );
31- 		return  (ssize_t )  iio_strlcpy (dst , attr -> iio .ctx -> values [idx ], len );
30+ 		return  (ssize_t )iio_strlcpy (dst , attr -> iio .ctx -> values [idx ], len );
3231	}
3332
3433	if  (dev -> ctx -> ops -> read_attr )
@@ -45,7 +44,7 @@ int iio_attr_read_longlong(const struct iio_attr *attr, long long *val)
4544
4645	ret  =  iio_attr_read_raw (attr , buf , sizeof (buf ));
4746	if  (ret  <  0 )
48- 		return  (int )  ret ;
47+ 		return  (int )ret ;
4948
5049	errno  =  0 ;
5150	value  =  strtoll (buf , & end , 0 );
@@ -75,13 +74,12 @@ int iio_attr_read_double(const struct iio_attr *attr, double *val)
7574
7675	ret  =  iio_attr_read_raw (attr , buf , sizeof (buf ));
7776	if  (ret  <  0 )
78- 		return  (int )  ret ;
77+ 		return  (int )ret ;
7978
8079	return  read_double (buf , val );
8180}
8281
83- ssize_t  iio_attr_write_raw (const  struct  iio_attr  * attr ,
84- 			   const  void  * src , size_t  len )
82+ ssize_t  iio_attr_write_raw (const  struct  iio_attr  * attr , const  void  * src , size_t  len )
8583{
8684	const  struct  iio_device  * dev  =  iio_attr_get_device (attr );
8785
@@ -108,18 +106,18 @@ int iio_attr_write_longlong(const struct iio_attr *attr, long long val)
108106	len  =  iio_snprintf (buf , sizeof (buf ), "%lld" , val );
109107	ret  =  iio_attr_write_raw (attr , buf , len  +  1 );
110108
111- 	return  (int )  (ret  <  0  ? ret  : 0 );
109+ 	return  (int )(ret  <  0  ? ret  : 0 );
112110}
113111
114112int  iio_attr_write_double (const  struct  iio_attr  * attr , double  val )
115113{
116114	ssize_t  ret ;
117115	char  buf [1024 ];
118116
119- 	ret  =  (ssize_t )  write_double (buf , sizeof (buf ), val );
117+ 	ret  =  (ssize_t )write_double (buf , sizeof (buf ), val );
120118	if  (!ret )
121119		ret  =  iio_attr_write_string (attr , buf );
122- 	return  (int )  (ret  <  0  ? ret  : 0 );
120+ 	return  (int )(ret  <  0  ? ret  : 0 );
123121}
124122
125123int  iio_attr_write_bool (const  struct  iio_attr  * attr , bool  val )
@@ -131,20 +129,18 @@ int iio_attr_write_bool(const struct iio_attr *attr, bool val)
131129	else 
132130		ret  =  iio_attr_write_raw (attr , "0" , 2 );
133131
134- 	return  (int )  (ret  <  0  ? ret  : 0 );
132+ 	return  (int )(ret  <  0  ? ret  : 0 );
135133}
136134
137- const  struct  iio_attr  * 
138- iio_attr_get (const  struct  iio_attr_list  * attrs , unsigned int   idx )
135+ const  struct  iio_attr  * iio_attr_get (const  struct  iio_attr_list  * attrs , unsigned int   idx )
139136{
140137	if  (idx  >= attrs -> num )
141138		return  NULL ;
142139
143140	return  & attrs -> attrs [idx ];
144141}
145142
146- const  struct  iio_attr  * 
147- iio_attr_find (const  struct  iio_attr_list  * attrs , const  char  * name )
143+ const  struct  iio_attr  * iio_attr_find (const  struct  iio_attr_list  * attrs , const  char  * name )
148144{
149145	unsigned int   i ;
150146
@@ -155,20 +151,17 @@ iio_attr_find(const struct iio_attr_list *attrs, const char *name)
155151	return  NULL ;
156152}
157153
158- const  char  * 
159- iio_attr_get_name (const  struct  iio_attr  * attr )
154+ const  char  * iio_attr_get_name (const  struct  iio_attr  * attr )
160155{
161156	return  attr -> name ;
162157}
163158
164- const  char  * 
165- iio_attr_get_filename (const  struct  iio_attr  * attr )
159+ const  char  * iio_attr_get_filename (const  struct  iio_attr  * attr )
166160{
167161	return  attr -> filename ;
168162}
169163
170- const  char  * 
171- iio_attr_get_static_value (const  struct  iio_attr  * attr )
164+ const  char  * iio_attr_get_static_value (const  struct  iio_attr  * attr )
172165{
173166	unsigned int   idx ;
174167
@@ -181,9 +174,8 @@ iio_attr_get_static_value(const struct iio_attr *attr)
181174	}
182175}
183176
184- int  iio_add_attr (union  iio_pointer  p , struct  iio_attr_list  * attrs ,
185- 		 const  char  * name , const  char  * filename ,
186- 		 enum  iio_attr_type  type )
177+ int  iio_add_attr (union  iio_pointer  p , struct  iio_attr_list  * attrs , const  char  * name ,
178+ 		const  char  * filename , enum  iio_attr_type  type )
187179{
188180	struct  iio_attr  * attr ;
189181
@@ -206,7 +198,7 @@ int iio_add_attr(union iio_pointer p, struct iio_attr_list *attrs,
206198		attr [attrs -> num ].filename  =  iio_strdup (filename );
207199
208200		if  (!attr [attrs -> num ].filename ) {
209- 			free ((char  * )  attr [attrs -> num ].name );
201+ 			free ((char  * )attr [attrs -> num ].name );
210202			return  - ENOMEM ;
211203		}
212204	} else  {
@@ -220,16 +212,17 @@ int iio_add_attr(union iio_pointer p, struct iio_attr_list *attrs,
220212	return  0 ;
221213}
222214
223- static  const  char  *   const  attr_type_string [] =  {
215+ static  const  char  * const  attr_type_string [] =  {
224216	"" ,
225217	" debug" ,
226218	" buffer" ,
227219};
228220
229- int  iio_device_add_attr (struct  iio_device  * dev ,
230- 			const  char  * name , enum  iio_attr_type  type )
221+ int  iio_device_add_attr (struct  iio_device  * dev , const  char  * name , enum  iio_attr_type  type )
231222{
232- 	union  iio_pointer  p  =  { .dev  =  dev , };
223+ 	union  iio_pointer  p  =  {
224+ 		.dev  =  dev ,
225+ 	};
233226	int  ret ;
234227
235228	ret  =  iio_add_attr (p , & dev -> attrlist [type ], name , NULL , type );
@@ -240,26 +233,27 @@ int iio_device_add_attr(struct iio_device *dev,
240233	return  0 ;
241234}
242235
243- int  iio_channel_add_attr (struct  iio_channel  * chn ,
244- 			 const  char  * name , const  char  * filename )
236+ int  iio_channel_add_attr (struct  iio_channel  * chn , const  char  * name , const  char  * filename )
245237{
246- 	union  iio_pointer  p  =  { .chn  =  chn , };
238+ 	union  iio_pointer  p  =  {
239+ 		.chn  =  chn ,
240+ 	};
247241	int  ret ;
248242
249- 	ret  =  iio_add_attr (p , & chn -> attrlist , name , filename ,
250- 			   IIO_ATTR_TYPE_CHANNEL );
243+ 	ret  =  iio_add_attr (p , & chn -> attrlist , name , filename , IIO_ATTR_TYPE_CHANNEL );
251244	if  (ret  <  0 )
252245		return  ret ;
253246
254247	chn_dbg (chn , "Added attr \'%s\' (\'%s\')\n" , name , filename );
255248	return  0 ;
256249}
257250
258- int  iio_context_add_attr (struct  iio_context  * ctx ,
259- 			 const  char  * key , const  char  * value )
251+ int  iio_context_add_attr (struct  iio_context  * ctx , const  char  * key , const  char  * value )
260252{
261253	char  * * values , * new_val ;
262- 	union  iio_pointer  p  =  { .ctx  =  ctx , };
254+ 	union  iio_pointer  p  =  {
255+ 		.ctx  =  ctx ,
256+ 	};
263257	unsigned int   i ;
264258	int  ret ;
265259
@@ -268,15 +262,14 @@ int iio_context_add_attr(struct iio_context *ctx,
268262		return  - ENOMEM ;
269263
270264	for  (i  =  0 ; i  <  ctx -> attrlist .num ; i ++ ) {
271- 		if (!strcmp (ctx -> attrlist .attrs [i ].name , key )) {
265+ 		if   (!strcmp (ctx -> attrlist .attrs [i ].name , key )) {
272266			free (ctx -> values [i ]);
273267			ctx -> values [i ] =  new_val ;
274268			return  0 ;
275269		}
276270	}
277271
278- 	values  =  realloc (ctx -> values ,
279- 			(ctx -> attrlist .num  +  1 ) *  sizeof (* ctx -> values ));
272+ 	values  =  realloc (ctx -> values , (ctx -> attrlist .num  +  1 ) *  sizeof (* ctx -> values ));
280273	if  (!values ) {
281274		free (new_val );
282275		return  - ENOMEM ;
@@ -306,7 +299,7 @@ int iio_context_add_attr(struct iio_context *ctx,
306299	causing subsequent attributes to shift forward as they were already sorted */ 
307300	if  (new_idx  !=  ctx -> attrlist .num  -  1 ) {
308301		memmove (& ctx -> values [new_idx  +  1 ], & ctx -> values [new_idx ],
309- 			(ctx -> attrlist .num  -  new_idx  -  1 ) *  sizeof (* ctx -> values ));
302+ 				 (ctx -> attrlist .num  -  new_idx  -  1 ) *  sizeof (* ctx -> values ));
310303		ctx -> values [new_idx ] =  new_val ;
311304	}
312305
@@ -316,9 +309,9 @@ int iio_context_add_attr(struct iio_context *ctx,
316309void  iio_free_attr_data (struct  iio_attr  * attr )
317310{
318311	if  (attr -> filename  !=  attr -> name )
319- 		free ((char  * )  attr -> filename );
312+ 		free ((char  * )attr -> filename );
320313
321- 	free ((char  * )  attr -> name );
314+ 	free ((char  * )attr -> name );
322315
323316	attr -> filename  =  NULL ;
324317	attr -> name  =  NULL ;
@@ -350,11 +343,12 @@ int iio_attr_get_range(const struct iio_attr *attr, double *min, double *step, d
350343	char  buf [MAX_ATTR_VALUE ];
351344	ret  =  iio_attr_read_raw (attr , buf , sizeof (buf ));
352345	if  (ret  <  0 )
353- 		return  (int )  ret ;
346+ 		return  (int )ret ;
354347
355- 	// Expect format: [min step max] 
348+ 		 // Expect format: [min step max] 
356349#if  defined(_MSC_VER )
357- 	n  =  iio_sscanf (buf , " [ %lf %lf %lf %c" , & lmin , & lstep , & lmax , & extra , (unsigned int  )sizeof (extra ));
350+ 	n  =  iio_sscanf (buf , " [ %lf %lf %lf %c" , & lmin , & lstep , & lmax , & extra ,
351+ 			(unsigned int  )sizeof (extra ));
358352#else 
359353	n  =  iio_sscanf (buf , " [ %lf %lf %lf %c" , & lmin , & lstep , & lmax , & extra );
360354#endif 
@@ -418,7 +412,7 @@ int iio_attr_get_available(const struct iio_attr *attr, char ***list, size_t *co
418412	* list  =  local_list ;
419413	* count  =  n ;
420414
421-      return  0 ;
415+ 	 return  0 ;
422416}
423417
424418void  iio_available_list_free (char  * * list , size_t  count )
@@ -433,8 +427,8 @@ void iio_available_list_free(char **list, size_t count)
433427	}
434428}
435429
436- int  iio_attr_get_available_buf (const   struct   iio_attr   * attr ,  char   * buf , 
437- 	size_t  buflen , char  * * list , size_t  * count )
430+ int  iio_attr_get_available_buf (
431+ 		 const   struct   iio_attr   * attr ,  char   * buf ,  size_t  buflen , char  * * list , size_t  * count )
438432{
439433	ssize_t  ret ;
440434	size_t  n  =  0 , max  =  (size_t )-1 ;
0 commit comments