CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Bulding Strings for URLs - SOLVED

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

Bulding Strings for URLs - SOLVED
PostPosted: Mon May 23, 2016 6:19 pm     Reply with quote

Hi All,

Im looking for more elegant ways to do 2 things:
(I have a working solution but im not happy)

1) getting string length for strings larger than 255 (ie: 400 chars)
Current solution:
Code:
while(Post_Buffer[length]!='\0')length++;


2) append a single character to a string as follows:

I need to encode certain caracters in UTF-8 for a URL, specifically the limit characters of a JSON Object ('{' ',' ':' '}')
its the default case that i dont like... feel free to suggest optimizations where you see fit.
Current solution:
Code:
void UTF8_ENCODE(char symbol)
{
   char Temp_string[]="x";
   Temp_string[0]=symbol;
   switch(symbol)
   {
      case '{':
      {
         strcat(Post_Buffer,"%7B");
         break;
      }
      case '}':
      {
         strcat(Post_Buffer,"%7D");
         break;
      }
      case '"':
      {
         strcat(Post_Buffer,"%22");
         break;
      }
      case ':':
      {
         strcat(Post_Buffer,"%3A");
         break;
      }
      case ',':
      {
         strcat(Post_Buffer,"%2C");
         break;
      }
      default:
      {
         strcat(Post_Buffer,Temp_string);
         break;
      }
   }
}


Thanks,

G.
_________________
CCS PCM 5.078 & CCS PCH 5.093


Last edited by Gabriel on Tue May 24, 2016 2:37 pm; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19339

View user's profile Send private message

PostPosted: Tue May 24, 2016 12:47 am     Reply with quote

For the length. What chip?.

Key is that the supplied strlen function, actually returns a 'size_t' variable. If you look in stddef.h, we have:
Code:

#if defined(__PCB__) || defined(__PCM__)
#define size_t unsigned int8
#else
#define size_t unsigned int16
#endif


So on anything other then PCB/PCM, it returns an int16.

So PIC18 and up, the supplied strlen function will happily handle strings over 256 characters.

If you are working with utf8, have you looked as the readme.txt with the current compiler. The _unicode macro may be useful for you.

Why not just get the length of the string once, and append?.
strcat is itself calling strlen to know 'where' the target has to go.

A lot depends on how much you are using this?. If this is being used to build an output string, and is being repeatedly called, then far more efficient to maintain a 'target' position yourself, and write the data to this position. Otherwise every single output, involves re-calculating the string length.

Then (for instance), the default would become:
Code:

      default:
      {
         Post_buffer[target++]=symbol;
         break;
      }


Remember 'strings' in C, are just arrays of characters (with a NULL terminator). Don't get 'hooked' on using string functions, just treat the array as an array, write what you want to it, and when you finish add one terminator.
Using string functions, every little bit you add, involves looping through the whole array to find the terminator, then copying the bytes, then adding a terminator, and then repeating this each time. Very laborious.....
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Tue May 24, 2016 2:37 pm     Reply with quote

Thanks for the explanation on strlen(), some insight was gained.
I checked why i was getting an error since it is int16.

I was printing strlen() as %u unstead of %Lu....
sigh...

reusing old code "known" to work.... jajajaja that went well!

i was not aware of a _unicode macro!... ill check it out.


G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group