---
name: "njb_playlistapi"
section: "3"
platform: "linux"
package: "libnjb-doc"
version: "2.2.5-4.3"
release: "Debian 6.0 (Squeeze)"
platforms: ["linux"]
url: "https://new.carta.tech/njb_playlistapi"
---

# njb\_playlistapi(3)

## NAME

The playlist manipulation API -

### Defines

\#define **NJB\_PL\_END**   0\
 \#define **NJB\_PL\_START**   1\

### Functions

void **NJB\_Reset\_Get\_Playlist** (**njb\_t** \*njb)\
 **njb\_playlist\_t** \* **NJB\_Get\_Playlist** (**njb\_t** \*njb)\
 int **NJB\_Delete\_Playlist** (**njb\_t** \*njb, u\_int32\_t plid)\
 int **NJB\_Update\_Playlist** (**njb\_t** \*njb, **njb\_playlist\_t** \*pl)\
 **njb\_playlist\_t** \* **NJB\_Playlist\_New** (void)\
 void **NJB\_Playlist\_Destroy** (**njb\_playlist\_t** \*pl)\
 void **NJB\_Playlist\_Addtrack** (**njb\_playlist\_t** \*pl, **njb\_playlist\_track\_t** \*track, unsigned int pos)\
 void **NJB\_Playlist\_Reset\_Gettrack** (**njb\_playlist\_t** \*pl)\
 **njb\_playlist\_track\_t** \* **NJB\_Playlist\_Gettrack** (**njb\_playlist\_t** \*pl)\
 int **NJB\_Playlist\_Set\_Name** (**njb\_playlist\_t** \*pl, const char \*name)\
 void **NJB\_Playlist\_Deltrack** (**njb\_playlist\_t** \*pl, unsigned int pos)\
 void **NJB\_Playlist\_Deltrack\_TrackID** (**njb\_playlist\_t** \*pl, u\_int32\_t trackid)\
 **njb\_playlist\_track\_t** \* **NJB\_Playlist\_Track\_New** (u\_int32\_t trackid)\
 void **NJB\_Playlist\_Track\_Destroy** (**njb\_playlist\_track\_t** \*track)\

## Function Documentation

### int NJB\_Delete\_Playlist (njb\_t \* njb, u\_int32\_t plid) This deletes a playlist from the device.

**Parameters:**

*njb* a pointer to the `njb_t` object to delete the playlist from \
 *plid* the playlist ID as reported from **NJB\_Get\_Playlist().**

**Returns:**

**0 on success, -1 on failure.**

**Examples:**  **pl.c.**

**References njb\_struct::device\_type, and NJB\_DEVICE\_NJB1.**

### njb\_playlist\_t\* NJB\_Get\_Playlist (njb\_t \* njb) This gets a playlist from the device. The device should first be rewound using the NJB\_Reset\_Get\_Playlist() function. The playlists are newly allocated and should be destroyed with NJB\_Playlist\_Destroy() after use.

**Parameters:**

*njb* **a pointer to the** `njb_t` **object to get playlists from**

**Returns:**

**a playlist or NULL if the last playlist has already been returned**

**See also:**

**NJB\_Reset\_Get\_Playlist()**

**NJB\_Playlist\_Destroy()**

**Examples:**  **pl.c, and playlists.c.**

**References njb\_struct::device\_type, NJB\_DEVICE\_NJB1, and njb\_struct::protocol\_state.**

### void NJB\_Playlist\_Addtrack (njb\_playlist\_t \* pl, njb\_playlist\_track\_t \* track, unsigned int pos) This function adds a playlist track (a data structure representing a track that is part of a playlist) to a playlist data structure.

**Parameters:**

*pl* **the playlist that the track shall be added to** \
 *track* **the track to add** \
 *pos* **the position in the playlist where this track should be added**

**See also:**

**NJB\_Playlist\_Deltrack()**

**Examples:**  **pl.c.**

**References njb\_playlist\_struct::\_state, njb\_playlist\_struct::cur, njb\_playlist\_struct::first, njb\_playlist\_struct::last, njb\_playlist\_track\_struct::next, NJB\_PL\_CHTRACKS, NJB\_PL\_NEW, NJB\_Playlist\_Gettrack(), NJB\_Playlist\_Reset\_Gettrack(), njb\_playlist\_struct::ntracks, and njb\_playlist\_track\_struct::prev.**

**Referenced by playlist\_unpack().**

### void NJB\_Playlist\_Deltrack (njb\_playlist\_t \* pl, unsigned int pos) This function deletes a track from a playlist.

**Parameters:**

*pl* **the playlist that the track shall be removed from** \
 *pos* **the position in the playlist to be deleted**

**See also:**

**NJB\_Playlist\_Addtrack()**

**NJB\_Playlist\_Deltrack\_TrackID()**

**References njb\_playlist\_struct::\_state, njb\_playlist\_struct::first, njb\_playlist\_struct::last, njb\_playlist\_track\_struct::next, NJB\_PL\_CHTRACKS, NJB\_Playlist\_Gettrack(), NJB\_Playlist\_Reset\_Gettrack(), NJB\_Playlist\_Track\_Destroy(), njb\_playlist\_struct::ntracks, and njb\_playlist\_track\_struct::prev.**

### void NJB\_Playlist\_Deltrack\_TrackID (njb\_playlist\_t \* pl, u\_int32\_t trackid) This function removes a track from a playlist by way of the track ID (as opposed to the position in the playlist). This function can be called even on playlists that don't have this track in them - this is useful for e.g. looping through all playlists and removing a certain track before deleting the track itself from the device.

**You need to call the NJB\_Update\_Playlist() function for each playlist that has been manipulated by this function, to assure that any changes are written back to the playlist on the device.**

**Typical use to remove a track with ID** `id` **from all playlists on a device:**

```
 njb_playlist_t *playlist;
```

```
 NJB_Reset_Get_Playlist(njb);
 while (playlist = NJB_Get_Playlist(njb)) {
    NJB_Playlist_Deltrack_TrackID(playlist, id);
    // If the playlist changed, update it
    if (NJB_Update_Playlist(njb, playlist) == -1)
        NJB_Error_Dump(njb, stderr);
    }
    NJB_Playlist_Destroy(playlist);
 }
 if (NJB_Error_Pending(njb)) {
    NJB_Error_Dump(njb, stderr);
 }
```

**Parameters:**

*pl* **the playlist that the track shall be removed from, if present.** \
 *trackid* **the track ID to remove from this playlist**

**See also:**

**NJB\_Playlist\_Deltrack()**

**NJB\_Update\_Playlist()**

**References njb\_playlist\_struct::\_state, njb\_playlist\_struct::first, njb\_playlist\_track\_struct::next, NJB\_PL\_CHTRACKS, NJB\_Playlist\_Gettrack(), NJB\_Playlist\_Reset\_Gettrack(), NJB\_Playlist\_Track\_Destroy(), njb\_playlist\_struct::ntracks, njb\_playlist\_track\_struct::prev, and njb\_playlist\_track\_struct::trackid.**

### void NJB\_Playlist\_Destroy (njb\_playlist\_t \* pl) This function destroys a playlist and frees up the memory used by it. All tracks that are part of the playlist will also be destroyed.

**Parameters:**

*pl* **the playlist to destroy**

**Examples:**  **pl.c.**

**References njb\_playlist\_struct::cur, njb\_playlist\_struct::first, njb\_playlist\_struct::name, njb\_playlist\_track\_struct::next, and NJB\_Playlist\_Track\_Destroy().**

**Referenced by playlist\_unpack().**

### njb\_playlist\_track\_t\* NJB\_Playlist\_Gettrack (njb\_playlist\_t \* pl) Returns a track from a playlist. The playlist has an internal structure to keep track of the constituent tracks, so the tracks will be retrieved in order. This function should typically be called repeatedly after an initial NJB\_Playlist\_Reset\_Gettrack() call.

**Parameters:**

*pl* **the playlist to get tracks from.**

**Returns:**

**a track or NULL of the last track from a playlist has already been returned**

**See also:**

**NJB\_Playlist\_Reset\_Gettrack()**

**Examples:**  **pl.c, and playlists.c.**

**References njb\_playlist\_struct::cur, and njb\_playlist\_track\_struct::next.**

**Referenced by NJB\_Playlist\_Addtrack(), NJB\_Playlist\_Deltrack(), NJB\_Playlist\_Deltrack\_TrackID(), and NJB\_Update\_Playlist().**

### njb\_playlist\_t\* NJB\_Playlist\_New (void) This function creates a new playlist data structure to hold a name and a number of tracks.

**Returns:**

**a new playlist structure**

**Examples:**  **pl.c.**

**References njb\_playlist\_struct::\_state, and NJB\_PL\_NEW.**

**Referenced by playlist\_unpack().**

### void NJB\_Playlist\_Reset\_Gettrack (njb\_playlist\_t \* pl) Resets the internal counter used when retrieveing tracks from a playlist. Should typically be called first, before any subsequent calls to NJB\_Playlist\_Gettrack().

**Parameters:**

*pl* **the playlist to be reset**

**See also:**

**NJB\_Playlist\_Gettrack()**

**Examples:**  **pl.c, and playlists.c.**

**References njb\_playlist\_struct::cur, and njb\_playlist\_struct::first.**

**Referenced by NJB\_Playlist\_Addtrack(), NJB\_Playlist\_Deltrack(), NJB\_Playlist\_Deltrack\_TrackID(), and NJB\_Update\_Playlist().**

### int NJB\_Playlist\_Set\_Name (njb\_playlist\_t \* pl, const char \* name) This function sets the name of the playlist. The name will be duplicated and stored internally, so the string is only needed during the function call.

**Parameters:**

*pl* **the playlist to set the name for** \
 *name* **the name to set for the playlist**

**Examples:**  **pl.c.**

**References njb\_playlist\_struct::\_state, njb\_playlist\_struct::name, NJB\_PL\_CHNAME, and NJB\_PL\_UNCHANGED.**

### void NJB\_Playlist\_Track\_Destroy (njb\_playlist\_track\_t \* track) This destroys a playlist track entry and frees any memory used by it.

**Parameters:**

*track* **the track entry to destroy.**

**Referenced by NJB\_Playlist\_Deltrack(), NJB\_Playlist\_Deltrack\_TrackID(), and NJB\_Playlist\_Destroy().**

### njb\_playlist\_track\_t\* NJB\_Playlist\_Track\_New (u\_int32\_t trackid) This creates a new track entry for playlists. The `trackid` used should be the same as retrieved from libnjb track reading functions.

**Parameters:**

*trackid* **the ID of the new track**

**Returns:**

**the new playlist track entry**

**Examples:**  **pl.c.**

**References njb\_playlist\_track\_struct::trackid.**

**Referenced by playlist\_unpack().**

### void NJB\_Reset\_Get\_Playlist (njb\_t \* njb) This resets the playlist retrieveal function. The playlists can then be retrieved one by one using the NJB\_Get\_Playlist() function.

**Typical usage:**

```
 njb_t *njb;
 njb_playlist_t *pl;
```

```
 NJB_Reset_Get_Playlist(njb);
 while ( (pl = NJB_Get_Playlist(njb)) != NULL ) {
    // Do something with all the playlists...
    NJB_Playlist_Destroy(pl);
 }
```

**Parameters:**

*njb* **a pointer to the** `njb_t` **object to reset the playlist retrieveal pointer for**

**See also:**

**NJB\_Get\_Playlist()**

**Examples:**  **pl.c, and playlists.c.**

**References njb\_struct::device\_type, njb3\_reset\_get\_playlist\_tag(), NJB\_DEVICE\_NJB1, and njb\_struct::protocol\_state.**

### int NJB\_Update\_Playlist (njb\_t \* njb, njb\_playlist\_t \* pl) This writes back an updated (modified) or new playlist to the device.

**This function** *may* **modify the playlist ID, i.e. the** `plid` **member of the** `njb_playlist_t` **struct, which means that if your program has cached this number anywhere, you need to update it using the value from** `pl->plid` **afterwards. This stems from the fact that playlists are sometimes updated by deleting the old playlist and creating a new one.**

**Parameters:**

*njb* **a pointer to the** `njb_t` **object to update the playlist on** \
 *pl* **the playlist to update.**

**Returns:**

**0 on success, -1 on failure.**

**Examples:**  **pl.c.**

**References njb\_playlist\_struct::\_state, njb\_struct::device\_type, EO\_NOMEM, njb\_playlist\_struct::name, njb3\_add\_multiple\_tracks\_to\_playlist(), njb3\_create\_playlist(), njb3\_update\_string\_frame(), NJB\_DEVICE\_NJB1, NJB\_PL\_CHNAME, NJB\_PL\_CHTRACKS, NJB\_PL\_NEW, NJB\_PL\_UNCHANGED, NJB\_Playlist\_Gettrack(), NJB\_Playlist\_Reset\_Gettrack(), NJB\_UC\_UTF8, njb\_verify\_last\_command(), njb\_playlist\_struct::ntracks, njb\_playlist\_struct::plid, strtoucs2(), njb\_playlist\_track\_struct::trackid, and utf8tostr().**

## Author

**Generated automatically by Doxygen for libnjb from the source code.**
