view libfaim/aim_util.c @ 312:3069be4c291e

[gaim-migrate @ 322] I don't know why I did this. I have homework due in 15 hours that I haven't started yet, and it's in a language I don't know and it's a project I don't understand. If my teacher knew about this, he would be pissed. He looks pissed all the time, even when he's not. When he smiles he looks devilish. Maybe I only think that because literally half the class flunked the midterm. I am not joking about that. More people got F's than A, B, and C combined. It's 2 am and the homework's due at 5 tomorrow so what do I do? Get chat to work. Wow. That's going to look good on my resume. "Why did you flunk this class?" "Because I was getting chat in Instant Messenger to work." Not that that's not something to be proud of, but I wonder which is more important to employers. The big battle, experience versus education. Just because you got good grades in college doesn't mean you're smarter than someone who flunked, it just means you put in the effort necessary to get a better grade and the other person didn't. Maybe the person who flunked was working on real honest-to-god actually *used* software, as opposed to some stupid tree that only gets used for a fringe branch of computer science that doesn't offer much more than a normal heap or binary search tree offers. Maybe the person was out there reverse-engineering protocols and allowing cross- platform communication to occur, creating interoperability and causing a greater demand not only for the product, but for the platform it runs on! Given the choices, who would you pick? Someone who was told how to code a tree and managed to get it to work, or someone who increases your userbase and marketability? Enough of my rant for a while. I've had waaaaay too much sugar (gummy candy is deadly). committer: Tailor Script <tailor@pidgin.im>
author Eric Warmenhoven <eric@warmenhoven.org>
date Fri, 02 Jun 2000 09:11:48 +0000
parents 0f14e6d8a51b
children 595ac7759563
line wrap: on
line source

/*
 *
 *
 *
 */

#include <faim/aim.h>
#include <ctype.h>

#define AIMUTIL_USEMACROS

#ifdef AIMUTIL_USEMACROS
/* macros in faim/aim.h */
#else
inline int aimutil_put8(u_char *buf, u_char data)
{
  buf[0] = (u_char)data&0xff;
  return 1;
}

inline u_char aimutil_get8(u_char *buf)
{
  return buf[0];
}

/*
 * Endian-ness issues here?
 */
inline int aimutil_put16(u_char *buf, u_short data)
{
  buf[0] = (u_char)(data>>8)&0xff;
  buf[1] = (u_char)(data)&0xff;
  return 2;
}

inline u_short aimutil_get16(u_char *buf)
{
  u_short val;
  val = (buf[0] << 8) & 0xff00;
  val+= (buf[1]) & 0xff;
  return val;
}

inline int aimutil_put32(u_char *buf, u_long data)
{
  buf[0] = (u_char)(data>>24)&0xff;
  buf[1] = (u_char)(data>>16)&0xff;
  buf[2] = (u_char)(data>>8)&0xff;
  buf[3] = (u_char)(data)&0xff;
  return 4;
}

inline u_long aimutil_get32(u_char *buf)
{
  u_long val;
  val = (buf[0] << 24) & 0xff000000;
  val+= (buf[1] << 16) & 0x00ff0000;
  val+= (buf[2] <<  8) & 0x0000ff00;
  val+= (buf[3]      ) & 0x000000ff;
  return val;
}
#endif /* AIMUTIL_USEMACROS */

inline int aimutil_putstr(u_char *dest, const u_char *src, int len)
{
  memcpy(dest, src, len);
  return len;
}

/*
 * Tokenizing functions.  Used to portably replace strtok/sep.
 *   -- DMP.
 *
 */
int aimutil_tokslen(char *toSearch, int index, char dl)
{
  int curCount = 1;
  char *next;
  char *last;
  int toReturn;

  last = toSearch;
  next = strchr(toSearch, dl);
  
  while(curCount < index && next != NULL)
    {
      curCount++;
      last = next + 1;
      next = strchr(last, dl);
    }
  
  if ((curCount < index) || (next == NULL))
    toReturn = strlen(toSearch) - (curCount - 1);
  else
    toReturn = next - toSearch - (curCount - 1);

  return toReturn;
}

int aimutil_itemcnt(char *toSearch, char dl)
{
  int curCount;
  char *next;
  
  curCount = 1;
  
  next = strchr(toSearch, dl);
  
  while(next != NULL)
    {
      curCount++;
      next = strchr(next + 1, dl);
    }
  
  return curCount;
}

char *aimutil_itemidx(char *toSearch, int index, char dl)
{
  int curCount;
  char *next;
  char *last;
  char *toReturn;
  
  curCount = 0;
  
  last = toSearch;
  next = strchr(toSearch, dl);
  
  while(curCount < index && next != NULL)
    {
      curCount++;
      last = next + 1;
      next = strchr(last, dl);
    }
  
  if (curCount < index)
    {
      toReturn = malloc(sizeof(char));
      *toReturn = '\0';
    }
  next = strchr(last, dl);
  
  if (curCount < index)
    {
      toReturn = malloc(sizeof(char));
      *toReturn = '\0';
    }
  else
    {
      if (next == NULL)
	{
	  toReturn = malloc((strlen(last) + 1) * sizeof(char));
	  strcpy(toReturn, last);
	}
      else
	{
	  toReturn = malloc((next - last + 1) * sizeof(char));
	  memcpy(toReturn, last, (next - last));
	  toReturn[next - last] = '\0';
	}
    }
  return toReturn;
}

/*
 * int snlen(const char *)
 * 
 * This takes a screen name and returns its length without
 * spaces.  If there are no spaces in the SN, then the 
 * return is equal to that of strlen().
 *
 */
int aim_snlen(const char *sn)
{
  int i = 0;
  const char *curPtr = NULL;

  if (!sn)
    return 0;

  curPtr = sn;
  while ( (*curPtr) != (char) NULL) {
      if ((*curPtr) != ' ')
	i++;
      curPtr++;
    }

  return i;
}

/*
 * int sncmp(const char *, const char *)
 *
 * This takes two screen names and compares them using the rules
 * on screen names for AIM/AOL.  Mainly, this means case and space
 * insensitivity (all case differences and spacing differences are
 * ignored).
 *
 * Return: 0 if equal
 *     non-0 if different
 *
 */

int aim_sncmp(const char *sn1, const char *sn2)
{
  const char *curPtr1 = NULL, *curPtr2 = NULL;

  if (aim_snlen(sn1) != aim_snlen(sn2))
    return 1;

  curPtr1 = sn1;
  curPtr2 = sn2;
  while ( (*curPtr1 != (char) NULL) && (*curPtr2 != (char) NULL) ) {
    if ( (*curPtr1 == ' ') || (*curPtr2 == ' ') ) {
      if (*curPtr1 == ' ')
	curPtr1++;
      if (*curPtr2 == ' ')
	curPtr2++;
    } else {
      if ( toupper(*curPtr1) != toupper(*curPtr2))
	return 1;
      curPtr1++;
      curPtr2++;
    }
  }

  return 0;
}