File: text.c

package info (click to toggle)
normalize 0.7.2-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,804 kB
  • ctags: 719
  • sloc: ansic: 8,244; sh: 7,534; perl: 454; makefile: 177; sed: 16
file content (149 lines) | stat: -rw-r--r-- 3,168 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* Copyright (C) 2001 Chris Vaill
   This file is part of nid3lib.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

/*
 * Text frame (i.e. 'T???') manipulation routines
 */

#define _POSIX_C_SOURCE 2

#include "config.h"

#if STDC_HEADERS
# include <stdlib.h>
# include <string.h>
#else
# if HAVE_STDLIB_H
#  include <stdlib.h>
# endif
# if HAVE_STRING_H
#  include <string.h>
# else
#  ifndef HAVE_MEMCPY
#   define memcpy(d,s,n) bcopy((s),(d),(n))
#   define memmove(d,s,n) bcopy((s),(d),(n))
#  endif
# endif
#endif

#include "nid3P.h"


int
id3_frame_text_enc(id3_frame_t f)
{
  unsigned char *buf = id3_frame_get_raw(f);

  if (buf) {
    switch (buf[0]) {
    case ID3_TEXT_ISO:
    case ID3_TEXT_UTF16:
    case ID3_TEXT_UTF16BE:
    case ID3_TEXT_UTF8:
      return (int)buf[0];
    }
  }

  return -1;
}

/*
 * Returns each text string in turn, NULL when there are no more.
 */
char *
id3_frame_text(id3_frame_t f)
{
  unsigned char *buf = id3_frame_get_raw(f);
  char *retval = NULL;
  int i;

  if (buf == NULL)
    return NULL;

  return buf + 1;

  retval = f->curr_txt;

  /* advance the f->curr_txt pointer */
  if (f->curr_txt == NULL) {
    f->curr_txt = buf + 1; /* start over */
  } else {
    i = f->curr_txt - (char *)buf;
    switch (buf[0]) {
    case ID3_TEXT_UTF16:
    case ID3_TEXT_UTF16BE:
      while (i <= f->sz && !(buf[i] == '\0' && buf[i+1] == '\0'))
	i += 2;
      i += 2;
      break;
    case ID3_TEXT_UTF8:
    case ID3_TEXT_ISO:
      while (i <= f->sz && buf[i] != '\0')
	i++;
      i++;
      break;
    }
    if (i > f->sz)
      f->curr_txt = NULL;
    else
      f->curr_txt = buf + i;
  }

  return retval;
}

/* FIXME: add ability to manipulate individual fields */
id3_frame_t
id3_add_text_frame(id3_t tag, const char *id, const char *text, int encoding)
{
  id3_frame_t fr;
  int len;

  fr = id3_frame_add(tag, id);
  if (fr == NULL)
    return NULL;

  len = 0;
  switch (encoding) {
  case ID3_TEXT_UTF16:
  case ID3_TEXT_UTF16BE:
    while (!(text[len] == '\0' && text[len+1] == '\0'))
      len += 2;
    break;
  case ID3_TEXT_UTF8:
  case ID3_TEXT_ISO:
    while (text[len] != '\0')
      len++;
    break;
  default:
    _id3_frame_destroy(fr);
    return NULL;
  }

  if (fr->data)
    free(fr->data);
  fr->sz = len + 1; /* +1 for encoding byte */
  fr->data = (unsigned char *)calloc(fr->sz + 2, 1); /* +2 for nul chars */
  if (fr->data == NULL) {
    _id3_frame_destroy(fr);
    return NULL;
  }
  fr->data[0] = encoding;
  memcpy(fr->data + 1, text, len);

  return fr;
}