U E D R S I H C RSS
ID
Password
Join
์„œ๋กœ ์‚ฌ๋ž‘ํ•˜๋Š” ์‚ฌ๋žŒ์ด ๋‘ ์‚ฌ๋žŒ ์‚ฌ์ด์— ํ•œ์ˆœ๊ฐ„์ด๋ผ๋„ ์‹œ๊ฐ„์ด ๋ผ์–ด๋“ค๊ฒŒ ๋‚ด๋ฒ„๋ ค๋‘๋ฉด, ๊ทธ๊ฒƒ์€ ์ž๋ผ์„œ ํ•œ ๋‹ฌ์ด ๋˜๊ณ , ์ผ๋…„์ด ๋˜๊ณ , ํ•œ ์„ธ๊ธฐ๊ฐ€ ๋œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ๋„ˆ๋ฌด ๋Šฆ์–ด์ง„๋‹ค. โ€•์žฅ ์ง€๋กœ๋‘(ํ”„๋ž‘์Šค ๊ทน์ž‘๊ฐ€, 1882โˆผ1944)

๏ปฟ * ์›๋ฌธ๋งํฌ : [http]http://www.metalshell.com/view/source/126/
  • "practical C programming" ์•ˆ์— ์žˆ๋Š” ์†Œ์Šค๋ผ๋„ค์š”. ๊ทธ๋Œ€๋กœ ํผ์˜ต๋‹ˆ๋‹ค. strtok() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋Š” embedding ํ™˜๊ฒฝ์—์„œ์˜ ์ฝ”๋”ฉ๋•Œ ์œ ์šฉํ•  ์ˆ˜ ์žˆ์„ ๋“ฏ.
/* Custom String Tokenizer - by mind@metalshell.com
 *
 * This example is more of an example on predetermined strings such as
 * reading in a configuration file and splitting the variable from the value
 * in or around quotation marks or equal signs.
 *
 * See below for porting this to read a configuration file or similar
 *
 * http://www.metalshell.com
 */
#include <stdio.h>
#include <string.h>

int RunExample();
int main()
{
  int  x, i;
  char  tmp[]="writing:your:own:string!tokenizer:yay";
  char  tnp[strlen(tmp)];

  /* See below
   *
   * int  z;
   * char  exempt[]="!@:$.,";
   * char  tmp[]="writing,your!own@str.ingto$kenize:ryay";
   *
   */

  /* Uncomment the next two lines to run the second example */

  // RunExample();

  // return 1;


  /* zero out our temp string */
  bzero(tnp, sizeof(tnp));

  for (x = 0, i = 0; x < strlen(tmp); x++)
  {
    /* Use this if you want to parse segments from
     * numerous splitters.
     *
     * for (z = 0; z < strlen(exempt); z++)
     * {
     *    if (tmp[x] == exempt[z])
     *    {
     *      .....
     *    }
     * }
     *
     */

    /* check for end of segment */
    if (tmp[x] == ':' || tmp[x] == '!')
    {
      printf("Extracted segment: %s\n", tnp);

      /* zero out our temp string */
      bzero(tnp, sizeof(tnp));

      /* reset our writing point back to the beginning */
      i = 0;

      /* start from the beginning of our loop
       * this is needed so the loop wont finish
       * and write : or ! to tnp[0] */
      continue;
    }

    /* write our character at position tnp[i] from tmp[x] */
    tnp[i] = tmp[x];

    /* increase our temp string's position */
    i++;
  }
  return 1;
}

/* Extraction method; */
void Extract(char *str)
{
  int    x, i, y;
  char  tmp[strlen(str)];

  /* zero out our temp string */
  bzero(tmp, sizeof(tmp));

  for (x = 0, i = 0, y = 0; x < strlen(str); x++)
  {
    /* check to see if the last loop reached the end of
     * our first segment. */
    if (!y)
    {
      /* nope.. check to see if this loop does */
      if (str[x] == '=')
      {
        y = 1;

        /* First segment passed; continue back to a new loop
         * and search for the next segment */
        continue;
      }

      /* keep looping back until we've gone through the
       * first segment (skipping) */
      continue;
    }

    /* check to see if we have passed the first segment */
    if (y == 1)
    {
      /* check if we are at the start of our valued segment */
      if (str[x] == 34)
      {
        /* we are.. setup y so we can start writing our value */
        y = 2;

        /* continue back to the beginning so we dont write '"' */
        continue;
      }

      /* '"' hasn't been reached yet continue looping.. */
      continue;
    }

    /* check to see if our value is done with an ending '"' */
    if (y == 2 && str[x] == 34)
    {
      /* Everything looks good; zero out our string and
       * start writing our temp string to our real string */
      bzero(str, strlen(tmp) + 1);
      for (x = 0; x < strlen(tmp); x++)
        str[x] = tmp[x];

      /* end loop */
      break;
    }

    /* write to our temp string */
    tmp[i] = str[x];
    i++;
  }
}

/*
 * This section will show the ways you can use this example to
 * read in a confiruation file and extract what you want.
 *
 * user="mind"
 * pass="metalshell"
 *
 */
int RunExample()
{
  char  str1[]="user=\"mind\"";
  char  str2[]="pass=\"metalshell\"";

  Extract(str1);
  Extract(str2);

  printf("User: %s\n", str1);
  printf("Pass: %s\n", str2);

  return 1;
}

/*
 * Since this example is using predetermined strings, I strongly suggest
 * if you use this code to read in a configuration file; Please add checks
 * on the string your reading in before using Extract(). This example is
 * safe to use and has been tested for stability but could still cause issues.
 *
 * If you have any question's or want help adding checks to this please visit:
 * http://www.metalshell.com/forum
 *
 */

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2010-10-28 12:42:52
Processing time 0.3431 sec