device.awk 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #
  2. # Copyright (C) 2005, 2006 Stephen Jungels
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful, but
  10. # WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # General Public License for more details.
  13. #
  14. # See COPYING for the full text of the license.
  15. # setup: get the list of preferred formats
  16. state==0 {
  17. numtypes = split (types, type, " ");
  18. lasttrack = "";
  19. state = 1;
  20. }
  21. # get file extension
  22. function typename(track, n, fields)
  23. {
  24. n = split (track, fields, ".");
  25. return fields[n];
  26. }
  27. # get file path minus extension
  28. function basename(track, j, n, fields, str, dot)
  29. {
  30. n = split (track, fields, ".");
  31. str = "";
  32. dot = "";
  33. for (j=1; j<n; j++)
  34. {
  35. str = str dot fields[j];
  36. dot = ".";
  37. }
  38. return str;
  39. }
  40. # if a sequence of identical tracks in different
  41. # formats has just ended, print the track in the
  42. # preferred format.
  43. function maybeprint(track, i, str)
  44. {
  45. str = basename(track);
  46. if (lasttrack != str)
  47. {
  48. if (lasttrack != "")
  49. {
  50. for (i=1; i<=numtypes; i++)
  51. {
  52. if (tracktypes ~ type[i])
  53. {
  54. print lasttrack "." type[i];
  55. break;
  56. }
  57. }
  58. }
  59. lasttrack = str;
  60. tracktypes = typename(track);
  61. }
  62. else
  63. {
  64. tracktypes = tracktypes " " typename(track);
  65. }
  66. }
  67. # process each input line
  68. state==1 {
  69. maybeprint($0);
  70. }
  71. END {
  72. maybeprint("");
  73. }