?login_element?

Subversion Repositories NedoOS

Rev

Rev 1961 | Rev 1972 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <oscalls.h>
  6. #include <osfs.h>
  7. #include <intrz80.h>
  8. #include <ctype.h>
  9. #include <tcp.h>
  10. #include <graphic.h>
  11. #include <terminal.c>
  12. struct fileStruct
  13. {
  14.   long picId;
  15.   unsigned int picYear;
  16.   unsigned long totalAmount;
  17.   unsigned char picRating[8];
  18.   unsigned char picName[256];
  19.   unsigned char picType[64];
  20.   unsigned char authorIds[64];
  21.   unsigned char authorTitle[64];
  22.   unsigned char authorRealName[64];
  23.   unsigned char afn[128];
  24.   unsigned char pfn[128];
  25.   unsigned char fileName[128];
  26. } curFileStruct;
  27. unsigned char ver[] = "2.2";
  28. unsigned char netbuf[2048];
  29. unsigned char picture[16384];
  30. unsigned char crlf[2] = {13, 10};
  31. unsigned long bytecount;
  32. unsigned char status, keypress, verbose, randomPic, slideShow;
  33. struct sockaddr_in targetadr;
  34. struct readstructure readStruct;
  35. unsigned long contLen;
  36. unsigned long count;
  37. unsigned int headlng;
  38. unsigned int slideShowTime = 0;
  39.  
  40. struct packetStruct
  41. {
  42. unsigned long contLen;
  43. unsigned int headlng;
  44. } pack;
  45.  
  46. void emptyKeyBuf(void)
  47. {
  48.   do
  49.   {
  50.   } while (_low_level_get() != 0);
  51. }
  52.  
  53. void printHelp(void)
  54. {
  55.   ATRIB(95);
  56.   printf("   GETPIC [%s] zxart.ee picture viewer for nedoNET\n\r", ver);
  57.   ATRIB(33);
  58.   ATRIB(40);
  59.   printf("-------------------------------------------------------\n\r");
  60.   printf(" Управление:\n\r");
  61.   printf(" 'ESC' - выход из программы;\n\r");
  62.   printf(" '<-' или 'B' к последним картинкам;\n\r");
  63.   printf(" '->' или 'Пробел' к более старым картинкам\n\r");
  64.   printf(" 'J' Прыжок на  указанную по счету картинку\n\r");
  65.   printf(" 'I' Просмотр экрана информации о картинках\n\r");
  66.   printf(" 'S' Сохранить картинку на диск в текущую папку\n\r");
  67.   printf(" 'V' не выводить информацию об авторах\n\r");
  68.   printf(" 'R' переход в режим  случайная картинка с рейтингом 4+\n\r");
  69.   printf(" 'A' переход в режим  слайд-шоу\n\r");
  70.   printf(" 'H' Данная справочная информация\n\r");
  71.   printf("-----------------Нажмите любую кнопку------------------\n\r");
  72.   ATRIB(93);
  73.   do
  74.   {
  75.     YIELD();
  76.     keypress = _low_level_get();
  77.   } while (keypress == 0);
  78.   emptyKeyBuf();
  79. }
  80. void delay(unsigned long counter)
  81. {
  82.   unsigned long start, finish;
  83.   counter = counter / 20;
  84.   if (counter < 1)
  85.   {
  86.     counter = 1;
  87.   }
  88.   start = time();
  89.   finish = start + counter;
  90.  
  91.   while (start < finish)
  92.   {
  93.     start = time();
  94.   }
  95. }
  96.  
  97. void errorPrint(unsigned int error)
  98. {
  99.   switch (error)
  100.   {
  101.   case 2:
  102.     printf("02 SHUT_RDWR\n\r");
  103.     break;
  104.   case 4:
  105.     printf("04 ERR_INTR\n\r");
  106.     break;
  107.   case 23:
  108.     printf("23 ERR_NFILE\n\r");
  109.     break;
  110.   case 35:
  111.     printf("35 ERR_EAGAIN or ERR_EWOULDBLOCK\n\r");
  112.     break;
  113.   case 37:
  114.     printf("37 ERR_ALREADY\n\r");
  115.     break;
  116.   case 38:
  117.     printf("38 ERR_NOTSOCK\n\r");
  118.     break;
  119.   case 40:
  120.     printf("40 ERR_EMSGSIZE\n\r");
  121.     break;
  122.   case 41:
  123.     printf("41 ERR_PROTOTYPE\n\r");
  124.     break;
  125.   case 47:
  126.     printf("47 ERR_AFNOSUPPORT\n\r");
  127.     break;
  128.   case 53:
  129.     printf("53 ERR_ECONNABORTED\n\r");
  130.     break;
  131.   case 54:
  132.     printf("54 ERR_CONNRESET\n\r");
  133.     break;
  134.   case 57:
  135.     printf("57 ERR_NOTCONN\n\r");
  136.     break;
  137.   case 65:
  138.     printf("65 ERR_HOSTUNREACH\n\r");
  139.     break;
  140.   default:
  141.     printf("%u UNKNOWN ERROR\n\r", error);
  142.     break;
  143.   }
  144.   do
  145.   {
  146.     YIELD();
  147.   } while (_low_level_get() == 0);
  148.   emptyKeyBuf();
  149. }
  150.  
  151. unsigned char OpenSock(unsigned char family, unsigned char protocol)
  152. {
  153.   unsigned char socket;
  154.   unsigned int todo;
  155.   todo = OS_NETSOCKET((family << 8) + protocol);
  156.   if (todo > 32767)
  157.   {
  158.     printf("OS_NETSOCKET: ");
  159.     errorPrint(todo & 255);
  160.     exit(0);
  161.   }
  162.   else
  163.   {
  164.     socket = ((todo & 65280) >> 8);
  165.     // printf ("OS_NETSOCKET: Socket #%d created\n\r", socket);
  166.   }
  167.   return socket;
  168. }
  169.  
  170. unsigned int netShutDown(unsigned char socket, unsigned char type)
  171. {
  172.   unsigned int todo;
  173.   todo = OS_NETSHUTDOWN(socket, type);
  174.   if (todo > 32767)
  175.   {
  176.     printf("OS_NETSHUTDOWN: ");
  177.     errorPrint(todo & 255);
  178.     return 255;
  179.   }
  180.   else
  181.   {
  182.     // printf ("Socket #%u closed.\n\r", socket);
  183.   }
  184.   return 0;
  185. }
  186.  
  187. unsigned char netConnect(unsigned char socket)
  188. {
  189.   unsigned int todo, retry = 10;
  190.  
  191.   targetadr.family = AF_INET;
  192.   targetadr.porth = 00;
  193.   targetadr.portl = 80;
  194.   targetadr.b1 = 217;
  195.   targetadr.b2 = 146;
  196.   targetadr.b3 = 69;
  197.   targetadr.b4 = 13;
  198.   while (retry > 0)
  199.   {
  200.     todo = OS_NETCONNECT(socket, &targetadr);
  201.  
  202.     if (todo > 32767)
  203.     {
  204.       netShutDown(socket, 1);
  205.       socket = OpenSock(AF_INET, SOCK_STREAM);
  206.       printf("OS_NETCONNECT [ERROR:%u] [Retry:%u] [Pic:%lu]\n\r", (todo & 255), retry, count);
  207.       retry--;
  208.       delay(200);
  209.     }
  210.     else
  211.     {
  212.       // printf("OS_NETCONNECT: connection successful, %u\n\r", (todo & 255));
  213.       return 0;
  214.     }
  215.   }
  216.   printf("OS_NETCONNECT: ");
  217.   errorPrint(todo & 255);
  218.   exit(0);
  219.   return 0;
  220. }
  221.  
  222. unsigned int tcpSend(unsigned char socket, unsigned int messageadr, unsigned int size)
  223. {
  224.   unsigned char retry = 50;
  225.   unsigned int todo;
  226.   readStruct.socket = socket;
  227.   readStruct.BufAdr = messageadr;
  228.   readStruct.bufsize = size;
  229.   readStruct.protocol = SOCK_STREAM;
  230.  
  231. wizwrite:
  232.   todo = OS_WIZNETWRITE(&readStruct);
  233.   if (todo > 32767)
  234.   {
  235.  
  236.     if (retry == 0)
  237.     {
  238.       printf("OS_WIZNETWRITE: ");
  239.       errorPrint(todo & 255);
  240.       exit(0);
  241.     }
  242.     retry--;
  243.     YIELD();
  244.     goto wizwrite;
  245.   }
  246.   else
  247.   {
  248.     //  printf("OS_WIZNETWRITE: %u bytes written. \n\r", todo);
  249.   }
  250.   return todo;
  251. }
  252.  
  253. unsigned int tcpRead(unsigned char socket)
  254. {
  255.   unsigned char retry = 75;
  256.   unsigned int err, todo;
  257.  
  258.   readStruct.socket = socket;
  259.   readStruct.BufAdr = (unsigned int)&netbuf;
  260.   readStruct.bufsize = sizeof(netbuf);
  261.   readStruct.protocol = SOCK_STREAM;
  262. wizread:
  263.   todo = OS_WIZNETREAD(&readStruct);
  264.   if (todo > 32767)
  265.   {
  266.     if (retry == 0)
  267.     {
  268.       err = todo & 255;
  269.       printf("OS_WIZNETREAD: ");
  270.       errorPrint(err);
  271.       if (err == 35)
  272.       {
  273.         retry = 75;
  274.         return 0;
  275.       }
  276.       exit(0);
  277.     }
  278.     retry--;
  279.     // printf("OS_WIZNETREAD: %u \n\r", retry);
  280.     YIELD();
  281.     delay(100);
  282.     goto wizread;
  283.   }
  284.   // printf("OS_WIZNETREAD: %u bytes read. \n\r", todo);
  285.   return todo;
  286. }
  287.  
  288. unsigned int cutHeader(unsigned int todo)
  289. {
  290.   unsigned int q;
  291.   unsigned char *count1;
  292.   count1 = strstr(netbuf, "Content-Length:");
  293.   if (count1 == NULL)
  294.   {
  295.     printf("contLen  not found \r\n");
  296.     contLen = 0;
  297.   }
  298.   else
  299.   {
  300.     contLen = atol(count1 + 15);
  301.     bytecount = contLen;
  302.     // printf ("Dlinna  soderzhimogo = %lu \n\r", bytecount);
  303.   }
  304.  
  305.   count1 = strstr(netbuf, "\r\n\r\n");
  306.   if (count1 == NULL)
  307.   {
  308.     printf("header not found\r\n");
  309.   }
  310.   else
  311.   {
  312.     headlng = ((unsigned int)count1 - (unsigned int)netbuf + 4);
  313.     q = todo - headlng;
  314.     //memcpy(&netbuf, count1 + 4, q);
  315.     // printf ("header removed. %u bytes\r\n", headlng);
  316.   }
  317.  
  318.   return q;
  319. }
  320.  
  321. char *str_replace(char *dst, int num, const char *str,
  322.                   const char *orig, const char *rep)
  323. {
  324.   const char *ptr;
  325.   size_t len1 = strlen(orig);
  326.   size_t len2 = strlen(rep);
  327.   char *tmp = dst;
  328.  
  329.   num -= 1;
  330.   while ((ptr = strstr(str, orig)) != NULL)
  331.   {
  332.     num -= (ptr - str) + len2;
  333.     if (num < 1)
  334.       break;
  335.  
  336.     strncpy(dst, str, (size_t)(ptr - str));
  337.     dst += ptr - str;
  338.     strncpy(dst, rep, len2);
  339.     dst += len2;
  340.     str = ptr + len1;
  341.   }
  342.  
  343.   for (; (*dst = *str) && (num > 0); --num)
  344.   {
  345.     ++dst;
  346.     ++str;
  347.   }
  348.   return tmp;
  349. }
  350.  
  351. void fillPicture(unsigned char socket)
  352. {
  353.   unsigned int todo, w, pPos, headskip;
  354.  
  355.   headskip = 0;
  356.   pPos = 0;
  357.   bytecount = 255;
  358.   while (1)
  359.   {
  360.     headlng = 0;
  361.     todo = tcpRead(socket);
  362.     if (todo == 0)
  363.     {
  364.       break;
  365.     }
  366.     if (headskip == 0)
  367.     {
  368.       headskip = 1;
  369.       todo = cutHeader(todo);
  370.     }
  371.  
  372.     if (pPos + todo > sizeof(picture))
  373.     {
  374.       printf("dataBuffer overrun... %u reached \n\r", pPos + todo);
  375.       break;
  376.     }
  377.  
  378.     for (w = 0; w < todo; w++)
  379.     {
  380.       picture[w + pPos] = netbuf[w + headlng];
  381.     }
  382.     bytecount = bytecount - todo;
  383.     pPos = pPos + todo;
  384.     if (bytecount == 0)
  385.     {
  386.       break;
  387.     }
  388.   }
  389.   netShutDown(socket, 1);
  390. }
  391. void nameRepair(unsigned char *pfn, unsigned int tfnSize)
  392. {
  393.  
  394.   str_replace(pfn, tfnSize, pfn, "\\", "_");
  395.   str_replace(pfn, tfnSize, pfn, "/", "_");
  396.   str_replace(pfn, tfnSize, pfn, ":", "_");
  397.   str_replace(pfn, tfnSize, pfn, "*", "_");
  398.   str_replace(pfn, tfnSize, pfn, "?", "_");
  399.   str_replace(pfn, tfnSize, pfn, "<", "_");
  400.   str_replace(pfn, tfnSize, pfn, ">", "_");
  401.   str_replace(pfn, tfnSize, pfn, "|", "_");
  402.   str_replace(pfn, tfnSize, pfn, " ", "_");
  403.   str_replace(pfn, tfnSize, pfn, "&#039;", "'");
  404.   str_replace(pfn, tfnSize, pfn, "&amp;", "&");
  405.   str_replace(pfn, tfnSize, pfn, "&quot;", "'");
  406.   str_replace(pfn, tfnSize, pfn, "&gt;", ")");
  407.   str_replace(pfn, tfnSize, pfn, "&lt;", "(");
  408.   str_replace(pfn, tfnSize, pfn, "\"", "'");
  409. }
  410.  
  411. void stringRepair(unsigned char *pfn, unsigned int tSize)
  412. {
  413.   str_replace(pfn, tSize, pfn, "&#039;", "'");
  414.   str_replace(pfn, tSize, pfn, "&amp;", "&");
  415.   str_replace(pfn, tSize, pfn, "&gt;", ">");
  416.   str_replace(pfn, tSize, pfn, "&lt;", "<");
  417.   str_replace(pfn, tSize, pfn, "&quot;", "\"");
  418.   str_replace(pfn, tSize, pfn, "\\/", "/");
  419. }
  420.  
  421. unsigned char getPic(unsigned long fileId)
  422. {
  423.   unsigned int todo;
  424.   unsigned char buffer[] = "0000000000";
  425.   unsigned char socket;
  426.   socket = OpenSock(AF_INET, SOCK_STREAM);
  427.   todo = netConnect(socket);
  428.   netbuf[0] = '\0';
  429.   sprintf(buffer, "%lu", fileId);
  430.   strcat(netbuf, "GET /file/id:");
  431.   strcat(netbuf, buffer);
  432.   strcat(netbuf, " HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: User-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS)\r\n\r\n\0");
  433.   todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf));
  434.   fillPicture(socket);
  435.   return 0;
  436. }
  437. unsigned char savePic(unsigned long fileId)
  438. {
  439.   FILE *fp2;
  440.   unsigned char afnSize, tfnSize;
  441.   unsigned char fileIdChar[10];
  442.  
  443.   afnSize = sizeof(curFileStruct.afn) - 1;
  444.   tfnSize = sizeof(curFileStruct.pfn) - 1;
  445.  
  446.   strcpy(curFileStruct.afn, curFileStruct.authorTitle);
  447.   nameRepair(curFileStruct.afn, afnSize);
  448.  
  449.   strcpy(curFileStruct.pfn, curFileStruct.picName);
  450.   nameRepair(curFileStruct.pfn, tfnSize);
  451.  
  452.   sprintf(curFileStruct.fileName, "%s-%s-%ld.scr", curFileStruct.afn, curFileStruct.pfn, fileId);
  453.   if (strlen(curFileStruct.fileName) > 62)
  454.   {
  455.     sprintf(fileIdChar, "-%ld", fileId);
  456.     str_replace(curFileStruct.fileName, sizeof(curFileStruct.fileName) - 1, curFileStruct.fileName, fileIdChar, "");
  457.     curFileStruct.fileName[50] = '\0';
  458.     strcat(curFileStruct.fileName, fileIdChar);
  459.     strcat(curFileStruct.fileName, ".scr");
  460.   }
  461.   OS_SETSYSDRV();
  462.   OS_MKDIR("../downloads");        // Create if not exist
  463.   OS_MKDIR("../downloads/getpic"); // Create if not exist
  464.   OS_CHDIR("../downloads/getpic");
  465.   fp2 = OS_CREATEHANDLE(curFileStruct.fileName, 0x80);
  466.   if (((int)fp2) & 0xff)
  467.   {
  468.     printf(curFileStruct.fileName);
  469.     printf(" creating error\r\n");
  470.     getchar();
  471.     exit(0);
  472.   }
  473.   OS_WRITEHANDLE(picture, fp2, 6912);
  474.   OS_CLOSEHANDLE(fp2);
  475.   return 0;
  476. }
  477.  
  478. int pos(unsigned char *s, unsigned char *c, unsigned int n, unsigned int startPos)
  479. {
  480.   unsigned int i, j;
  481.   unsigned int lenC, lenS;
  482.  
  483.   for (lenC = 0; c[lenC]; lenC++)
  484.     ;
  485.   for (lenS = 0; s[lenS]; lenS++)
  486.     ;
  487.  
  488.   for (i = startPos; i <= lenS - lenC; i++)
  489.   {
  490.     for (j = 0; s[i + j] == c[j]; j++)
  491.       ;
  492.  
  493.     if (j - lenC == 1 && i == lenS - lenC && !(n - 1))
  494.       return i;
  495.     if (j == lenC)
  496.       if (n - 1)
  497.         n--;
  498.       else
  499.         return i;
  500.   }
  501.   return -1;
  502. }
  503.  
  504. const char *parseJson(unsigned char *property)
  505. {
  506.   unsigned int w, lng, lngp1, findEnd, listPos;
  507.   unsigned char terminator;
  508.   int n;
  509.   n = -1;
  510.   netbuf[0] = '\0';
  511.   n = pos(picture, property, 1, 0);
  512.   if (n == -1)
  513.   {
  514.     strcpy(netbuf, "Not Found");
  515.     // printf("Property %s not found", property);
  516.     return netbuf;
  517.   }
  518.   lng = n - 1 + strlen(property);
  519.   if (picture[lng] == ':')
  520.   {
  521.     terminator = '\0';
  522.   }
  523.   if (picture[lng] == '\"')
  524.   {
  525.     terminator = '\"';
  526.   }
  527.   if (picture[lng] == '[')
  528.   {
  529.     terminator = ']';
  530.   }
  531.  
  532.   findEnd = 1;
  533.   lngp1 = lng + 1;
  534.  
  535.   while (42)
  536.   {
  537.  
  538.     if ((picture[lngp1 + findEnd] == ','))
  539.     {
  540.       if (terminator == '\0')
  541.       {
  542.         break;
  543.       }
  544.       if ((picture[lng + findEnd] == terminator))
  545.       {
  546.         findEnd--;
  547.         break;
  548.       }
  549.     }
  550.     findEnd++;
  551.   }
  552.   listPos = 0;
  553.   for (w = lngp1; w < findEnd + lngp1; w++)
  554.   {
  555.     netbuf[listPos] = picture[w];
  556.     listPos++;
  557.   }
  558.   netbuf[listPos] = '\0';
  559.   return netbuf;
  560. }
  561. void convert866(void)
  562. {
  563.   unsigned int lng, targetPos, w, q = 0;
  564.   unsigned char buffer[8], one, two;
  565.   unsigned int decVal;
  566.   lng = strlen(netbuf);
  567.   targetPos = lng + 1;
  568.  
  569.   while (q < lng)
  570.   {
  571.     one = netbuf[q];
  572.     two = netbuf[q + 1];
  573.     if (one == 92 && two == 117)
  574.     {
  575.       q = q + 2;
  576.       for (w = 0; w < 4; w++)
  577.       {
  578.         buffer[w] = netbuf[q + w];
  579.       }
  580.       q = q + 4;
  581.       buffer[4] = '\0';
  582.       decVal = (unsigned int)strtol(buffer, NULL, 16);
  583.  
  584.       if (decVal < 1088)
  585.       {
  586.         decVal = decVal - 912;
  587.       }
  588.       if (decVal > 1087)
  589.       {
  590.         decVal = decVal - 864;
  591.       }
  592.       if (decVal == 1025)
  593.       {
  594.         decVal = 240;
  595.       }
  596.       if (decVal == 1105)
  597.       {
  598.         decVal = 241;
  599.       }
  600.       netbuf[targetPos] = decVal;
  601.     }
  602.     else
  603.     {
  604.       netbuf[targetPos] = netbuf[q];
  605.       q++;
  606.     }
  607.     targetPos++;
  608.   }
  609.   netbuf[targetPos] = '\0';
  610.  
  611.   for (w = lng + 1; w < targetPos + 1; w++)
  612.   {
  613.     netbuf[w - lng - 1] = netbuf[w];
  614.   }
  615. }
  616.  
  617. unsigned long processJson(unsigned long startPos, unsigned char limit, unsigned char queryNum)
  618. {
  619.   unsigned int retry, tSize;
  620.   unsigned int todo, pPos, headskip;
  621.   unsigned char buffer[] = "000000000";
  622.   unsigned char *count, socket;
  623.   unsigned char userAgent[] = " HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS; GetPic)\r\n\r\n\0";
  624.   retry = 10;
  625.   netbuf[0] = '\0';
  626.   switch (queryNum)
  627.   {
  628.   case 0:
  629.     strcat(netbuf, "GET /api/export:zxPicture/filter:zxPictureType=standard/limit:");
  630.     sprintf(buffer, "%u", limit);
  631.     strcat(netbuf, buffer);
  632.     strcat(netbuf, "/start:");
  633.     sprintf(buffer, "%lu", startPos);
  634.     strcat(netbuf, buffer);
  635.     strcat(netbuf, "/order:date,desc");
  636.     strcat(netbuf, userAgent);
  637.     break;
  638.  
  639.   case 1:
  640.     strcat(netbuf, "GET /api/types:zxPicture/export:zxPicture/language:eng/start:0/limit:1/order:rand/filter:zxPictureMinRating=4;zxPictureType=standard");
  641.     strcat(netbuf, userAgent);
  642.     break;
  643.  
  644.   case 3: // /api/export:author/filter:authorId=2202
  645.     strcat(netbuf, "GET /api/export:author/filter:authorId=");
  646.     sprintf(buffer, "%lu", startPos);
  647.     strcat(netbuf, buffer);
  648.     strcat(netbuf, userAgent);
  649.     break;
  650.  
  651.   case 99: // GET /jsonElementData/elementId:182798
  652.     strcpy(netbuf, "GET /jsonElementData/elementId:");
  653.     sprintf(buffer, "%lu", startPos);
  654.     strcat(netbuf, buffer);
  655.     strcat(netbuf, userAgent);
  656.     break;
  657.   }
  658.  
  659. rejson:
  660.   socket = OpenSock(AF_INET, SOCK_STREAM);
  661.   netConnect(socket);
  662.  
  663.   todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf));
  664.  
  665.   headskip = 0;
  666.   pPos = 0;
  667.   fillPicture(socket);
  668.  
  669.   count = strstr(picture, "responseStatus\":\"success");
  670.   if (count == NULL)
  671.   {
  672.     ATRIB(91);
  673.     printf("BAD JSON, NO responseStatus: success. %u   \r\n", retry);
  674.     retry--;
  675.     YIELD();
  676.     if (retry > 0)
  677.       goto rejson;
  678.     return -1;
  679.   }
  680.  
  681.   count = strstr(picture, "\"id\":");
  682.   if (count == NULL)
  683.   {
  684.     ATRIB(91);
  685.     printf("BAD JSON: ID not found.\r\n");
  686.     return -2;
  687.   }
  688.  
  689.   netbuf[0] = '\0';
  690.   if (queryNum < 3)
  691.   {
  692.     parseJson("\"id\":");
  693.     curFileStruct.picId = atol(netbuf);
  694.     parseJson(",\"title\":\"");
  695.     convert866();
  696.     strcpy(curFileStruct.picName, netbuf);
  697.  
  698.     tSize = sizeof(curFileStruct.picName);
  699.     stringRepair(curFileStruct.picName, tSize);
  700.  
  701.     parseJson(",\"type\":\"");
  702.     strcpy(curFileStruct.picType, netbuf);
  703.     parseJson("\"rating\":\"");
  704.     strcpy(curFileStruct.picRating, netbuf);
  705.     parseJson("\"year\":\"");
  706.     curFileStruct.picYear = atoi(netbuf);
  707.     parseJson("\"totalAmount\":");
  708.     curFileStruct.totalAmount = atol(netbuf);
  709.     parseJson("\"authorIds\":[");
  710.     strcpy(curFileStruct.authorIds, netbuf);
  711.   }
  712.   if (queryNum == 99)
  713.   {
  714.     parseJson(",\"title\":\"");
  715.     convert866();
  716.     strcpy(curFileStruct.authorTitle, netbuf);
  717.     parseJson(",\"realName\":\"");
  718.     convert866();
  719.     strcpy(curFileStruct.authorRealName, netbuf);
  720.   }
  721.   return curFileStruct.picId;
  722. }
  723.  
  724. void printData(void)
  725. {
  726.   ATRIB(93);
  727.   printf(" #: ");
  728.   ATRIB(97);
  729.   printf("%lu", count);
  730.   ATRIB(93);
  731.   printf(" ID: ");
  732.   ATRIB(97);
  733.   printf("%lu ", curFileStruct.picId);
  734.   ATRIB(93);
  735.   printf(" Total Pics: ");
  736.   ATRIB(97);
  737.   printf("%lu \r\n", curFileStruct.totalAmount);
  738.   ATRIB(93);
  739.   printf(" Author: ");
  740.   ATRIB(96);
  741.   printf("%s\r\n", curFileStruct.authorTitle);
  742.   ATRIB(93);
  743.   printf(" TITLE: ");
  744.   ATRIB(95);
  745.   printf("%s\r\n", curFileStruct.picName);
  746.   ATRIB(93);
  747.   printf(" RATING: ");
  748.   ATRIB(97);
  749.   printf("%s", curFileStruct.picRating);
  750.   ATRIB(93);
  751.   printf(" YEAR: ");
  752.   ATRIB(97);
  753.   printf("%u", curFileStruct.picYear);
  754.   printf(" \r\n");
  755.   ATRIB(93);
  756.   printf(" AuthorsIDs ");
  757.   ATRIB(97);
  758.   printf("%s", curFileStruct.authorIds);
  759.   ATRIB(93);
  760.   printf(" Real name: ");
  761.   ATRIB(97);
  762.   printf("%s", curFileStruct.authorRealName);
  763.   printf(" \r\n");
  764.   ATRIB(96);
  765.   printf(" \r\n");
  766. }
  767. void safeKeys(unsigned char keypress)
  768. {
  769.   if (keypress == 27)
  770.   {
  771.     printf("Good bye...\r\n");
  772.     ATRIB(37);
  773.     ATRIB(40);
  774.     exit(0);
  775.   }
  776.  
  777.   if (keypress == 'j' || keypress == 'J')
  778.   {
  779.     printf("Jump to picture:");
  780.     scanf("%lu", &count);
  781.     if (count > curFileStruct.totalAmount - 1)
  782.     {
  783.       count = curFileStruct.totalAmount - 1;
  784.     }
  785.   }
  786.  
  787.   if (keypress == 'v' || keypress == 'V')
  788.   {
  789.     verbose = !verbose;
  790.  
  791.     if (verbose == 0)
  792.     {
  793.       BOX(1, 1, 80, 25, 40);
  794.       AT(1, 1);
  795.     }
  796.   }
  797.  
  798.   if (keypress == 'h' || keypress == 'H')
  799.   {
  800.     printHelp();
  801.   }
  802.  
  803.   if (keypress == 'r' || keypress == 'R')
  804.   {
  805.     randomPic = !randomPic;
  806.  
  807.     if (verbose == 1)
  808.     {
  809.       if (randomPic == 1)
  810.       {
  811.         printf("    Random mode enabled...\r\n");
  812.       }
  813.       else
  814.       {
  815.         printf("    Sequental mode enabled...\r\n");
  816.       }
  817.     }
  818.   }
  819.   if (keypress == 'a' || keypress == 'A')
  820.   {
  821.     slideShow = !slideShow;
  822.     if (slideShow == 1)
  823.     {
  824.       if (verbose == 1)
  825.         printf("    SlideShow mode enabled...\r\n\r\n");
  826.       slideShowTime = 250;
  827.     }
  828.     else
  829.     {
  830.       if (verbose == 1)
  831.         printf("    Manual mode enabled...\r\n\r\n");
  832.       slideShowTime = 0;
  833.     }
  834.   }
  835. }
  836.  
  837. C_task main(void)
  838. {
  839.   unsigned char errno;
  840.   unsigned long ipadress;
  841.   long iddqd, idkfa;
  842.   os_initstdio();
  843.  
  844.   count = 0;
  845.   verbose = 1;
  846.   randomPic = 0;
  847.   slideShow = 0;
  848.  
  849.   BOX(1, 1, 80, 25, 40);
  850.   AT(1, 1);
  851.   printHelp();
  852.   safeKeys(keypress);
  853.  
  854. start:
  855.   emptyKeyBuf();
  856.   switch (randomPic)
  857.   {
  858.   case 0:
  859.     iddqd = processJson(count, 1, 0);
  860.     break;
  861.   case 1:
  862.     iddqd = processJson(0, 1, 1);
  863.     break;
  864.   }
  865.  
  866.   if (iddqd < 0)
  867.   {
  868.     count++;
  869.     goto start;
  870.   }
  871.   idkfa = processJson(atol(curFileStruct.authorIds), 0, 99);
  872.   if (idkfa < 0)
  873.   {
  874.     printf(" Cant parse curFileStruct.authorIds = %s \r\n\r\n", curFileStruct.authorIds);
  875.     count++;
  876.     goto start;
  877.   }
  878.  
  879.   if (verbose == 1)
  880.   {
  881.     printData();
  882.   }
  883.   else
  884.   {
  885.     // ATRIB(97);
  886.     // printf(" Getting picture...\r\n");
  887.   }
  888.   if (!strcmp(curFileStruct.picType, "standard"))
  889.  
  890.   {
  891.     errno = getPic(iddqd);
  892.  
  893.   review:
  894.     keypress = viewScreen6912((unsigned int)&picture, slideShowTime);
  895.     emptyKeyBuf();
  896.   }
  897.   else
  898.   {
  899.     printf("  >>Format %s not supported, skipped \n\r", curFileStruct.picType);
  900.     count++;
  901.     goto start;
  902.   }
  903.  
  904.   ///// Keys only for pictures
  905.   if (keypress == 's' || keypress == 'S')
  906.   {
  907.     savePic(iddqd);
  908.     if (verbose == 1)
  909.       printf("        ID:%lu    TITLE:%s  SAVED\r\n\r\n", curFileStruct.picId, curFileStruct.picName);
  910.     count++;
  911.   }
  912.  
  913.   if (keypress == 248 || keypress == 'b' || keypress == 'B')
  914.   {
  915.     if (count > 0)
  916.     {
  917.       count--;
  918.     }
  919.   }
  920.   if (keypress == 251 || keypress == 32)
  921.   {
  922.     count++;
  923.     goto start;
  924.   }
  925.   if (keypress == 'i' || keypress == 'I')
  926.   {
  927.     do
  928.     {
  929.       YIELD();
  930.     } while (_low_level_get() == 0);
  931.     emptyKeyBuf();
  932.     goto review;
  933.   }
  934.   safeKeys(keypress);
  935.   goto start;
  936. }
  937.